12下一页
返回列表 发新帖我要提问本帖赏金: 10.00元(功能说明)

[综合信息] 华大HC32F460之ADC

[复制链接]
7470|34
 楼主| binoo7 发表于 2021-2-13 10:43 | 显示全部楼层 |阅读模式
#申请原创#@小跑堂 @小跑堂  @小跑堂
今天是大年初二,我们来讲讲华大HC32F460的ADC功能
首先,片上搭载的是12位的ADC,一共有17个通道,但是,16个外部模拟输入通道和 1个内部基准电压/8bitDAC 输出的检测通道。
MCU内部集成 ADC1和 ADC2两个 ADC 模块,挂载于AHB-APB(APB3)总线


7465960272eba75185.png
有一点需要特别说明:ADC外设的时钟源是PCLK4,
在进行ADC转换的时候有一个 A/D转换时钟 ADCLK,这个时钟的时钟源是什么呢?看一本书真的看不懂,我找了几个地方来一一印证一下,首先是系统时钟框图
97577602732863c59e.png
从上图可以看到ADCLK 的时钟源挺多的,再次看代码
19415602732da3d3a1.png
代码里列出来的是三种时钟PCLK/MPLLQ/UPLLR
具体实现的过程是这样的
  1. /**
  2. *******************************************************************************
  3. ** \brief  ADC clock configuration.
  4. **
  5. ** \note
再看用户手册中的介绍,有没有很迷茫?
55669602734ffb314b.png
好啦,在这里总结一下这个时钟源到底是怎么选的根本就没有配置相关的寄存器,而是通过配置时钟后,不同的时钟比例来对应不同的时钟源选择,迷茫不?

可能是我见识比较浅,在用户手册里,还发现了ADC看门狗这个功能,他是这么说的
ADC 模块还搭载模拟看门狗功能,对任意指定通道的转换结果进行监视,检测是否超出用户设定的阀值。

具体是什么样的,我们稍后再说。
时钟配置好以后就是配置通道了
2100760273871607d5.png
ADC 模块有多个通道,可配置为两个序列:
序列 A,序列 B 进行转换。
序列 A 和 B配有独立的通道选择寄存器 ADC_CHSELRA,ADC_CHSELRB。
寄存器每位代表一个通道,如 bit0 位写 1 表示转换 CH0,写 0 表示不转换 CH0。
1175960273916ee918.png
两个序列可独立选择任意1 个或多个通道进行转换。
例如:ADC_CHSELRA 设置为 0x0055,ADCHSELRB 设置为 0x0002,则序列 A 的触发条件发生时,将依次转换 CH0,CH2,CH4 和 CH6 这 4个通道。序列 B 的触发条件发生时,将转换 CH1 这一个通道。
对于内部模拟通道,有 3 个可选的模拟量:8bitDAC_1 输出、8bitDAC_2 输出、内部基准电压(约 1.1V)。在选择内部模拟通道作为 ADC 转换对象时,需要先 设 置 内 部 通 道 选 择 寄 存 器 , 再 设 置 ADC 的 通 道 选 择 寄 存 器ADC_CHSELRA/ADC_CHSELRB 选择内部通道,才可以启动 ADC 转换。

下面是触发源选择,序列 A,序列 B 独立选择触发源。
可选择的触发源包括外部端口 ADTRGx,内部事件IN_TRGx0, IN_TRGx1。
其中,端口 ADTRGx 下降沿输入有效。IN_TRGx0,IN_TRGx1由寄存器 ADC_ITRGSELR0,1 设置,可以选择芯片内部丰富的事件源。此外,写寄存器 ADC_STR 可生成软件触发信号,软件触发只能在 ADC 处于待机状态时使用,并且只适用于序列 A。

619160273a1938111.png
下面就是通道的扫描模式,分成三种 单次扫描 多次扫描和双通道扫描
A/D 控制寄存器 ADC_CR0.MS[1:0]设为 00b 选择序列 A 单次扫描模式。
该模式下,当寄存器 ADC_TRGSR 选择的序列 A 启动条件发生,或者 ADC_STR.STRT位写 1 软件触发,ADC 启动,对序列 A 通道选择寄存器 ADC_CHSELRA 中选择的所有通道依次进行采样和转换,转换结果存入相应的数据寄存器 ADC_DR 中。ADC 转换过程中 ADC_STR.STRT 保持为 1,当所有通道转换结束后自动清 0,ADC 进入转换待机状态,等待下次触发条件的发生。
当所有通道转换结束时,序列 A 转换结束标志位 ADC_ISR.EOCAF 置 1,并产生序列A 转换结束事件 ADC_EOCA,可以用此事件启动 DMA。若 ADC_ICR.EOCAIEN 为1,中断许可的状态下,同时还产生序列 A 转换结束中断请求。

4428360273ac030997.png
序列 A 单次扫描模式的软件流程:
1. 确认 ADC_STR.STRT 为 0,ADC 处于转换待机状态。
2. A/D 控制寄存器 ADC_CR0.MS[1:0]设为 00b 选择序列 A 单次扫描模式。
3. 设置序列 A 通道选择寄存器 ADC_CHSELRA。
4. 设置采样时间寄存器 ADC_SSTR。

5. ADC_STR.STRT 写 1 软件触发序列 A,或者设置寄存器 ADC_TRGSR 选择序列 A触发条件。
6. 查询序列 A 转换结束标志位 EOCAF。
7. 读取各通道数据寄存器 ADC_DR。
8. 写 0 清除 EOCAF 标志位,为下次转换准备。
上面的步骤 6~8 的 CPU 查询方式也可以替换成中断方式,利用 ADC_EOCA 中断来处理转换数据。或者利用 ADC_EOCA 事件启动 DMA 读取数据。

A/D 控制寄存器 ADCR0.MS[1:0]设为 01b 选择序列 A 连续扫描模式。
序列 A 连续扫描模式与序列 A 单次扫描模式动作类似,不同点在于,连续模式在所用通道转换结束后不是进入转换待机状态,而是重新开始转换序列 A。STRT 位也不会自动清 0。
当需要停止连续扫描时,对 STRT 位写 0,并读 STRT 确认为 0 以判断 ADC 进入转换待机状态。

序列 A 连续扫描模式的软件流程:
1. 确认 ADC_STR.STRT 为 0,ADC 处于转换待机状态。
2. A/D 控制寄存器 ADC_CR0.MS[1:0]设为 01b 选择序列 A 连续扫描模式。
3. 设置序列 A 通道选择寄存器 ADC_CHSELRA。
4. 设置采样时间寄存器 ADC_SSTR。
5. ADC_STR.STRT 写 1 软件触发序列 A,或者设置寄存器 ADC_TRGSR 选择序列 A触发条件。

6. 查询序列 A 转换结束标志位 EOCAF。
7. 读取各通道数据寄存器 ADC_DR。
8. 写 0 清楚 EOCAF 标志位,为下次转换准备。
9. 不需要继续转换时,对 STRT 位写 0,并读 STRT 确认为 0 以判断 ADC 进入转换待机状态。
上面的步骤 6~8 的查询方式也可以替换成中断方式,利用 ADC_EOCA 中断来处理转换数据。或者利用 ADC_EOCA 事件启动 DMA 读取数据。
注意:
– 由于是连续转换,每次扫描的间隔比较短,特别是只选择 1 个通道转换时。推荐使用 ADC_EOCA 事件启动 DMA 读取数据,避免查询方式下处理不及时导致数据丢失。

A/D 控制寄存器 ADC_CR0.MS[1:0]设为 10b 或 11b 选择双序列扫描模式,即序列 A 与序列 B 都可由各自选择的触发条件来启动扫描。
当 MS[1:0]=10b 时,序列 A 和 B 等效于两个独立的单次扫描序列。MS[1:0]=11b 序列A 为连续扫描模式,B 为单次扫描模式。
序列 A 由 ADC_TRGSR.TRGSELA[2:0]选择触发源,由 ADC_CHSELRA 选择转换的通道。序列 B 由 ADC_TRGSR.TRGSELB[2:0]选择触发源,由 ADC_CHSELRB 选择转换的通道。
当序列 A 全部通道转换结束时,序列 A 转换结束标志位 ADC_ISR.EOCAF 置 1,并产生序列 A 转换结束事件 ADC_EOCA,若 ADC_ISCR.EOCAIEN 为 1,中断许可的状态下,同时产生序列 A 转换结束中断请求。
当序列 B 全部通道转换结束时,序列 B 转换结束标志位 ADC_ISR.EOCBF 置 1,并产生序列 B 转换结束事件 ADC_EOCB,若ADC_ISCR.EOCBIEN 为 1,中断许可的状态下,同时产生序列 B 转换结束中断请求。
双序列扫描模式下,当序列 A 与序列 B 发生竞争时,序列 B 将会被优先处理,即序列B 优先级高于序列 A。

8576360273bb59c9bc.png
1711460273bc56f27f.png
7968560273bdb22b55.png
还有很多功能这里就不做一一讲解了,下面看一下官方给的代码我们来分析一下流程
  1. /*******************************************************************************
  2. * Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software is owned and published by:
  5. * Huada Semiconductor Co., Ltd. ("HDSC").
  6. *
  7. * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
  8. * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
  9. *
  10. * This software contains source code for use with HDSC
  11. * components. This software is licensed by HDSC to be adapted only
  12. * for use in systems utilizing HDSC components. HDSC shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. HDSC is providing this software "AS IS" and will
  15. * not be responsible for issues arising from incorrect user implementation
  16. * of the software.
  17. *
  18. * Disclaimer:
  19. * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
  20. * REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
  21. * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
  22. * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
  23. * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
  24. * WARRANTY OF NONINFRINGEMENT.
  25. * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
  26. * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
  27. * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
  28. * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
  29. * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
  31. * SAVINGS OR PROFITS,
  32. * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
  34. * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
  35. * FROM, THE SOFTWARE.
  36. *
  37. * This software may be replicated in part or whole for the licensed use,
  38. * with the restriction that this Disclaimer and Copyright notice must be
  39. * included with each copy of this software, whether used in part or whole,
  40. * at all times.
  41. */
  42. /******************************************************************************/
  43. /** \file main.c
  44. **
  45. ** \brief ADC sample
  46. **
  47. **   - 2018-11-30  1.0  Wuze First version for Device Driver Library of
  48. **     ADC
  49. **
  50. ******************************************************************************/

  51. /*******************************************************************************
  52. * Include files
  53. ******************************************************************************/
  54. #include "hc32_ddl.h"

  55. /*******************************************************************************
  56. * Local type definitions ('typedef')
  57. ******************************************************************************/

  58. /*******************************************************************************
  59. * Local pre-processor symbols/macros ('#define')
  60. ******************************************************************************/
  61. /*
  62. * If you remap the mapping between the channel and the pin with the function
  63. * ADC_ChannleRemap, define ADC_CH_REMAP as non-zero, otherwise define as 0.
  64. */
  65. #define ADC_CH_REMAP                (0u)

  66. /* ADC clock selection definition. */
  67. #define ADC_CLK_PCLK                (1u)
  68. #define ADC_CLK_MPLLQ               (2u)
  69. #define ADC_CLK_UPLLR               (3u)

  70. /* Select PCLK as ADC clock. */
  71. #define ADC_CLK                     (ADC_CLK_UPLLR)

  72. /* ADC1 channel definition for this example. */
  73. #define ADC1_SA_NORMAL_CHANNEL      (ADC1_CH0 | ADC1_CH10)
  74. #define ADC1_SA_AVG_CHANNEL         (ADC1_CH12 | ADC1_CH13)
  75. #define ADC1_SA_CHANNEL             (ADC1_SA_NORMAL_CHANNEL | ADC1_SA_AVG_CHANNEL)
  76. #define ADC1_SA_CHANNEL_COUNT       (4u)

  77. #define ADC1_AVG_CHANNEL            (ADC1_SA_AVG_CHANNEL)
  78. #define ADC1_CHANNEL                (ADC1_SA_CHANNEL)

  79. /* ADC1 channel sampling time.     ADC1_CH0  ADC1_CH10  ADC1_CH12   ADC1_CH13 */
  80. #define ADC1_SA_CHANNEL_SAMPLE_TIME { 0x30,     0x80,      0x50,      0x60 }

  81. /* ADC2 channel definition for this example. */
  82. #define ADC2_SA_NORMAL_CHANNEL      (ADC2_CH0 | ADC2_CH2)
  83. #define ADC2_SA_AVG_CHANNEL         (ADC2_CH5)
  84. #define ADC2_SA_CHANNEL             (ADC2_SA_NORMAL_CHANNEL | ADC2_SA_AVG_CHANNEL)
  85. #define ADC2_SA_CHANNEL_COUNT       (3u)

  86. #define ADC2_AVG_CHANNEL            (ADC2_SA_AVG_CHANNEL)
  87. #define ADC2_CHANNEL                (ADC2_SA_CHANNEL)

  88. /* ADC2 channel sampling time.      ADC2_CH0  ADC2_CH2  ADC2_CH5  */
  89. #define ADC2_SA_CHANNEL_SAMPLE_TIME { 0x60,     0x50,     0x40 }

  90. /* ADC resolution definitions. */
  91. #define ADC_RESOLUTION_8BIT         (8u)
  92. #define ADC_RESOLUTION_10BIT        (10u)
  93. #define ADC_RESOLUTION_12BIT        (12u)

  94. #define ADC1_RESOLUTION             (ADC_RESOLUTION_12BIT)
  95. #define ADC2_RESOLUTION             (ADC_RESOLUTION_10BIT)

  96. /* Scan mode definitions. */
  97. #define ADC1_SCAN_MODE              (AdcMode_SAOnce)
  98. #define ADC2_SCAN_MODE              (AdcMode_SAContinuous)

  99. /* ADC reference voltage. The voltage of pin VREFH. */
  100. #define ADC_VREF                    (3.288f)

  101. /* ADC accuracy. */
  102. #define ADC1_ACCURACY               (1ul << ADC1_RESOLUTION)

  103. /* ADC2 continuous conversion times. */
  104. #define ADC2_CONTINUOUS_TIMES       (3u)

  105. /* Timeout value definitions. */
  106. #define TIMEOUT_MS                  (10u)

  107. /*******************************************************************************
  108. * Global variable definitions (declared in header file with 'extern')
  109. ******************************************************************************/

  110. /*******************************************************************************
  111. * Local function prototypes ('static')
  112. ******************************************************************************/
  113. static void SystemClockConfig(void);

  114. static void AdcConfig(void);
  115. static void AdcClockConfig(void);
  116. static void AdcInitConfig(void);
  117. static void AdcChannelConfig(void);

  118. static void AdcSetChannelPinMode(const M4_ADC_TypeDef *ADCx,
  119.                                  uint32_t u32Channel,
  120.                                  en_pin_mode_t enMode);
  121. static void AdcSetPinMode(uint8_t u8AdcPin, en_pin_mode_t enMode);

  122. /*******************************************************************************
  123. * Local variable definitions ('static')
  124. ******************************************************************************/
  125. static uint16_t m_au16Adc1Value[ADC1_CH_COUNT];
  126. static uint16_t m_au16Adc2Value[ADC2_CH_COUNT];

  127. static stc_clk_sysclk_cfg_t m_stcSysclkCfg =
  128. {
  129.     /* Default system clock division. */
  130.     .enHclkDiv  = ClkSysclkDiv1,  // 168MHz
  131.     .enExclkDiv = ClkSysclkDiv2,  // 84MHz
  132.     .enPclk0Div = ClkSysclkDiv1,  // 168MHz
  133.     .enPclk1Div = ClkSysclkDiv2,  // 84MHz
  134.     .enPclk2Div = ClkSysclkDiv4,  // 42MHz
  135.     .enPclk3Div = ClkSysclkDiv4,  // 42MHz
  136.     .enPclk4Div = ClkSysclkDiv2,  // 84MHz
  137. };

  138. /*******************************************************************************
  139. * Function implementation - global ('extern') and local ('static')
  140. ******************************************************************************/
  141. /**
  142. *******************************************************************************
  143. ** \brief  Main function.
  144. **
  145. ** \param  None.
  146. **
  147. ** \retval int32_t return value, if needed.
  148. **
  149. ******************************************************************************/
  150. int32_t main(void)
  151. {
  152.     uint8_t u8Count;

  153.     /* Configuring a new system clock if you need. */
  154.     SystemClockConfig();

  155.     /* Config ADC. */
  156.     AdcConfig();

  157.     /* Config UART for printing. Baud rate 115200. */
  158.     Ddl_UartInit();

  159.     /***************** Configuration end, application start **************/

  160.     /* ADC1 sequence A single scan. */
  161.     /* Start ADC1, wait ADC1 scan converting done, read ADC1 data. */
  162.     ADC_PollingSa(M4_ADC1, m_au16Adc1Value, ADC1_CH_COUNT, TIMEOUT_MS);

  163.     /* ADC2 sequence A continuous scan. */
  164.     u8Count = 0u;
  165.     ADC_StartConvert(M4_ADC2);
  166.     while (u8Count < ADC2_CONTINUOUS_TIMES)
  167.     {
  168.         if (Set == ADC_GetEocFlag(M4_ADC2, ADC_SEQ_A))
  169.         {
  170.             ADC_GetChData(M4_ADC2, ADC2_SA_CHANNEL, m_au16Adc2Value, ADC2_SA_CHANNEL_COUNT);
  171.             ADC_ClrEocFlag(M4_ADC2, ADC_SEQ_A);
  172.             u8Count++;
  173.             // TODO: USE THE m_au16Adc2Value.
  174.         }
  175.     }

  176.     /*
  177.      *  DO NOT forget to stop ADC when your mode is
  178.      *  AdcMode_SAContinuous or AdcMode_SAContinuousSBOnce
  179.      *  unless you need.
  180.      */
  181.     ADC_StopConvert(M4_ADC2);

  182.     while (1u)
  183.     {
  184.         ADC_PollingSa(M4_ADC1, m_au16Adc1Value, ADC1_CH_COUNT, TIMEOUT_MS);
  185.         /* ADC1 channel 10 maps pin ADC12_IN10 by default. */
  186.         printf("\nADC12_IN10 value %d.", m_au16Adc1Value[10u]);
  187.         printf("\nADC12_IN10 voltage is %.4fV.",
  188.             ((float)m_au16Adc1Value[10u] * ADC_VREF) / (float)ADC1_ACCURACY);

  189.         /* Main loop cycle is 500ms. */
  190.         Ddl_Delay1ms(500u);
  191.     }
  192. }

  193. /*******************************************************************************
  194. * Local function prototypes ('static')
  195. ******************************************************************************/
  196. /**
  197. *******************************************************************************
  198. ** \brief  Configuring a new system clock.
  199. **         System clock frequency: 168MHz.
  200. **         System clock source:    MPLL.
  201. **         MPLL clock source:      XTAL(8MHz).
  202. **
  203. ******************************************************************************/
  204. static void SystemClockConfig(void)
  205. {
  206.     stc_clk_xtal_cfg_t stcXtalCfg;
  207.     stc_clk_mpll_cfg_t stcMpllCfg;
  208.     stc_sram_config_t  stcSramConfig;

  209.     MEM_ZERO_STRUCT(stcXtalCfg);
  210.     MEM_ZERO_STRUCT(stcMpllCfg);

  211.     /* Set bus clock division first. */
  212.     CLK_SysClkConfig(&m_stcSysclkCfg);

  213.     /* Switch system clock source to MPLL. */
  214.     /* Use XTAL as MPLL source. */
  215.     stcXtalCfg.enFastStartup = Enable;
  216.     stcXtalCfg.enMode = ClkXtalModeOsc;
  217.     stcXtalCfg.enDrv  = ClkXtalLowDrv;
  218.     CLK_XtalConfig(&stcXtalCfg);
  219.     CLK_XtalCmd(Enable);

  220.     /* Set MPLL out 168MHz. */
  221.     stcMpllCfg.pllmDiv = 1u;
  222.     /* sysclk = 8M / pllmDiv * plln / PllpDiv */
  223.     stcMpllCfg.plln    = 42u;
  224.     stcMpllCfg.PllpDiv = 2u;
  225.     stcMpllCfg.PllqDiv = 16u;
  226.     stcMpllCfg.PllrDiv = 16u;
  227.     CLK_SetPllSource(ClkPllSrcXTAL);
  228.     CLK_MpllConfig(&stcMpllCfg);

  229.     /* Flash read wait cycle setting. */
  230.     EFM_Unlock();
  231.     EFM_SetLatency(EFM_LATENCY_4);
  232.     EFM_Lock();

  233.     /* If the system clock frequency is higher than 100MHz and SRAM1, SRAM2, SRAM3 or Ret_SRAM is used,
  234.        the wait cycle must be set. */
  235.     stcSramConfig.u8SramIdx     = Sram12Idx | Sram3Idx | SramRetIdx;
  236.     stcSramConfig.enSramRC      = SramCycle2;
  237.     stcSramConfig.enSramWC      = SramCycle2;
  238.     stcSramConfig.enSramEccMode = EccMode0;
  239.     stcSramConfig.enSramEccOp   = SramNmi;
  240.     stcSramConfig.enSramPyOp    = SramNmi;
  241.     SRAM_Init(&stcSramConfig);

  242.     CLK_MpllCmd(Enable);

  243.     /* Wait MPLL ready. */
  244.     while(Set != CLK_GetFlagStatus(ClkFlagMPLLRdy))
  245.     {
  246.         ;
  247.     }

  248.     /* Set system clock source. */
  249.     CLK_SetSysClkSource(CLKSysSrcMPLL);
  250. }

  251. /**
  252. *******************************************************************************
  253. ** \brief  ADC configuration, including clock configuration, initial configuration
  254. **         and channel configuration.
  255. **
  256. ** \param  None.
  257. **
  258. ** \retval None.
  259. **
  260. ******************************************************************************/
  261. static void AdcConfig(void)
  262. {
  263.     AdcClockConfig();
  264.     AdcInitConfig();
  265.     AdcChannelConfig();
  266. }

  267. /**
  268. *******************************************************************************
  269. ** \brief  ADC clock configuration.
  270. **
  271. ** \note   1) ADCLK max frequency is 60MHz.
  272. **         2) If PCLK2 and PCLK4 are selected as the ADC clock,
  273. **            the following conditions must be met:
  274. **            a. ADCLK(PCLK2) max 60MHz;
  275. **            b. PCLK4 : ADCLK = 1:1, 2:1, 4:1, 8:1, 1:2, 1:4
  276. **
  277. ******************************************************************************/
  278. static void AdcClockConfig(void)
  279. {
  280. #if (ADC_CLK == ADC_CLK_PCLK)
  281.     /* Set bus clock division, depends on the system clock frequency. */
  282.     m_stcSysclkCfg.enPclk2Div = ClkSysclkDiv64;
  283.     m_stcSysclkCfg.enPclk4Div = ClkSysclkDiv16;

  284.     CLK_SysClkConfig(&m_stcSysclkCfg);
  285.     CLK_SetPeriClkSource(ClkPeriSrcPclk);

  286. #elif (ADC_CLK == ADC_CLK_MPLLQ)
  287.     stc_clk_xtal_cfg_t stcXtalCfg;
  288.     stc_clk_mpll_cfg_t stcMpllCfg;

  289.     if (CLKSysSrcMPLL == CLK_GetSysClkSource())
  290.     {
  291.         /*
  292.          * Configure MPLLQ(same as MPLLP and MPLLR) when you
  293.          * configure MPLL as the system clock.
  294.          */
  295.     }
  296.     else
  297.     {
  298.         /* Use XTAL as MPLL source. */
  299.         stcXtalCfg.enFastStartup = Enable;
  300.         stcXtalCfg.enMode = ClkXtalModeOsc;
  301.         stcXtalCfg.enDrv  = ClkXtalLowDrv;
  302.         CLK_XtalConfig(&stcXtalCfg);
  303.         CLK_XtalCmd(Enable);

  304.         /* Set MPLL out 240MHz. */
  305.         stcMpllCfg.pllmDiv = 1u;
  306.         /* mpll = 8M / pllmDiv * plln */
  307.         stcMpllCfg.plln    = 30u;
  308.         stcMpllCfg.PllpDiv = 16u;
  309.         stcMpllCfg.PllqDiv = 16u;
  310.         stcMpllCfg.PllrDiv = 16u;
  311.         CLK_SetPllSource(ClkPllSrcXTAL);
  312.         CLK_MpllConfig(&stcMpllCfg);
  313.         CLK_MpllCmd(Enable);
  314.     }
  315.     CLK_SetPeriClkSource(ClkPeriSrcMpllp);

  316. #elif (ADC_CLK == ADC_CLK_UPLLR)
  317.     stc_clk_xtal_cfg_t stcXtalCfg;
  318.     stc_clk_upll_cfg_t stcUpllCfg;

  319.     MEM_ZERO_STRUCT(stcXtalCfg);
  320.     MEM_ZERO_STRUCT(stcUpllCfg);

  321.     /* Use XTAL as UPLL source. */
  322.     stcXtalCfg.enFastStartup = Enable;
  323.     stcXtalCfg.enMode = ClkXtalModeOsc;
  324.     stcXtalCfg.enDrv = ClkXtalLowDrv;
  325.     CLK_XtalConfig(&stcXtalCfg);
  326.     CLK_XtalCmd(Enable);

  327.     /* Set UPLL out 240MHz. */
  328.     stcUpllCfg.pllmDiv = 2u;
  329.     /* upll = 8M(XTAL) / pllmDiv * plln */
  330.     stcUpllCfg.plln    = 60u;
  331.     stcUpllCfg.PllpDiv = 16u;
  332.     stcUpllCfg.PllqDiv = 16u;
  333.     stcUpllCfg.PllrDiv = 16u;
  334.     CLK_SetPllSource(ClkPllSrcXTAL);
  335.     CLK_UpllConfig(&stcUpllCfg);
  336.     CLK_UpllCmd(Enable);
  337.     CLK_SetPeriClkSource(ClkPeriSrcUpllr);
  338. #endif
  339. }

  340. /**
  341. *******************************************************************************
  342. ** \brief  ADC initial configuration.
  343. **
  344. ******************************************************************************/
  345. static void AdcInitConfig(void)
  346. {
  347.     stc_adc_init_t stcAdcInit;

  348.     MEM_ZERO_STRUCT(stcAdcInit);

  349. #if (ADC1_RESOLUTION == ADC_RESOLUTION_8BIT)
  350.     stcAdcInit.enResolution = AdcResolution_8Bit;
  351. #elif (ADC1_RESOLUTION == ADC_RESOLUTION_10BIT)
  352.     stcAdcInit.enResolution = AdcResolution_10Bit;
  353. #else
  354.     stcAdcInit.enResolution = AdcResolution_12Bit;
  355. #endif
  356.     stcAdcInit.enDataAlign  = AdcDataAlign_Right;
  357.     stcAdcInit.enAutoClear  = AdcClren_Disable;
  358.     stcAdcInit.enScanMode   = ADC1_SCAN_MODE;
  359.     /* 1. Enable ADC1. */
  360.     PWC_Fcg3PeriphClockCmd(PWC_FCG3_PERIPH_ADC1, Enable);
  361.     /* 2. Initialize ADC1. */
  362.     ADC_Init(M4_ADC1, &stcAdcInit);

  363. #if (ADC2_RESOLUTION == ADC_RESOLUTION_8BIT)
  364.     stcAdcInit.enResolution = AdcResolution_8Bit;
  365. #elif (ADC2_RESOLUTION == ADC_RESOLUTION_10BIT)
  366.     stcAdcInit.enResolution = AdcResolution_10Bit;
  367. #else
  368.     stcAdcInit.enResolution = AdcResolution_12Bit;
  369. #endif
  370.     stcAdcInit.enScanMode   = ADC2_SCAN_MODE;
  371.     /* 1. Enable ADC2. */
  372.     PWC_Fcg3PeriphClockCmd(PWC_FCG3_PERIPH_ADC2, Enable);
  373.     /* 2. Initialize ADC2. */
  374.     ADC_Init(M4_ADC2, &stcAdcInit);
  375. }

  376. /**
  377. *******************************************************************************
  378. ** \brief  ADC channel configuration.
  379. **
  380. ******************************************************************************/
  381. static void AdcChannelConfig(void)
  382. {
  383.     stc_adc_ch_cfg_t stcChCfg;
  384.     uint8_t au8Adc1SaSampTime[ADC1_SA_CHANNEL_COUNT] = ADC1_SA_CHANNEL_SAMPLE_TIME;
  385.     uint8_t au8Adc2SaSampTime[ADC2_SA_CHANNEL_COUNT] = ADC2_SA_CHANNEL_SAMPLE_TIME;

  386.     MEM_ZERO_STRUCT(stcChCfg);

  387.     stcChCfg.u32Channel  = ADC1_SA_CHANNEL;
  388.     stcChCfg.u8Sequence  = ADC_SEQ_A;
  389.     stcChCfg.pu8SampTime = au8Adc1SaSampTime;
  390.     /* 1. Set the ADC pin to analog mode. */
  391.     AdcSetChannelPinMode(M4_ADC1, ADC1_CHANNEL, Pin_Mode_Ana);
  392.     /* 2. Add ADC channel. */
  393.     ADC_AddAdcChannel(M4_ADC1, &stcChCfg);

  394.     /* 3. Configure the average channel if you need. */
  395.     ADC_ConfigAvg(M4_ADC1, AdcAvcnt_32);
  396.     /* 4. Add average channel if you need. */
  397.     ADC_AddAvgChannel(M4_ADC1, ADC1_AVG_CHANNEL);

  398.     stcChCfg.u32Channel  = ADC2_SA_CHANNEL;
  399.     stcChCfg.pu8SampTime = au8Adc2SaSampTime;
  400.     /* 1. Set the ADC pin to analog mode. */
  401.     AdcSetChannelPinMode(M4_ADC2, ADC2_CHANNEL, Pin_Mode_Ana);
  402.     /* 2. Add ADC channel. */
  403.     ADC_AddAdcChannel(M4_ADC2, &stcChCfg);

  404.     /* 3. Configure the average channel if you need. */
  405.     ADC_ConfigAvg(M4_ADC2, AdcAvcnt_64);
  406.     /* 4. Add average channel if you need. */
  407.     ADC_AddAvgChannel(M4_ADC2, ADC2_AVG_CHANNEL);
  408. }

  409. /**
  410. *******************************************************************************
  411. ** \brief  Config the pin which is mapping the channel to analog or digit mode.
  412. **
  413. ******************************************************************************/
  414. static void AdcSetChannelPinMode(const M4_ADC_TypeDef *ADCx,
  415.                                  uint32_t u32Channel,
  416.                                  en_pin_mode_t enMode)
  417. {
  418.     uint8_t u8ChIndex;
  419. #if (ADC_CH_REMAP)
  420.     uint8_t u8AdcPin;
  421. #else
  422.     uint8_t u8ChOffset = 0u;
  423. #endif

  424.     if (M4_ADC1 == ADCx)
  425.     {
  426.         u32Channel &= ADC1_PIN_MASK_ALL;
  427.     }
  428.     else
  429.     {
  430.         u32Channel &= ADC2_PIN_MASK_ALL;
  431. #if (!ADC_CH_REMAP)
  432.         u8ChOffset = 4u;
  433. #endif
  434.     }

  435.     u8ChIndex = 0u;
  436.     while (0u != u32Channel)
  437.     {
  438.         if (u32Channel & 0x1ul)
  439.         {
  440. #if (ADC_CH_REMAP)
  441.             u8AdcPin = ADC_GetChannelPinNum(ADCx, u8ChIndex);
  442.             AdcSetPinMode(u8AdcPin, enMode);
  443. #else
  444.             AdcSetPinMode((u8ChIndex+u8ChOffset), enMode);
  445. #endif
  446.         }

  447.         u32Channel >>= 1u;
  448.         u8ChIndex++;
  449.     }
  450. }

  451. /**
  452. *******************************************************************************
  453. ** \brief  Set an ADC pin as analog input mode or digit mode.
  454. **
  455. ******************************************************************************/
  456. static void AdcSetPinMode(uint8_t u8AdcPin, en_pin_mode_t enMode)
  457. {
  458.     en_port_t enPort = PortA;
  459.     en_pin_t enPin   = Pin00;
  460.     bool bFlag       = true;
  461.     stc_port_init_t stcPortInit;

  462.     MEM_ZERO_STRUCT(stcPortInit);
  463.     stcPortInit.enPinMode = enMode;
  464.     stcPortInit.enPullUp  = Disable;

  465.     switch (u8AdcPin)
  466.     {
  467.     case ADC1_IN0:
  468.         enPort = PortA;
  469.         enPin  = Pin00;
  470.         break;

  471.     case ADC1_IN1:
  472.         enPort = PortA;
  473.         enPin  = Pin01;
  474.         break;

  475.     case ADC1_IN2:
  476.         enPort = PortA;
  477.         enPin  = Pin02;
  478.         break;

  479.     case ADC1_IN3:
  480.         enPort = PortA;
  481.         enPin  = Pin03;
  482.         break;

  483.     case ADC12_IN4:
  484.         enPort = PortA;
  485.         enPin  = Pin04;
  486.         break;

  487.     case ADC12_IN5:
  488.         enPort = PortA;
  489.         enPin  = Pin05;
  490.         break;

  491.     case ADC12_IN6:
  492.         enPort = PortA;
  493.         enPin  = Pin06;
  494.         break;

  495.     case ADC12_IN7:
  496.         enPort = PortA;
  497.         enPin  = Pin07;
  498.         break;

  499.     case ADC12_IN8:
  500.         enPort = PortB;
  501.         enPin  = Pin00;
  502.         break;

  503.     case ADC12_IN9:
  504.         enPort = PortB;
  505.         enPin  = Pin01;
  506.         break;

  507.     case ADC12_IN10:
  508.         enPort = PortC;
  509.         enPin  = Pin00;
  510.         break;

  511.     case ADC12_IN11:
  512.         enPort = PortC;
  513.         enPin  = Pin01;
  514.         break;

  515.     case ADC1_IN12:
  516.         enPort = PortC;
  517.         enPin  = Pin02;
  518.         break;

  519.     case ADC1_IN13:
  520.         enPort = PortC;
  521.         enPin  = Pin03;
  522.         break;

  523.     case ADC1_IN14:
  524.         enPort = PortC;
  525.         enPin  = Pin04;
  526.         break;

  527.     case ADC1_IN15:
  528.         enPort = PortC;
  529.         enPin  = Pin05;
  530.         break;

  531.     default:
  532.         bFlag = false;
  533.         break;
  534.     }

  535.     if (true == bFlag)
  536.     {
  537.         PORT_Init(enPort, enPin, &stcPortInit);
  538.     }
  539. }

  540. /*******************************************************************************
  541. * EOF (not truncated)
  542. ******************************************************************************/


ADC转换的流程在主函数已经说的很清楚了,我把主函数摘下来大家一看就清楚了
/* Configuring a new system clock if you need. */
    SystemClockConfig();

    /* Config ADC. */
    AdcConfig();

    /* Config UART for printing. Baud rate 115200. */
    Ddl_UartInit();

    /***************** Configuration end, application start **************/

    /* ADC1 sequence A single scan. */
    /* Start ADC1, wait ADC1 scan converting done, read ADC1 data. */
    ADC_PollingSa(M4_ADC1, m_au16Adc1Value, ADC1_CH_COUNT, TIMEOUT_MS);

    /* ADC2 sequence A continuous scan. */
    u8Count = 0u;
    ADC_StartConvert(M4_ADC2);
    while (u8Count < ADC2_CONTINUOUS_TIMES)
    {
        if (Set == ADC_GetEocFlag(M4_ADC2, ADC_SEQ_A))
        {
            ADC_GetChData(M4_ADC2, ADC2_SA_CHANNEL, m_au16Adc2Value, ADC2_SA_CHANNEL_COUNT);
            ADC_ClrEocFlag(M4_ADC2, ADC_SEQ_A);
            u8Count++;
            // TODO: USE THE m_au16Adc2Value.
        }
    }

    /*
     *  DO NOT forget to stop ADC when your mode is
     *  AdcMode_SAContinuous or AdcMode_SAContinuousSBOnce
     *  unless you need.
     */
    ADC_StopConvert(M4_ADC2);

    while (1u)
    {
        ADC_PollingSa(M4_ADC1, m_au16Adc1Value, ADC1_CH_COUNT, TIMEOUT_MS);
        /* ADC1 channel 10 maps pin ADC12_IN10 by default. */
        printf("\nADC12_IN10 value %d.", m_au16Adc1Value[10u]);
        printf("\nADC12_IN10 voltage is %.4fV.",
            ((float)m_au16Adc1Value[10u] * ADC_VREF) / (float)ADC1_ACCURACY);

        /* Main loop cycle is 500ms. */
        Ddl_Delay1ms(500u);
    }

打赏榜单

21小跑堂 打赏了 10.00 元 2021-02-22
理由:恭喜通过原创文章审核!请多多加油哦!

jcky001 发表于 2021-2-14 15:03 | 显示全部楼层
大年初二还在给我们科普,楼主辛苦哈
edyd 发表于 2021-2-15 20:21 | 显示全部楼层
大年初四来顶贴,不错不错!学习了!
zhuotuzi 发表于 2021-2-22 23:35 | 显示全部楼层
来学习一下。
guijial511 发表于 2021-2-23 08:49 来自手机 | 显示全部楼层
不错,学习了。
wanduzi 发表于 2021-2-23 20:06 | 显示全部楼层
看看是啥啊。
huangcunxiake 发表于 2021-2-23 22:37 | 显示全部楼层
看不懂。高深
pzljun 发表于 2021-4-7 01:06 | 显示全部楼层
这个坑货的模拟采集,没有设置基准源的寄存器~
caigang13 发表于 2021-4-7 09:45 来自手机 | 显示全部楼层
感谢楼主的详细讲解
aspoke 发表于 2021-4-10 21:17 | 显示全部楼层
非DMA方式 多路ADC   
232321122 发表于 2021-4-10 21:17 | 显示全部楼层
         
ghuca 发表于 2021-4-10 21:18 | 显示全部楼层
如何得到最佳adc精度  
soodesyt 发表于 2021-4-10 21:19 | 显示全部楼层
总结的非常到位。   
plsbackup 发表于 2021-4-10 21:20 | 显示全部楼层
单片机的性能怎么样  
mnynt121 发表于 2021-4-10 21:20 | 显示全部楼层
期待楼主更多的分享了。  
kmzuaz 发表于 2021-4-10 21:21 | 显示全部楼层
ADC 规则通道 和 注入通道的区别
qiufengsd 发表于 2021-4-10 21:21 | 显示全部楼层
能同时接32个模拟通道吗  
sdCAD 发表于 2021-4-10 21:22 | 显示全部楼层
17个通道吗
fengm 发表于 2021-4-10 21:25 | 显示全部楼层
         
plsbackup 发表于 2021-4-10 21:26 | 显示全部楼层
转换 速度可以到多少呢  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

457

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部