当前文档版本为 v1.2.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

Driver: ADC

1 功能概述

本文档的例程演示 ADC 外设 Driver 的基本用法,包括:

  1. 单次转换及多次转换功能,并演示从 ADC 采样值反推输入电压的方法

  2. ADC 采样芯片内部温度传感器,获取当前芯片温度,并演示与普通 ADC 采样功能共存的方法

  3. ADC DMA 功能

2 环境要求

  • PAN1080 EVB 一块 (需确保核心板 SoC 为校准后的芯片)

  • USB-TypeC 线一条(用于供电和查看串口打印 Log)

  • 万用表 一块

  • 硬件接线:

    • 使用USB线,将 PC USB 与 EVB USB-TypeC(USB->UART)相连

    • 根据 EVB 核心板芯片的封装不同,使用杜邦线将 EVB 底板上的:

      • TX0 与 P00 相连, RX0 与 P01 相连(若核心板芯片为 QFN32 或 LQFP64 封装)

      • TX0 与 P30 相连, RX0 与 P31 相连(若核心板板芯片为 QFN48 封装)

    • 使用跳线帽将 EVB 底板左上角的 ADC 与 P20 (ADC_Channel_6) 相连

  • PC 软件: 串口调试助手(UartAssist)或终端工具(SecureCRT),波特率 921600

3 编译和烧录

例程位置:

  1. zephyr\samples_panchip\drivers\adc (Zephyr ADC Sample)

  2. zephyr\samples_panchip\drivers\adc_hal_api (ADC HAL API Sample)

使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍

4 演示说明 (Zephyr ADC Sample)

  1. 使用万用表,测量 EVB 板 ADC 引脚的电压,调节可调电阻 R_ADJ 的阻值,将 ADC 引脚电压调整至 1.2v 以内 (如 800mV)

  2. 执行例程,串口打印如下 Log:

    Try to load HW calibration data.. DONE.
    - Chip Type         : 0x80
    - Chip CP Version   : None
    - Chip FT Version   : 7
    - Chip MAC Address  : D0000C06FB5D
    - Chip Flash UID    : 31373237304A65114330FFFFFFFFFFFF
    - Chip Flash Size   : 1024 KB
    *** Booting Zephyr OS build zephyr-v2.7.0  ***
    adc-vbg voltage: 1200 mV
    
    ---------------- Measure round 0 ----------------
    sample one time:
    adc sampling data: 0xa8f
    The ADC sample code is 2703
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 796 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2718
    adc sampling data: 2709
    adc sampling data: 2715
    adc sampling data: 2717
    adc sampling data: 2713
    adc sampling data: 2715
    adc sampling data: 2711
    adc sampling data: 2718
    adc sampling data: 2706
    adc sampling data: 2711
    The processed ADC sample code is 2713
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 799 mV
    The voltage value calculated with fitting formula2 is 800 mV
    
    ---------------- Measure round 1 ----------------
    sample one time:
    adc sampling data: 0xa8d
    The ADC sample code is 2701
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 795 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2710
    adc sampling data: 2679
    adc sampling data: 2710
    adc sampling data: 2717
    adc sampling data: 2707
    adc sampling data: 2715
    adc sampling data: 2710
    adc sampling data: 2706
    adc sampling data: 2707
    adc sampling data: 2714
    The processed ADC sample code is 2707
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 797 mV
    
    ---------------- Measure round 2 ----------------
    sample one time:
    adc sampling data: 0xa8f
    The ADC sample code is 2703
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 796 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2707
    adc sampling data: 2709
    adc sampling data: 2702
    adc sampling data: 2703
    adc sampling data: 2714
    adc sampling data: 2709
    adc sampling data: 2707
    adc sampling data: 2715
    adc sampling data: 2683
    adc sampling data: 2687
    The processed ADC sample code is 2703
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 796 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    ---------------- Measure round 3 ----------------
    sample one time:
    adc sampling data: 0xa8d
    The ADC sample code is 2701
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 795 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2687
    adc sampling data: 2703
    adc sampling data: 2716
    adc sampling data: 2709
    adc sampling data: 2707
    adc sampling data: 2715
    adc sampling data: 2717
    adc sampling data: 2710
    adc sampling data: 2679
    adc sampling data: 2712
    The processed ADC sample code is 2705
    The voltage value calculated with ideal formula is 792 mV
    The voltage value calculated with fitting formula is 796 mV
    The voltage value calculated with fitting formula2 is 797 mV
    
    ---------------- Measure round 4 ----------------
    sample one time:
    adc sampling data: 0xa9f
    The ADC sample code is 2719
    The voltage value calculated with ideal formula is 796 mV
    The voltage value calculated with fitting formula is 800 mV
    The voltage value calculated with fitting formula2 is 801 mV
    
    sample ten times:
    adc sampling data: 2713
    adc sampling data: 2709
    adc sampling data: 2711
    adc sampling data: 2715
    adc sampling data: 2707
    adc sampling data: 2710
    adc sampling data: 2710
    adc sampling data: 2719
    adc sampling data: 2711
    adc sampling data: 2704
    The processed ADC sample code is 2710
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 5 ----------------
    sample one time:
    adc sampling data: 0xa8f
    The ADC sample code is 2703
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 796 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2687
    adc sampling data: 2707
    adc sampling data: 2710
    adc sampling data: 2711
    adc sampling data: 2715
    adc sampling data: 2703
    adc sampling data: 2713
    adc sampling data: 2715
    adc sampling data: 2709
    adc sampling data: 2719
    The processed ADC sample code is 2708
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 798 mV
    
    ---------------- Measure round 6 ----------------
    sample one time:
    adc sampling data: 0xa93
    The ADC sample code is 2707
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 797 mV
    
    sample ten times:
    adc sampling data: 2709
    adc sampling data: 2707
    adc sampling data: 2710
    adc sampling data: 2709
    adc sampling data: 2719
    adc sampling data: 2709
    adc sampling data: 2711
    adc sampling data: 2715
    adc sampling data: 2709
    adc sampling data: 2706
    The processed ADC sample code is 2710
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 7 ----------------
    sample one time:
    adc sampling data: 0xa8e
    The ADC sample code is 2702
    The voltage value calculated with ideal formula is 791 mV
    The voltage value calculated with fitting formula is 795 mV
    The voltage value calculated with fitting formula2 is 796 mV
    
    sample ten times:
    adc sampling data: 2707
    adc sampling data: 2714
    adc sampling data: 2712
    adc sampling data: 2707
    adc sampling data: 2715
    adc sampling data: 2705
    adc sampling data: 2701
    adc sampling data: 2703
    adc sampling data: 2713
    adc sampling data: 2707
    The processed ADC sample code is 2708
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 798 mV
    
    ---------------- Measure round 8 ----------------
    sample one time:
    adc sampling data: 0xa8b
    The ADC sample code is 2699
    The voltage value calculated with ideal formula is 790 mV
    The voltage value calculated with fitting formula is 794 mV
    The voltage value calculated with fitting formula2 is 795 mV
    
    sample ten times:
    adc sampling data: 2707
    adc sampling data: 2714
    adc sampling data: 2711
    adc sampling data: 2706
    adc sampling data: 2712
    adc sampling data: 2714
    adc sampling data: 2706
    adc sampling data: 2713
    adc sampling data: 2708
    adc sampling data: 2708
    The processed ADC sample code is 2709
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 798 mV
    
    ---------------- Measure round 9 ----------------
    sample one time:
    adc sampling data: 0xa95
    The ADC sample code is 2709
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 798 mV
    
    sample ten times:
    adc sampling data: 2719
    adc sampling data: 2711
    adc sampling data: 2719
    adc sampling data: 2707
    adc sampling data: 2710
    adc sampling data: 2708
    adc sampling data: 2737
    adc sampling data: 2712
    adc sampling data: 2707
    adc sampling data: 2710
    The processed ADC sample code is 2714
    The voltage value calculated with ideal formula is 795 mV
    The voltage value calculated with fitting formula is 799 mV
    The voltage value calculated with fitting formula2 is 800 mV
    
    Done.
    

4.1 ADC 通道配置

配置 ADC 通道参数,主要有两个参数可调:

  1. adc_channel_cfg.reference: ADC 参考电压,有 2 个可选值:

    • ADC_REF_VDD_1_2: 参考电压为 ADC 内部 1.2v VBG

    • ADC_REF_VDD_1: 参考电压为 SoC VDD (VBAT, 通常为 3.3v)

  2. adc_channel_cfg.channel_id: ADC 采样通道,可选值为 0 ~ 7,分别表示通道 1 ~ 通道 7

static const struct adc_channel_cfg m_channel_cfg = {
   .gain             = ADC_GAIN_1,
   .reference        = ADC_REF_VDD_1_2,
   .acquisition_time = ADC_ACQ_TIME_DEFAULT,
   .channel_id       = ADC_CHANNEL_ID,
};

4.2 ADC 单次转换功能

  1. 配置adc序列信息并adc初始化。

    const struct adc_sequence sequence = {
        .channels = BIT(ADC_CHANNEL_ID),			/*通道选择*/
        .buffer = m_sample_buffer,			/*结果缓存buffer*/
        .buffer_size = sizeof(m_sample_buffer),		/*buffer大小,不能小于次数*/
        .resolution = ADC_RESOLUTION,			/*我们固定为12bit adc*/
    };
    
    const struct device *adc_dev = init_adc();		/*初始化adc模块*/
    
    if (!adc_dev) {
        return -ENODEV;
    }
    
  2. 读取adc转换值。

    ret = adc_read(adc_dev, &sequence);	/*读取adc转换的code*/
    if (ret != 0) {
        printk("adc_read() failed with code %d\n", ret);
    }
    printk("adc sampling data: 0x%x\n", m_sample_buffer[0]); /*打印获取code*/
    
  3. 软件分别通过理想公式和拟合公式推算 ADC 采样电压,并将其打印出来:

    voltage_with_ideal_formula = ADC_REF_VOLTAGE * adc_code / 4096;
    printk("The voltage value calculated with ideal formula is %d mV\n", voltage_with_ideal_formula);
    
    voltage_with_fitting_formula = ADC_OutputVoltage(ADC, adc_code) / 1000;
    printk("The voltage value calculated with fitting formula is %d mV\n", voltage_with_fitting_formula);
    
    if (otp.m.ft_version >= 7) {
       voltage_with_fitting_formula2 = ADC_OutputVoltageRefVbgPiecewise(ADC, adc_code) / 1000;
       printk("The voltage value calculated with fitting formula2 is %d mV\n\n", voltage_with_fitting_formula2);
    }
    

    上述 3 种计算方法中:

    • voltage_with_ideal_formula 为使用理想公式计算的结果

    • voltage_with_fitting_formula 为使用最小二乘法拟合的直线公式计算的结果

    • voltage_with_fitting_formula2 为使用分段拟合的直线公式计算的结果

    • 注1:一般来说,理想公式计算结果最不准确,最小二乘法拟合的直线公式与分段拟合的直线公式计算的结果更准确。

    • 注2:分段拟合公式仅可在 FT Version >= 7 的芯片上使用。

    • 注3:我们在 FT Version >= 11 的芯片上优化了最小二乘法拟合公式参数,请更新 ZDK 至 v1.0.2 或更高版本以确保最小二乘法公式计算结果的准确性。

  4. 观察计算结果是否与实际输入电压一致

4.3 ADC 多次转换功能

  1. 配置adc序列信息并adc初始化。

    const struct adc_sequence_options options = {
        .callback        = repeated_samplings_callback,		/*中断回调函数*/
        .extra_samplings = ADC_BUFFER_SIZE / 2,			/*转换次数*/
        .interval_us     = 0,					/*间隔时间*/
        .user_data       = user_data,				/*用户数据*/
    };
    
    const struct adc_sequence sequence = {
        .options     = &options,					/*增加附加序列信息*/
        .channels = BIT(ADC_CHANNEL_ID),				/*通道选择*/
        .buffer = m_sample_buffer,				/*结果缓存buffer*/
        .buffer_size = sizeof(m_sample_buffer),			/*buffer大小,不能小于次数*/
        .resolution = ADC_RESOLUTION,				/*我们固定为12bit adc*/
    };
    
    const struct device *adc_dev = init_adc();			/*初始化adc模块*/
    
    if (!adc_dev) {
        return -ENODEV;
    }
    
  2. 设置回调函数

    static enum adc_action repeated_samplings_callback(const struct device *dev,
                         const struct adc_sequence *sequence,
                         uint16_t sampling_index)
    {
       ++m_samplings_done;
       if (m_samplings_done == 1U) {
          /* After first sampling continue normally. */
          return ADC_ACTION_CONTINUE;
       } else {
          /*
           * The second sampling is repeated 9 times (the samples are
           * written in the same place), then the sequence is finished
           * prematurely.
           */
          if (m_samplings_done < ADC_BUFFER_SIZE / 2) {
                /*这里选择continue,code会依次存放在buffer中*/
                /*如果选择repeat,那么只会存放在buffer的固定第二个数据中*/
             return ADC_ACTION_CONTINUE;
          } else {
             convert_finish = true;
             return ADC_ACTION_FINISH;
          }
       }
    }
    
  3. 读取adc转换值并计算平均值。

    ret = adc_read(adc_dev, &sequence);	/*读取adc转换的code*/
    if (ret != 0) {
        printk("adc_read() failed with code %d\n", ret);
    }
    
    for (uint8_t i = 0; i < ADC_BUFFER_SIZE / 2; i++) {
        avg_code += m_sample_buffer[i];
        printk("adc sampling data: 0x%x\n", m_sample_buffer[i]);
    }
    
  4. 软件分别通过理想公式和拟合公式推算 ADC 采样电压,并将其打印出来:

    voltage_with_ideal_formula = ADC_REF_VOLTAGE * adc_code / 4096;
    printk("The voltage value calculated with ideal formula is %d mV\n", voltage_with_ideal_formula);
    
    voltage_with_fitting_formula = ADC_OutputVoltage(ADC, adc_code) / 1000;
    printk("The voltage value calculated with fitting formula is %d mV\n", voltage_with_fitting_formula);
    
    if (otp.m.ft_version >= 7) {
       voltage_with_fitting_formula2 = ADC_OutputVoltageRefVbgPiecewise(ADC, adc_code) / 1000;
       printk("The voltage value calculated with fitting formula2 is %d mV\n\n", voltage_with_fitting_formula2);
    }
    

    上述 3 种计算方法中:

    • voltage_with_ideal_formula 为使用理想公式计算的结果

    • voltage_with_fitting_formula 为使用最小二乘法拟合的直线公式计算的结果

    • voltage_with_fitting_formula2 为使用分段拟合的直线公式计算的结果

    • 注1:一般来说,理想公式计算结果最不准确,最小二乘法拟合的直线公式与分段拟合的直线公式计算的结果更准确。

    • 注2:分段拟合公式仅可在 FT Version >= 7 的芯片上使用。

    • 注3:我们在 FT Version >= 11 的芯片上优化了最小二乘法拟合公式参数,请更新 ZDK 至 v1.0.2 或更高版本以确保最小二乘法公式计算结果的准确性。

  5. 观察计算结果是否与实际输入电压一致

4.4 ADC 转换结果说明

本次演示 ADC 实际输入电压为 800 mV,程序进行 10 轮次的循环 ADC 测试,由前面的串口打印 Log,选取任意一轮的结果(如最后一轮):

  • 单次 ADC 采样:

    • 使用理想公式计算的 ADC 输入电压为 793 mV

    • 使用最小二乘法拟合的直线公式计算的 ADC 输入电压为 797 mV

    • 使用分段拟合的直线公式计算的 ADC 输入电压为 798 mV

  • 多次 ADC 采样(10次)取平均:

    • 使用理想公式计算的 ADC 输入电压为 795 mV

    • 使用最小二乘法拟合的直线公式计算的 ADC 输入电压为 799 mV

    • 使用分段拟合的直线公式计算的 ADC 输入电压为 800 mV

由以上结果可见:

  1. ADC 单次采样,误差可能较大,多次采样取平均后误差有明显改善

  2. 使用理想公式计算结果误差较大,使用拟合公式后结果有明显改善

5 演示说明 (ADC HAL API Sample)

  1. 使用万用表,测量 EVB 板 ADC 引脚的电压,调节可调电阻 R_ADJ 的阻值,将 ADC 引脚电压调整至 1.2v 以内 (如 800mV)

  2. 执行例程,串口打印如下 Log:

    Try to load HW calibration data.. DONE.
    - Chip Type         : 0x80
    - Chip CP Version   : None
    - Chip FT Version   : 7
    - Chip MAC Address  : D0000C06FB5D
    - Chip Flash UID    : 31373237304A65114330FFFFFFFFFFFF
    - Chip Flash Size   : 1024 KB
    *** Booting Zephyr OS build zephyr-v2.7.0  ***
    adc-vbg voltage: 1200 mV
    ---------------- Measure round 0 ----------------
    ADC sampling data:
    2687 2687 2703 2708 2708 2708 2708 2709 2710 2710
    2713 2713 2713 2713 2714 2715 2716 2716 2717 2717
    The processed ADC sample code is 2713
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 799 mV
    The voltage value calculated with fitting formula2 is 800 mV
    
    ---------------- Measure round 1 ----------------
    ADC sampling data:
    2687 2687 2703 2705 2707 2708 2708 2709 2709 2709
    2711 2711 2711 2712 2714 2714 2715 2715 2715 2719
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 2 ----------------
    ADC sampling data:
    2687 2700 2706 2707 2707 2709 2709 2709 2710 2711
    2711 2713 2713 2714 2714 2718 2718 2719 2719 2727
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 3 ----------------
    ADC sampling data:
    2685 2687 2687 2701 2701 2703 2706 2707 2707 2709
    2709 2711 2711 2714 2715 2715 2717 2717 2728 2732
    The processed ADC sample code is 2709
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 797 mV
    The voltage value calculated with fitting formula2 is 798 mV
    
    ---------------- Measure round 4 ----------------
    ADC sampling data:
    2687 2703 2703 2705 2708 2709 2710 2711 2711 2711
    2711 2714 2714 2715 2715 2715 2715 2719 2719 2723
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 5 ----------------
    ADC sampling data:
    2687 2698 2699 2700 2701 2706 2707 2708 2709 2710
    2711 2711 2711 2714 2714 2715 2717 2727 2731 2731
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 6 ----------------
    ADC sampling data:
    2699 2701 2702 2703 2703 2705 2708 2708 2710 2710
    2711 2711 2711 2714 2715 2715 2717 2719 2719 2719
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 7 ----------------
    ADC sampling data:
    2686 2701 2701 2702 2703 2704 2708 2710 2710 2711
    2711 2711 2711 2711 2714 2717 2718 2719 2719 2726
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 8 ----------------
    ADC sampling data:
    2687 2704 2705 2706 2707 2707 2707 2708 2708 2709
    2711 2711 2711 2711 2711 2712 2712 2714 2715 2719
    The processed ADC sample code is 2711
    The voltage value calculated with ideal formula is 794 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    ---------------- Measure round 9 ----------------
    ADC sampling data:
    2687 2699 2699 2701 2706 2707 2707 2708 2710 2710
    2710 2711 2711 2711 2713 2716 2719 2728 2731 2751
    The processed ADC sample code is 2710
    The voltage value calculated with ideal formula is 793 mV
    The voltage value calculated with fitting formula is 798 mV
    The voltage value calculated with fitting formula2 is 799 mV
    
    Done.
    
  3. 本次演示 ADC 实际输入电压为 800 mV,由上述串口打印 Log 可知:

    • 使用 ADC 采样 10 轮,每轮采样 20 次,取采样值的中位数

      • 使用理想公式计算的 ADC 输入电压为 794 mV 左右

      • 使用最小二乘法拟合的直线公式计算的 ADC 输入电压为 798 mV 左右

      • 使用分段拟合的直线公式计算的 ADC 输入电压为 799 mV 左右

注:本例程的 ADC 软件操作流程请参考工程代码中的相关注释。

6 开发说明

  1. 为使用 ADC 计算电压准确,请确保当前的芯片为校准后的芯片,且芯片的 FT 版本号最好大于或等于 7,可根据芯片上电后的串口 Log 判断:

    Try to load HW calibration data.. DONE.
    - Chip Type         : 0x80
    - Chip CP Version   : None
    - Chip FT Version   : 7
    

    上述 Log 即表示当前芯片为校准后的芯片,FT Version 为 7;另外,若芯片的 FT Version 小于 7,ADC也是可以正常使用的,只是计算准确度可能会稍微差一些。

  2. 当使用 Zephyr ADC Driver 时,需要在prj.conf文件中添加 “CONFIG_ADC=y” 以启用 ADC 模块;而当直接使用 ADC HAL Driver时,无需执行此步骤

  3. ADC 初始化代码执行完成后,应当延时一段时间后再执行采样操作,以确保 ADC 硬件模块处于稳定状态

  4. 应用中可能会需要使用分压电阻将待测电压分压至 ADC 采样电压范围内(0 ~ 1.2v),而为了减少电路漏电,可能会需要使用较大的电阻,但是,过大的分压电阻可能导致 ADC 采样抖动较大,解决的方法一方面可以在 ADC 输入引脚处增加电容,另一方面要采用稍小一些的分压电阻,这里我们推荐总电阻不要超过 1MΩ,例如使用 ADC 测量典型的锂电池电压(4.2v),分压电阻可采用 1/4 分压的方式,可分别使用一个 250KΩ 和 一个 750KΩ 的电阻,将电池电压分压至 1.1v 以内, 由分压电路产生的漏电流为 4.2v / 1MΩ = 4.2 uA

7 使用 ADC 采样芯片内部温度传感器

PAN1080 SoC 内部有一个温度传感器,其输出电压信号直接接入 ADC 模块的内部特殊通道,这样我们可以通过 ADC 去获取当前芯片的温度。

PAN1080 ZDK 已经适配了通过 ADC 获取芯片温度的流程,但是默认没有使能。关于此功能的详细介绍,请参考 SoC App 开发指南 文档中8 芯片温度检测小节的相关介绍。

开启芯片温度检测功能后,系统会定时通过 ADC 去采样温度传感器,然而,如果 App 中也用到 ADC 的话,就可能产生冲突,继而导致 ADC 采样错误。

SDK 中专门提供了额外的保护接口用于避免此种情况:

/* soc.h */
extern void soc_sync_adc_temp_begin(void);
extern void soc_sync_adc_temp_end(void);

我们只需在 App 使用 ADC 相关接口前,调用 soc_sync_adc_temp_begin(),在使用 ADC 相关接口后,调用一次 soc_sync_adc_temp_end,即可避免与温度检测流程冲突。

ADC 例程和 ADC HAL API 例程的 prj_sync_with_temp.conf 配置文件,即演示了 ADC 与 温度检测共存情况下,App 使用 ADC 的方法,用户可根据此 config 文件中的配置,对照代码查阅具体的程序写法。

8 ADC DMA 配置说明

ADC DMA 例程位于:zephyr\samples_panchip\drivers\adc_dma

  • CONFIG_ADC_PANCHIP_DMA=y

    该配置使能 ADC DMA feature。

  • CONFIG_ADC_PANCHIP_DMA_TIMEOUT=1000

    该配置为ADC DMA读取一次时的超时时间,单位为ms。

  • ADC配置注意点 需要确认extra_samplings+1等于实际采样的次数,否则采样次数不对会导致不会触发DMA中断,从而导致ADC DMA read超时。

9 RAM/Flash资源使用情况

Memory region         Used Size  Region Size  %age Used
FLASH:       30148 B       384 KB      7.67%
SRAM:        6032 B        64 KB      9.20%

10 Adc全功能测试case

Adc全功能测试case及测试流程请参考文档:03_MCU/mcu_samples_doc/PAN1080 ADC例程说明.pdf