[DemoCode下载] ADC的简单用法

[复制链接]
1012|7
 楼主| xixi2017 发表于 2024-2-25 19:02 | 显示全部楼层 |阅读模式
ar, 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.0
  4. * $Revision: 2 $
  5. * $Date: 15/02/06 10:22a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Monitor the conversion result of channel 2 by the digital compare function.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M058S.h"

  13. #define PLL_CLOCK       50000000



  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Define Function Prototypes                                                                              */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. void SYS_Init(void);
  18. void UART0_Init(void);
  19. void AdcResultMonitorTest(void);


  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* Define global variables and constants                                                                   */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. volatile uint32_t g_u32AdcCmp0IntFlag;
  24. volatile uint32_t g_u32AdcCmp1IntFlag;


  25. void SYS_Init(void)
  26. {
  27.     /*---------------------------------------------------------------------------------------------------------*/
  28.     /* Init System Clock                                                                                       */
  29.     /*---------------------------------------------------------------------------------------------------------*/

  30.     /* Enable Internal RC 22.1184 MHz clock */
  31.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  32.     /* Waiting for Internal RC clock ready */
  33.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  34.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  35.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  36.     /* Enable external XTAL 12 MHz clock */
  37.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  38.     /* Waiting for external XTAL clock ready */
  39.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  40.     /* Set core clock as PLL_CLOCK from PLL */
  41.     CLK_SetCoreClock(PLL_CLOCK);

  42.     /* Enable UART module clock */
  43.     CLK_EnableModuleClock(UART0_MODULE);

  44.     /* Enable ADC module clock */
  45.     CLK_EnableModuleClock(ADC_MODULE);

  46.     /* Select UART module clock source */
  47.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  48.     /* ADC clock source is 22.1184 MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  49.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init I/O Multi-function                                                                                 */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  54.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  55.     SYS->P3_MFP |= SYS_MFP_P30_RXD | SYS_MFP_P31_TXD;

  56.     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  57.     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

  58.     /* Configure the P1.0 - P1.3 ADC analog input pins */
  59.     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  60.     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

  61. }

  62. /*---------------------------------------------------------------------------------------------------------*/
  63. /* Init UART                                                                                               */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. void UART0_Init()
  66. {
  67.     /* Reset IP */
  68.     SYS_ResetModule(UART0_RST);

  69.     /* Configure UART0 and set UART0 Baudrate */
  70.     UART_Open(UART0, 115200);
  71. }

  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /* Function: AdcResultMonitorTest                                                                          */
  74. /*                                                                                                         */
  75. /* Parameters:                                                                                             */
  76. /*   None.                                                                                                 */
  77. /*                                                                                                         */
  78. /* Returns:                                                                                                */
  79. /*   None.                                                                                                 */
  80. /*                                                                                                         */
  81. /* Description:                                                                                            */
  82. /*   ADC result monitor function test.                                                                     */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. void AdcResultMonitorTest()
  85. {
  86.     printf("\n");
  87.     printf("+----------------------------------------------------------------------+\n");
  88.     printf("|           ADC compare function (result monitor) sample code          |\n");
  89.     printf("+----------------------------------------------------------------------+\n");
  90.     printf("\nIn this test, software will compare the conversion result of channel 2.\n");

  91.     /* Power on ADC module */
  92.     ADC_POWER_ON(ADC);

  93.     /* Set the ADC operation mode as continuous scan, input mode as single-end and enable the analog input channel 2 */
  94.     ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, 0x1 << 2);

  95.     /* Enable ADC comparator 0. Compare condition: conversion result < 0x800; match Count=5. */
  96.     printf("   Set the compare condition of comparator 0: channel 2 is less than 0x800; match count is 5.\n");
  97.     ADC_ENABLE_CMP0(ADC, 2, ADC_ADCMPR_CMPCOND_LESS_THAN, 0x800, 5);

  98.     /* Enable ADC comparator 1. Compare condition: conversion result >= 0x800; match Count=5. */
  99.     printf("   Set the compare condition of comparator 1: channel 2 is greater than or equal to 0x800; match count is 5.\n");
  100.     ADC_ENABLE_CMP1(ADC, 2, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x800, 5);

  101.     /* clear the ADC comparator 0 interrupt flag for safe */
  102.     ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);
  103.     /* enable ADC comparator 0 interrupt */
  104.     ADC_EnableInt(ADC, ADC_CMP0_INT);

  105.     /* clear the ADC comparator 1 interrupt flag for safe */
  106.     ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);
  107.     /* enable ADC comparator 1 interrupt */
  108.     ADC_EnableInt(ADC, ADC_CMP1_INT);

  109.     NVIC_EnableIRQ(ADC_IRQn);

  110.     g_u32AdcCmp0IntFlag = 0;
  111.     g_u32AdcCmp1IntFlag = 0;

  112.     /* Clear the ADC interrupt flag */
  113.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  114.     /* start A/D conversion */
  115.     ADC_START_CONV(ADC);

  116.     /* Wait ADC compare interrupt */
  117.     while((g_u32AdcCmp0IntFlag == 0) && (g_u32AdcCmp1IntFlag == 0));

  118.     /* Stop A/D conversion */
  119.     ADC_STOP_CONV(ADC);
  120.     /* Disable ADC comparator interrupt */
  121.     ADC_DisableInt(ADC, ADC_CMP0_INT);
  122.     ADC_DisableInt(ADC, ADC_CMP1_INT);
  123.     /* Disable compare function */
  124.     ADC_DISABLE_CMP0(ADC);
  125.     ADC_DISABLE_CMP1(ADC);

  126.     if(g_u32AdcCmp0IntFlag == 1)
  127.     {
  128.         printf("Comparator 0 interrupt occurs.\nThe conversion result of channel 2 is less than 0x800\n");
  129.     }
  130.     else
  131.     {
  132.         printf("Comparator 1 interrupt occurs.\nThe conversion result of channel 2 is greater than or equal to 0x800\n");
  133.     }
  134. }


  135. /*---------------------------------------------------------------------------------------------------------*/
  136. /* ADC interrupt handler                                                                                   */
  137. /*---------------------------------------------------------------------------------------------------------*/
  138. void ADC_IRQHandler(void)
  139. {
  140.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP0_INT) != 0)
  141.     {
  142.         g_u32AdcCmp0IntFlag = 1;
  143.         ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);     /* clear the A/D compare flag 0 */
  144.     }

  145.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP1_INT) != 0)
  146.     {
  147.         g_u32AdcCmp1IntFlag = 1;
  148.         ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);     /* clear the A/D compare flag 1 */
  149.     }
  150. }
  151. /*---------------------------------------------------------------------------------------------------------*/
  152. /* MAIN function                                                                                           */
  153. /*---------------------------------------------------------------------------------------------------------*/

  154. int main(void)
  155. {

  156.     /* Unlock protected registers */
  157.     SYS_UnlockReg();

  158.     /* Init System, IP clock and multi-function I/O */
  159.     SYS_Init();

  160.     /* Lock protected registers */
  161.     SYS_LockReg();

  162.     /* Init UART0 for printf */
  163.     UART0_Init();

  164.     /*---------------------------------------------------------------------------------------------------------*/
  165.     /* SAMPLE CODE                                                                                             */
  166.     /*---------------------------------------------------------------------------------------------------------*/

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

  168.     /* Result monitor test */
  169.     AdcResultMonitorTest();

  170.     /* Disable ADC module */
  171.     ADC_Close(ADC);

  172.     /* Disable ADC IP clock */
  173.     CLK_DisableModuleClock(ADC_MODULE);

  174.     /* Disable External Interrupt */
  175.     NVIC_DisableIRQ(ADC_IRQn);

  176.     printf("\nExit ADC sample code\n");

  177.     while(1);

  178. }



 楼主| xixi2017 发表于 2024-2-25 19:03 | 显示全部楼层
这段单片机代码是用来监视通道2的转换结果,通过数字比较功能来实现。以下是代码的主要功能和结构:

首先是一些头文件包含和宏定义,其中包含了芯片型号相关的头文件和一些宏定义,如时钟频率等。

定义了一些全局变量,用于标志ADC比较中断的发生情况。

SYS_Init() 函数用于初始化系统时钟和相关的模块,包括使能内部和外部时钟,设置时钟源,初始化多功能引脚等。

UART0_Init() 函数用于初始化UART0串口通信模块,设置波特率等。

AdcResultMonitorTest() 函数用于ADC结果监视测试。在这个函数中:

打印测试信息。
启动ADC模块并配置为连续扫描模式,设置通道2为单端输入。
启用ADC比较功能,通过比较条件设置通道2的转换结果与预设值进行比较,并设置匹配计数。
启用ADC比较中断,并等待中断发生。
中断服务函数 ADC_IRQHandler() 用于处理ADC的比较中断,当比较中断发生时,设置相应的标志位。
根据比较中断的标志位确定通道2的转换结果是小于预设值还是大于等于预设值,并输出相应信息。
main() 函数中:

解锁保护寄存器。
初始化系统。
锁定保护寄存器。
初始化UART0。
打印系统时钟频率。
运行ADC结果监视测试。
关闭ADC模块和相应时钟,并禁用ADC比较中断。
死循环。
总体来说,这段代码的功能是通过ADC监视通道2的转换结果,根据预设的比较条件判断结果大小,并通过UART0串口输出相应信息。
wanduzi 发表于 2024-2-25 19:23 | 显示全部楼层
最简单的就是初始化,查询读取。
zhuotuzi 发表于 2024-2-26 22:40 | 显示全部楼层
单片机的ADC(模数转换器)是一种将模拟信号转换为数字信号的器件。单片机的ADC具有以下特点:

分辨率:ADC的分辨率是指其能够将模拟信号转换为多少个数字量。例如,一个12位ADC可以将模拟信号转换为4096个数字量。
采样率:ADC的采样率是指其每秒钟能够采样多少次模拟信号。例如,一个100kHz的ADC每秒钟可以采样100000次模拟信号。
单片机ADC的简单用法

单片机ADC的简单用法如下:

配置ADC
选择ADC的通道
设置ADC的分辨率
设置ADC的采样率
启动ADC转换
发送ADC转换启动命令
读取ADC转换结果
读取ADC转换结果寄存器
heisexingqisi 发表于 2024-2-27 21:07 | 显示全部楼层
测量热敏电阻
 楼主| xixi2017 发表于 2024-2-28 11:43 | 显示全部楼层
wanduzi 发表于 2024-2-25 19:23
最简单的就是初始化,查询读取。

最常用的MCU外设之一。
yiy 发表于 2024-2-28 16:06 | 显示全部楼层
ADC具备中断功能吗
xinpian101 发表于 2024-2-28 20:26 | 显示全部楼层
使能时钟,使能中断。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

145

主题

2034

帖子

2

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