单片机外设(五)ADC

#新星杯·14天创作挑战营·第14期#

一.ADC介绍

ADC(Analog - to - digital converter,模拟数字转换器)核心知识提取

  • 定义:将模拟形式连续信号转换为数字形式离散信号的设备,把连续变量模拟信号转离散数字信号 。
  • 应用需求:现实中温度、压力等模拟信号,需转换为易储存、处理的数字形式,依靠ADC实现模拟量到数字量转换存储 。
  • 基本原理:经采样、保持、量化、编码四个步骤完成转换 。
  • 主要技术指标
    • 测量范围:决定外接设备信号输出电压范围 。
    • 分辨率:用输出二进制数位数表示,位数越多,误差越小、转换精度越高 。
    • 转换速度:A/D转换器完成一次转换所需时间 。
    • 采样时间:保持某时刻外部信号的时间 。
    • 采样率:一秒内采集次数 。

在这里插入图片描述

二 I.MX6ull ADC介绍

1. Features(特性)

  • 采样率:Up to 1MS/s sampling rate(高达1MS/s的采样率 )
  • 转换模式:Single or continuous conversion (automatic return to idle after single conversion)(单次或连续转换,单次转换后自动返回空闲 )
  • 输出模式:Output Modes: (in right - justified unsigned format) 12 - bit/10 - bit/8 - bit(输出模式:右对齐无符号格式,12位/10位/8位 )
  • 可配置项:Configurable sample time and conversion speed/power(可配置采样时间和转换速度/功率 )
  • 标志与中断:Conversion complete and hardware average complete flag and interrupt(转换完成和硬件平均完成标志及中断 )
  • 时钟选择:Input clock selectable from up to three sources(输入时钟可从多达三个源选择 )
  • 自动比较与中断:Automatic compare with interrupt for less - than, greater - than or equal - to, within range, or out - of - range, programmable value(对小于、大于或等于、范围内或范围外的可编程值自动比较并产生中断 )
  • 低功耗运行:Operation in low power modes for lower noise operation(在低功率模式下运行,实现更低噪声运行 )
  • 硬件功能:Hardware average function(硬件平均函数 );Self - calibration mode(自校准模式 )

2.ADC状态

  • Disabled State(禁用状态)

描述:The ADC module is disabled during reset or stop mode (if internal clock is not selected as source of clock).(在复位或停止模式下(如果未选择内部时钟作为时钟源),ADC模块将被禁用 )

  • Idle State(闲置状态)

  • 描述:The module is idle when a conversion has completed and another conversion has not been initiated. When idle and the asynchronous clock output enable is disabled (ADACKEN = 0), the module is in its lowest power state(当一个转换完成而另一个转换尚未启动时,模块处于空闲状态。当空闲并且异步时钟输出使能被禁用(ADACKEN = 0)时,模块处于最低功率状态。)

  • Conversion State(转换状态)

  • 描述:The ADC can perform an analog - to - digital conversion on any of the software selectable channels. All modes perform conversion by a successive approximation algorithm.(ADC可以在任何软件可选通道上执行模数转换。所有模式都通过逐次逼近算法执行转换 )

3.ADC block diagram

在这里插入图片描述

4.ADC Clock Select and Divide Control

The ADC digital module has two clock sources(ADC数字模块有两个时钟源)

  • 时钟源类型
    • IPG clock(IPG时钟)
    • Internal clock (ADACK) is a dedicated clock used only by the ADC(内部时钟(ADACK)是仅由ADC使用的专用时钟)
  • 时钟生成与选择
    • ADC digital block generates IPG clock/2 by internally dividing the IPG clock. The final clock is chosen from the following clocks. (ADC数字块通过内部分割IPG时钟产生IPG时钟/2,最终时钟从以下时钟中选择)
    • 可选时钟:IPG clock (IPG时钟);IPG clock divided by 2 (IPG时钟除以2);ADACK
  • 时钟配置逻辑
    From the three clocks listed above, one is chosen depending on the configuration of ADICLK[1:0] bits of ADC_CFG. This chosen clock is divided depending on the configuration of ADIV[1:0] bits of ADC_CFG. The final generated clock is used as conversion clock for ADC.(从上面列出的三个时钟中,根据ADC_CFG的ADICLK[1:0]位的配置选择一个。此选择的时钟根据ADC_CFG的ADIV[1:0]位的配置进行分频,最终生成的时钟用作ADC的转换时钟)
ADICLKSelected Clock Source
00IPG clock
01IPG clock divided by 2
10Reserved
11Asynchronous clock (ADACK)
  • IPG时钟:The IPG clock. This is the default selection following reset.(IPG时钟。这是重置后的默认选择)
  • IPG时钟除以2:The IPG clock divided by two. For higher IPG clock rates, this allows a maximum divide by 16 of the IPG clock using the ADIV bits. (IPG时钟除以2。对于更高的IPG时钟速率,这允许最大使用ADIV位将IPG时钟除以16)
  • 异步时钟(ADACK):The asynchronous clock (ADACK). This clock is generated from a clock source within the ADC module. Conversions are possible using ADACK as the input clock source while the MCU is in stop mode.(异步时钟(ADACK)。该时钟从ADC模块内的时钟源生成。当MCU处于停止模式时,可使用ADACK作为输入时钟源进行转换)

5.Hardware Average Function

硬件平均函数核心信息提取

  1. 启用与基本功能:可通过设置AVGE=1启用硬件平均函数,对多次转换结果做硬件平均 。转换次数由AVGS[1:0]位决定,可选4、8、16、32次转换参与平均;进行硬件平均时,ADACT位会被置位 。
  2. 转换与平均流程:选定输入经采样、转换后,结果存入累加器;完成选定转换次数后,从累加器计算平均值;开启硬件平均时,单个转换完成不会置位COCON位 。
  3. 比较功能与中断关联:若比较功能禁用或评估为真,完成选定转换次数后,平均结果传输至ADC_Rn寄存器,置位COCON位;若对应ADC中断使能(AIENn=1),置位COCON位时会触发ADC中断 。

6.Automatic Compare Function

比较功能可配置检查结果与单个/范围比较值的关系,模式由ACFGT、ACREN及ADC_CV寄存器值确定,输入采样转换后用CV1和CV2,有六种比较模式 。

在这里插入图片描述
在这里插入图片描述

7.Calibration Function

要完成校准,用户需遵循的程序:

  1. 用实际操作值配置ADC_CFG以获最大精度
  2. 配置ADC_GC值及CAL位
  3. 检查ADC_GS中CALF位和ADC_GC中CAL位状态
  4. CAL位变“0”时,检查CALF状态和COCO[0]位状态

三.ADC重点寄存器介绍

1.Control register(ADCx_HC0)

在这里插入图片描述

2.Status register(ADCx_HS)

在这里插入图片描述

3.Data result register(ADCx_R0)

在这里插入图片描述

4.Configuration register(ADCx_CFG)

在这里插入图片描述

四. ADC采集电压

1.编程思路

  1. 将管脚设为GPIO功能模式
  2. 使能ADC模块时钟
  3. 设置ADC控制器,确定转换精度与时钟源
  4. 让ADC控制器进入自动校准流程
  5. 注册ADC中断
  6. 选择ADC通道并开启转换
  7. 在ADC中断处理函数中读取转换值

在这里插入图片描述

#include "imx6ull.h"

void adc_init(void)
{
   /*ADC GPIO*/
   IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05 &= ~(0xf << 0);
   IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05 |= (0x5 << 0);

   /*Enable*/
   CCM_CCGR1 |= (0x3 << 16);
   /*
      ADICLK [1:0] 01 IPG Clock divided by 2
      MODE   [3:2] 10  12-bit conversion
   */
   ADC1->CFG = (1 << 0) | (0x2 << 2);
  

}

int adc_auto_calibration(void)
{
    /*
      CAL [7] CAL begins the calibration 
    */
    ADC1->GC = (1 << 7);

    /*
     
    */
    while(ADC1->GC & (1 << 7))
    {
       if(ADC1->GS & (1<< 1))
       {
         uart_printf("adc\r\n");
         return -1;
       }
    }
   if(ADC1->HS & (1 << 0) == 0)
   {
      uart_printf("ADC calibration failed\r\n");
      return -1;
   }
   uart_printf("ADC succedd\r\n");
   return 1;
}

void adc_channel_start(int channel)
{
  ADC1->HC[0] = (channel << 0) | (1 << 7);
  return;
}

int adc_interrupt_handler(int id)
{
   uint32_t adc_mv;
   uint32_t adc_value;

   adc_value = ADC1->R[0] & 0xfff;
   adc_mv = (adc_value *3300) /4096;

   uart_printf("adc_value:%d,%dmv\r\n",adc_value,adc_mv);;

   start_adc_channel(5);

   gpt_delay_sec(1);
   return 0;
}

void adc_test(void)
{
   int ret;

   adc_init();

   ret = adc_auto_calibration();
   if(ret < 0)
   {
      return;
   }

   /*register adc interrupt*/
   request_irq(ADC1_IRQn, adc_interrupt_handler);
   enable_irq();

  adc_channel_start(5);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值