[DemoCode下载] ADC的高速连续扫描模式

[复制链接]
717|2
 楼主| 玛尼玛尼哄 发表于 2024-2-28 20:51 | 显示全部楼层 |阅读模式
ck, TE, dc, ADC, AD
  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]    Demonstrate how to use HIRC as ADC clock source to achieve 1411 ksps ADC conversion rate.
  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. volatile uint32_t g_u32AdcIntFlag;


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

  19.     /* Enable HIRC clock (Internal RC 48 MHz) */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

  23.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  25.     /* Enable UART module clock */
  26.     CLK_EnableModuleClock(UART0_MODULE);

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

  29.     /* Enable ADC module clock */
  30.     CLK_EnableModuleClock(ADC_MODULE);

  31.     /* ADC clock source is HCLK = PCLK1 = 48MHz, set divider to 2, ADC clock is 48/2 MHz */
  32.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(2));

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

  36.     /*----------------------------------------------------------------------*/
  37.     /* Init I/O Multi-function                                              */
  38.     /*----------------------------------------------------------------------*/

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

  42.     /* Set PB.0 ~ PB.3 to input mode */
  43.     GPIO_SetMode(PB, BIT0|BIT1|BIT2|BIT3, GPIO_MODE_INPUT);

  44.     /* Configure the GPB0 - GPB3 ADC analog input pins.  */
  45.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk |
  46.                        SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
  47.     SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_ADC0_CH0 | SYS_GPB_MFPL_PB1MFP_ADC0_CH1 |
  48.                       SYS_GPB_MFPL_PB2MFP_ADC0_CH2 | SYS_GPB_MFPL_PB3MFP_ADC0_CH3);

  49.     /* Disable the GPB0 - GPB3 digital input path to avoid the leakage current. */
  50.     GPIO_DISABLE_DIGITAL_PATH(PB, BIT0|BIT1|BIT2|BIT3);

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

  54. void ADC_FunctionTest()
  55. {
  56.     uint32_t u32ChannelCount;
  57.     int32_t  i32ConversionData;

  58.     printf("\n");
  59.     printf("+----------------------------------------------------------------------+\n");
  60.     printf("|           ADC 1411 ksps continuous scan mode sample code             |\n");
  61.     printf("+----------------------------------------------------------------------+\n\n");

  62.     printf("+----------------------------------------------------------------------+\n");
  63.     printf("|   ADC clock source -> PCLK1  = 48 MHz                                |\n");
  64.     printf("|   ADC clock divider          = 2                                     |\n");
  65.     printf("|   ADC clock                  = 48 MHz / 2 = 24 MHz                   |\n");
  66.     printf("|   ADC extended sampling time = 0                                     |\n");
  67.     printf("|   ADC conversion time = 17 + ADC extended sampling time = 17         |\n");
  68.     printf("|   ADC conversion rate = 24 MHz / 17 = 1411 ksps                      |\n");
  69.     printf("+----------------------------------------------------------------------+\n");

  70.     /* Enable ADC converter */
  71.     ADC_POWER_ON(ADC);

  72.     while(1)
  73.     {
  74.         printf(" Press any key to start the continuous scan mode test\n");
  75.         getchar();
  76.         /* Set the ADC operation mode as continuous scan, input mode as single-end and
  77.              enable the analog input channel 0, 1, 2 and 3 */
  78.         ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, BIT0|BIT1|BIT2|BIT3);

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

  81.         /* Enable the sample module interrupt */
  82.         ADC_ENABLE_INT(ADC, ADC_ADF_INT);  // Enable sample module A/D interrupt.
  83.         NVIC_EnableIRQ(ADC_IRQn);

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

  87.         /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  88.         while(g_u32AdcIntFlag == 0);

  89.         /* Get the conversion result */
  90.         for(u32ChannelCount = 0; u32ChannelCount < 4; u32ChannelCount++)
  91.         {
  92.             i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, u32ChannelCount);
  93.             printf("Conversion result of channel %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData);
  94.         }

  95.         printf("\n");

  96.         /* Stop A/D conversion */
  97.         ADC_STOP_CONV(ADC);

  98.         /* Disable the sample module interrupt */
  99.         ADC_DISABLE_INT(ADC, ADC_ADF_INT);
  100.     }
  101. }


  102. void ADC_IRQHandler(void)
  103. {
  104.     g_u32AdcIntFlag = 1;
  105.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Clear the A/D interrupt flag */
  106. }

  107. /*----------------------------------------------------------------------*/
  108. /* Init UART0                                                           */
  109. /*----------------------------------------------------------------------*/
  110. void UART0_Init(void)
  111. {
  112.     /* Reset UART0 */
  113.     SYS_ResetModule(UART0_RST);

  114.     /* Configure UART0 and set UART0 baud rate */
  115.     UART_Open(UART0, 115200);
  116. }

  117. int32_t main(void)
  118. {
  119.     /* Init System, IP clock and multi-function I/O. */
  120.     SYS_Init();

  121.     /* Init UART0 for printf */
  122.     UART0_Init();

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

  124.     /* ADC function test */
  125.     ADC_FunctionTest();

  126.     /* Disable ADC IP clock */
  127.     CLK_DisableModuleClock(ADC_MODULE);

  128.     /* Disable External Interrupt */
  129.     NVIC_DisableIRQ(ADC_IRQn);

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

  131.     while(1);
  132. }


 楼主| 玛尼玛尼哄 发表于 2024-2-28 20:52 | 显示全部楼层
这段代码是一个用于演示如何使用高精度内部振荡器(HIRC)作为ADC(模数转换器)时钟源以实现1411千样本每秒(ksps)ADC转换速率的示例代码。以下是代码的主要功能和流程:

初始化函数 SYS_Init():

解锁受保护的寄存器以允许对其进行写入操作。
启用HIRC(内部RC 48 MHz)作为系统时钟,并等待其稳定。
配置系统时钟和模块时钟,如UART0和ADC模块。
配置GPIO以及ADC的模拟输入引脚,并禁用数字输入路径以避免泄漏电流。
锁定受保护的寄存器以防止进一步更改。
ADC功能测试函数 ADC_FunctionTest():

输出ADC的配置信息,包括时钟源、时钟分频、扩展采样时间、转换时间和转换速率。
在循环中,等待用户按下任意键来启动连续扫描模式测试。
配置ADC的操作模式、输入模式和要启用的模拟输入通道。
清除ADC中断标志并启用ADC中断。
启动ADC转换并等待ADC中断完成。
获取每个通道的转换结果并打印输出。
停止ADC转换并禁用ADC中断。
ADC中断处理函数 ADC_IRQHandler():

在ADC中断中设置标志以指示转换完成,并清除ADC中断标志。
初始化UART0函数 UART0_Init():

重置UART0模块。
配置UART0并设置波特率为115200。
主函数 main():

调用SYS_Init()和UART0_Init()来初始化系统和UART0模块。
输出系统时钟频率。
调用ADC_FunctionTest()来执行ADC功能测试。
禁用ADC模块的时钟和ADC中断。
循环等待。
 楼主| 玛尼玛尼哄 发表于 2024-2-28 20:52 | 显示全部楼层
这段代码的主要目的是演示如何配置并使用NUC029系列微控制器的ADC模块以实现高速ADC转换。通过使用HIRC作为ADC时钟源,可以实现较高的ADC转换速率。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

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