[DemoCode下载] M091的ADC在Burst模式下如何操作

[复制链接]
1871|3
 楼主| yiyigirl2014 发表于 2023-11-12 23:04 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Perform A/D Conversion with ADC burst mode.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"


  11. /*---------------------------------------------------------------------------------------------------------*/
  12. /* Define global variables and constants                                                                   */
  13. /*---------------------------------------------------------------------------------------------------------*/
  14. #define CONV_TOTAL_COUNT    20


  15. void SYS_Init(void)
  16. {
  17.     /* Unlock protected registers */
  18.     SYS_UnlockReg();

  19.     /* Enable HIRC */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  21.     /* Waiting for HIRC clock ready */
  22.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  23.     /* Switch HCLK clock source to HIRC */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  25.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  26.     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);

  27.     /* Switch UART0 clock source to HIRC */
  28.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  29.     /* Enable UART peripheral clock */
  30.     CLK_EnableModuleClock(UART0_MODULE);

  31.     /* Enable ADC module clock */
  32.     CLK_EnableModuleClock(ADC_MODULE);

  33.     /* ADC clock source is PCLK1, set divider to 1 */
  34.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(1));

  35.     /* Update System Core Clock */
  36.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  37.     SystemCoreClockUpdate();

  38.     /*----------------------------------------------------------------------*/
  39.     /* Init I/O Multi-function                                              */
  40.     /*----------------------------------------------------------------------*/
  41.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  42.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  43.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);


  44.     /* Set PB.2 - PB.3 to input mode */
  45.     GPIO_SetMode(PB, BIT2|BIT3, GPIO_MODE_INPUT);
  46.     /* Configure the PB.2 - PB.3 ADC analog input pins. */
  47.     SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk)) |
  48.                     (SYS_GPB_MFPL_PB2MFP_ADC0_CH2 | SYS_GPB_MFPL_PB3MFP_ADC0_CH3);
  49.     /* Disable the PB.2 - PB.3 digital input path to avoid the leakage current. */
  50.     GPIO_DISABLE_DIGITAL_PATH(PB, BIT2|BIT3);

  51.     /* Lock protected registers */
  52.     SYS_LockReg();
  53. }

  54. void ADC_FunctionTest()
  55. {
  56.     uint8_t  u8Option;
  57.     uint32_t u32ConvCount;
  58.     int32_t  i32ConversionData[CONV_TOTAL_COUNT];

  59.     printf("\n");
  60.     printf("+----------------------------------------------------------------------+\n");
  61.     printf("|                     ADC burst mode sample code                       |\n");
  62.     printf("+----------------------------------------------------------------------+\n");

  63.     for(u32ConvCount = 0; u32ConvCount < CONV_TOTAL_COUNT; u32ConvCount++)
  64.     {
  65.         i32ConversionData[u32ConvCount] = 0;
  66.     }

  67.     /* Enable ADC converter */
  68.     ADC_POWER_ON(ADC);

  69.     while(1)
  70.     {
  71.         printf("Select input mode:\n");
  72.         printf("  [1] Single end input (channel 2 only)\n");
  73.         printf("  [2] Differential input (channel pair 1 only)\n");
  74.         printf("  Other keys: exit burst mode test\n");
  75.         u8Option = getchar();

  76.         if(u8Option == '1')
  77.         {
  78.             /* Set input mode as single-end, burst mode, and select channel 2 */
  79.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_BURST, BIT2);

  80.             /* Clear the A/D interrupt flag for safe */
  81.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  82.             /* Reset the ADC interrupt indicator and trigger sample module to start A/D conversion */
  83.             u32ConvCount = 0;
  84.             ADC_START_CONV(ADC);

  85.             while(1)
  86.             {
  87.                 /* Wait ADC conversion completed */
  88.                 while (ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)==0);
  89.                 ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* clear ADF interrupt flag */

  90.                 /* Get the conversion result until VALIDF turns to 0 */
  91.                 while(ADC->ADSR0 & ADC_ADSR0_VALIDF_Msk)
  92.                 {
  93.                     /* Get the conversion result from ADC channel 0 always */
  94.                     i32ConversionData[u32ConvCount++] = ADC_GET_CONVERSION_DATA(ADC, 0);
  95.                     if(u32ConvCount == CONV_TOTAL_COUNT)
  96.                         break;
  97.                 }

  98.                 if(u32ConvCount == CONV_TOTAL_COUNT)
  99.                     break;
  100.             }

  101.             /* Stop A/D conversion */
  102.             ADC_STOP_CONV(ADC);

  103.             /* Show the conversion result */
  104.             for(u32ConvCount = 0; u32ConvCount < CONV_TOTAL_COUNT; u32ConvCount++)
  105.             {
  106.                 printf("Conversion result of channel 2 [#%d]: 0x%X (%d)\n", u32ConvCount+1, i32ConversionData[u32ConvCount], i32ConversionData[u32ConvCount]);
  107.             }

  108.             /* Clear remaining data in FIFO that got before stop ADC */
  109.             while (ADC_IS_DATA_VALID(ADC, 0))
  110.             {
  111.                 i32ConversionData[0] = ADC_GET_CONVERSION_DATA(ADC, 0);
  112.             }
  113.         }
  114.         else if(u8Option == '2')
  115.         {
  116.             /* Set input mode as differential, burst mode, and select channel 2 */
  117.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_BURST, BIT2);

  118.             /* Clear the A/D interrupt flag for safe */
  119.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  120.             /* Reset the ADC interrupt indicator and trigger sample module to start A/D conversion */
  121.             u32ConvCount = 0;
  122.             ADC_START_CONV(ADC);

  123.             while(1)
  124.             {
  125.                 /* Wait ADC conversion completed */
  126.                 while (ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)==0);
  127.                 ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* clear ADF interrupt flag */

  128.                 /* Get the conversion result until VALIDF turns to 0 */
  129.                 while(ADC->ADSR0 & ADC_ADSR0_VALIDF_Msk)
  130.                 {
  131.                     /* Get the conversion result from ADC channel 0 always */
  132.                     i32ConversionData[u32ConvCount++] = ADC_GET_CONVERSION_DATA(ADC, 0);
  133.                     if(u32ConvCount == CONV_TOTAL_COUNT)
  134.                         break;
  135.                 }

  136.                 if(u32ConvCount == CONV_TOTAL_COUNT)
  137.                     break;
  138.             }

  139.             /* Stop A/D conversion */
  140.             ADC_STOP_CONV(ADC);

  141.             /* Show the conversion result */
  142.             for(u32ConvCount = 0; u32ConvCount < CONV_TOTAL_COUNT; u32ConvCount++)
  143.             {
  144.                 printf("Conversion result of channel pair 1 [#%d]: 0x%X (%d)\n", u32ConvCount+1, i32ConversionData[u32ConvCount], i32ConversionData[u32ConvCount]);
  145.             }

  146.             /* Clear remaining data in FIFO that got before stop ADC */
  147.             while (ADC_IS_DATA_VALID(ADC, 0))
  148.             {
  149.                 i32ConversionData[0] = ADC_GET_CONVERSION_DATA(ADC, 0);
  150.             }
  151.         }
  152.         else
  153.             return;

  154.         printf("\n");
  155.     }
  156. }

  157. /*----------------------------------------------------------------------*/
  158. /* Init UART0                                                           */
  159. /*----------------------------------------------------------------------*/
  160. void UART0_Init(void)
  161. {
  162.     /* Reset UART0 */
  163.     SYS_ResetModule(UART0_RST);

  164.     /* Configure UART0 and set UART0 baud rate */
  165.     UART_Open(UART0, 115200);
  166. }

  167. int32_t main(void)
  168. {
  169.     /* Init System, IP clock and multi-function I/O. */
  170.     SYS_Init();

  171.     /* Init UART0 for printf */
  172.     UART0_Init();

  173.     printf("\nSystem clock rate: %d Hz", SystemCoreClock);

  174.     /* ADC function test */
  175.     ADC_FunctionTest();

  176.     /* Disable ADC IP clock */
  177.     CLK_DisableModuleClock(ADC_MODULE);

  178.     printf("Exit ADC sample code\n");

  179.     while(1);
  180. }


elephant00 发表于 2023-11-23 16:17 | 显示全部楼层
开启ADC:首先,需要开启ADC功能。在NuMicro M091系列中,可以通过设置相应的寄存器或特殊功能寄存器来启动ADC。
设置ADC模式:M091的ADC支持多种模式,包括单次转换模式、连续转换模式和Burst模式。要使用Burst模式,需要设置ADC的控制寄存器来选择Burst模式。
设置ADC触发源:ADC的触发源可以是定时器事件、外部信号等。根据需要,可以通过设置相应的寄存器或特殊功能寄存器来选择合适的触发源。
设置ADC通道:ADC可以测量模拟信号的电压,选择的通道将决定测量的信号源。可以在M091的ADC中设置要使用的通道。
启动ADC转换:一旦ADC的所有设置都完成了,可以启动ADC进行转换。在Burst模式下,ADC会连续进行多次转换,直到达到预设的转换数量或者收到停止命令。
读取ADC结果:ADC转换完成后,可以读取ADC的结果。在M091中,可以通过相应的寄存器或特殊功能寄存器来读取ADC的转换结果。
huangcunxiake 发表于 2023-11-30 19:56 | 显示全部楼层
新唐的部分例子让我看的有点头大。
EmmaTT 发表于 2023-12-6 10:40 来自手机 | 显示全部楼层
还好吧,写的也挺清晰的了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

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