[牛人杂谈] 通过写入ADC软件触发寄存器来触发ADC

[复制链接]
1163|7
 楼主| huangcunxiake 发表于 2024-1-21 21:14 | 显示全部楼层 |阅读模式
  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]    Trigger ADC by writing ADC software trigger register.
  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 */
  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.     int32_t  i32ConversionData;

  58.     printf("\n");
  59.     printf("+----------------------------------------------------------------------+\n");
  60.     printf("|                   ADC Software trigger mode test                     |\n");
  61.     printf("+----------------------------------------------------------------------+\n");

  62.     /* Enable ADC converter */
  63.     ADC_POWER_ON(ADC);

  64.     while(1)
  65.     {
  66.         printf("Select input mode:\n");
  67.         printf("  [1] Single end input (channel 2 only)\n");
  68.         printf("  [2] Differential input (channel pair 1 only)\n");
  69.         printf("  Other keys: exit single mode test\n");
  70.         u8Option = getchar();

  71.         if(u8Option == '1')
  72.         {
  73.             /* Set input mode as single-end, Single-cycle scan mode, and select channel 2 */
  74.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE_CYCLE, BIT2);

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

  77.             /* Enable the sample module interrupt */
  78.             ADC_ENABLE_INT(ADC, ADC_ADF_INT);  /* Enable sample module A/D interrupt. */
  79.             NVIC_EnableIRQ(ADC_IRQn);

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

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

  85.             /* Disable the sample module interrupt */
  86.             ADC_DISABLE_INT(ADC, ADC_ADF_INT);

  87.             /* Get the conversion result of ADC channel 2 */
  88.             i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 2);
  89.             printf("Conversion result of channel 2: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData);
  90.         }
  91.         else if(u8Option == '2')
  92.         {
  93.             /* Set input mode as differential, Single-cycle scan mode, and select channel 2 */
  94.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_SINGLE_CYCLE, BIT2);

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

  97.             /* Enable the sample module interrupt */
  98.             ADC_ENABLE_INT(ADC, ADC_ADF_INT);  /* Enable sample module A/D interrupt. */
  99.             NVIC_EnableIRQ(ADC_IRQn);

  100.             /* Reset the ADC indicator and trigger sample module to start A/D conversion */
  101.             g_u32AdcIntFlag = 0;
  102.             ADC_START_CONV(ADC);

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

  105.             /* Disable the sample module interrupt */
  106.             ADC_DISABLE_INT(ADC, ADC_ADF_INT);

  107.             /* Get the conversion result of channel 2 */
  108.             i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 2);
  109.             printf("Conversion result of channel pair 1: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData);
  110.         }
  111.         else
  112.             return ;
  113.     }
  114. }


  115. void ADC_IRQHandler(void)
  116. {
  117.     g_u32AdcIntFlag = 1;
  118.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Clear the A/D interrupt flag */
  119. }

  120. /*----------------------------------------------------------------------*/
  121. /* Init UART0                                                           */
  122. /*----------------------------------------------------------------------*/
  123. void UART0_Init(void)
  124. {
  125.     /* Reset UART0 */
  126.     SYS_ResetModule(UART0_RST);

  127.     /* Configure UART0 and set UART0 baud rate */
  128.     UART_Open(UART0, 115200);
  129. }

  130. int32_t main(void)
  131. {
  132.     /* Init System, IP clock and multi-function I/O. */
  133.     SYS_Init();

  134.     /* Init UART0 for printf */
  135.     UART0_Init();

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

  137.     /* ADC function test */
  138.     ADC_FunctionTest();

  139.     /* Disable ADC IP clock */
  140.     CLK_DisableModuleClock(ADC_MODULE);

  141.     /* Disable External Interrupt */
  142.     NVIC_DisableIRQ(ADC_IRQn);

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

  144.     while(1);
  145. }
程序的主要功能是通过写入ADC软件触发寄存器来触发ADC(模数转换器)并进行采样,然后根据用户选择的输入模式输出ADC的转换结果。
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* Define global variables and constants                                                                   */
  3. /*---------------------------------------------------------------------------------------------------------*/
  4. volatile uint32_t g_u32AdcIntFlag;
定义了一个全局变量g_u32AdcIntFlag,用于标志ADC的中断是否发生。
 楼主| huangcunxiake 发表于 2024-1-21 21:15 | 显示全部楼层
系统初始化函数 SYS_Init()

  1. void SYS_Init(void)
  2. {
  3.     // ...
  4. }

此函数用于初始化系统。包括解锁保护寄存器、启用内部高速振荡器(HIRC)、配置时钟源、初始化串口通信(UART)和ADC等模块。
ADC功能测试函数 ADC_FunctionTest()
此函数用于测试ADC的功能。用户可以选择单端输入模式或差分输入模式,并选择相应的通道进行ADC转换。在函数中,通过UART输出相关信息,然后配置ADC并触发转换,等待ADC中断,并在中断发生后获取ADC的转换结果。
 楼主| huangcunxiake 发表于 2024-1-21 21:15 | 显示全部楼层
DC中断处理函数 ADC_IRQHandler()
  1. void ADC_IRQHandler(void)
  2. {
  3.     g_u32AdcIntFlag = 1;
  4.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Clear the A/D interrupt flag */
  5. }

DC中断处理函数,在ADC转换完成时被调用。将全局变量g_u32AdcIntFlag置为1,表示ADC中断已发生,并清除ADC中断标志。
 楼主| huangcunxiake 发表于 2024-1-21 21:16 | 显示全部楼层
ART初始化函数 UART0_Init()
此函数用于初始化UART0模块,包括复位UART0、配置UART0通信参数等。
main() 函数
主函数初始化系统、UART,输出系统时钟频率信息,调用ADC功能测试函数进行ADC测试,然后关闭ADC和相关时钟。

整个程序的主要流程是初始化系统、UART和ADC,然后进入ADC功能测试函数,在该函数中根据用户选择进行ADC转换,最后在主函数中关闭ADC和相关时钟。程序通过UART与用户进行交互,输出ADC转换结果。
yiyigirl2014 发表于 2024-1-22 22:57 | 显示全部楼层
软件出发是一种比较灵活的方式实用ADC
OliviaSH 发表于 2024-1-23 16:22 | 显示全部楼层
软件触发更灵活
埃娃 发表于 2024-1-29 10:43 来自手机 | 显示全部楼层
软件触发用的是不是更多些啊
海边浪漫幻象 发表于 2025-9-8 15:36 | 显示全部楼层
通过写入 ADC 软件触发寄存器触发 ADC 的过程:先配置 ADC 工作模式(如采样通道、分辨率),确保 ADC 处于就绪状态;向软件触发寄存器写入特定触发值(如 0x01),寄存器收到指令后,立即启动 ADC 转换;转换完成后,ADC 会置位完成标志,可通过查询或中断读取转换结果。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3745

帖子

11

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