[DemoCode下载] ADC量測系統電壓

[复制链接]
1291|5
 楼主| a_ziliu 发表于 2016-12-26 10:38 | 显示全部楼层 |阅读模式
NUC240的ADC量測系統電壓
EC_NUC230_240_ADC_AVDD.zip (975.68 KB, 下载次数: 31)
643757107 发表于 2016-12-26 15:21 | 显示全部楼层
这个需要分压吗?直接测VCC?
598330983 发表于 2016-12-26 16:07 | 显示全部楼层
学习一下这个系列的操作方式
huangcunxiake 发表于 2016-12-31 10:38 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 2 $
  5. * $Date: 15/08/18 11:54a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NuEdu Basic01 Volume Knob Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include "NuEdu-Basic01.h"
  14. volatile uint8_t g_u8ADF;
  15. #define VBG_VOLTAGE (1250) /* 1.20V = 1200 mV (Typical band-gap voltage) */
  16. //#define ADC_SAMPLE_COUNT 128 /* The last line of GetAVDDCodeByADC() need revise when ADC_SAMPLE_COUNT is changed. */
  17. #define ADC_SAMPLE_COUNT 128 /* The last line of GetAVDDCodeByADC() need revise when ADC_SAMPLE_COUNT is changed. */
  18. void ADC_IRQHandler(void)
  19. {
  20.     uint32_t u32Flag;

  21.     /* Get ADC conversion finish interrupt flag */
  22.     u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT);

  23.     /* Check ADC conversion finish */
  24.     if(u32Flag & ADC_ADF_INT)
  25.         g_u8ADF = 1;

  26.     /* Clear conversion finish flag */
  27.     ADC_CLR_INT_FLAG(ADC, u32Flag);
  28. }
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. /* Function: GetAVDDCodeByADC                                                                              */
  31. /*                                                                                                         */
  32. /* Parameters:                                                                                             */
  33. /*   None.                                                                                                 */
  34. /*                                                                                                         */
  35. /* Returns:                                                                                                */
  36. /*   ADC code of AVDD voltage.                                                                             */
  37. /*                                                                                                         */
  38. /* Description:                                                                                            */
  39. /*   Get ADC conversion result of Band-gap voltage.                                                        */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. uint32_t GetAVDDCodeByADC(void)
  42. {
  43.     uint32_t u32Count, u32Sum, u32Data;
  44.     /* Enable ADC module clock */
  45.     CLK_EnableModuleClock(ADC_MODULE);
  46.                
  47.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  48.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(22));

  49.     /* Configure ADC: single-end input, single scan mode, enable ADC analog circuit. */
  50.     ADC_Open(ADC, NULL, ADC_ADCR_ADMD_SINGLE, BIT7);
  51.     /* Configure the analog input source of channel 7 as internal band-gap voltage */
  52.     ADC_CONFIG_CH7(ADC, ADC_ADCHER_PRESEL_INT_BANDGAP);

  53.     /* Power on ADC */
  54.     ADC_POWER_ON(ADC);

  55.     /* Clear conversion finish flag */
  56.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  57.     /* Enable ADC conversion finish interrupt */
  58.     ADC_EnableInt(ADC, ADC_ADF_INT);
  59.     NVIC_EnableIRQ(ADC_IRQn);

  60.     g_u8ADF = 0;
  61.     u32Sum = 0;

  62.     /* sample times are according to ADC_SAMPLE_COUNT definition */
  63.     for(u32Count = 0; u32Count < ADC_SAMPLE_COUNT; u32Count++)
  64.     {
  65.         /* Delay for band-gap voltage stability */
  66.         CLK_SysTickDelay(1000);

  67.         /* Start A/D conversion */
  68.         ADC_START_CONV(ADC);

  69.         u32Data = 0;
  70.         /* Wait conversion done */
  71.         while(g_u8ADF == 0);
  72.         g_u8ADF = 0;
  73.         /* Get the conversion result */
  74.         u32Data = ADC_GET_CONVERSION_DATA(ADC, 7);
  75.         /* Sum each conversion data */
  76.         u32Sum += u32Data;
  77.     }
  78.     /* Disable ADC interrupt */
  79.     ADC_DisableInt(ADC, ADC_ADF_INT);
  80.     /* Disable ADC */
  81.     ADC_POWER_DOWN(ADC);

  82.     /* Return the average of ADC_SAMPLE_COUNT samples */
  83.     return (u32Sum >> 7);
  84.                
  85. }

  86. /*---------------------------------------------------------------------------------------------------------*/
  87. /* Function: GetAVDDVoltage                                                                                */
  88. /*                                                                                                         */
  89. /* Parameters:                                                                                             */
  90. /*   None.                                                                                                 */
  91. /*                                                                                                         */
  92. /* Returns:                                                                                                */
  93. /*   AVDD voltage(mV).                                                                                     */
  94. /*                                                                                                         */
  95. /* Description:                                                                                            */
  96. /*   Use Band-gap voltage to calculate AVDD voltage                                                        */
  97. /*---------------------------------------------------------------------------------------------------------*/
  98. uint32_t GetAVDDVoltage(void)
  99. {
  100.     uint32_t  u32ConversionResult;
  101.     uint64_t u64MvAVDD;

  102.     /* Calculate Vref by using conversion result of VBG */
  103.     u32ConversionResult = GetAVDDCodeByADC();

  104.     /* u32ConversionResult = VBG * 1024 / Vref, Vref = AVDD */
  105.     /* => AVDD = VBG * 4096 / u32ConversionResult */
  106.     u64MvAVDD = (VBG_VOLTAGE << 12) / (uint64_t)u32ConversionResult;

  107.     //printf("Conversion result: 0x%X\n", u32ConversionResult);

  108.     return (uint32_t)u64MvAVDD;
  109. }


  110. /*---------------------------------------------------------------------------------------------------------*/
  111. /*  MAIN function                                                                                          */
  112. /*---------------------------------------------------------------------------------------------------------*/
  113. int main(void)
  114. {
  115.      uint32_t u32AVDDVoltage;
  116.     //Initial System
  117.     SYS_Init();
  118.         UART0_Init();
  119.     /* Measure AVDD */
  120. u32AVDDVoltage = GetAVDDVoltage();
  121.     printf("AVDD Voltage: %dmV\n", u32AVDDVoltage);
  122.   
  123.     while(1);
  124. }


huangcunxiake 发表于 2016-12-31 10:38 | 显示全部楼层
这个例程看起来,ADC的用法比其他系统的还有简单。头文件做的很好
mintspring 发表于 2017-1-2 16:21 | 显示全部楼层
/* Clear conversion finish flag */
    ADC_CLR_INT_FLAG(ADC, u32Flag);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

310

帖子

6

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