[牛人杂谈] 内部参考电压

[复制链接]
2105|11
 楼主| 天灵灵地灵灵 发表于 2020-7-4 22:50 | 显示全部楼层 |阅读模式
带隙电压(VBG)是内部固定基准电压,无论电源如何变化。 VBG的输出内部连接到ADC通道多路复用器和模拟比较器(ACMP)负输入端。因此可以通过VBG的A / D转换结果来计算AVDD的电压。
当AVDD直接连接到电源时,VBG可用于计算AVDD电压。与传统分压电路相比,使用VBG可以减少外部元件和功耗。
由于工艺变化,每个芯片的VBG可能略有不同。因此,计算出的AVDD可能会有一些偏差。 M031 /M032系列芯片出厂时,都具有内置的VBG A / D转换值(当AVDD = 3072 mV时)。利用内置的VBG A / D转换值和当前的VBG A / D转换值,用户可以更准确地计算AVDD,与仅使用VBG A / D转换值相比来说更为准确。
130485f0096dd27ad6.png
  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 calculate battery voltage( AVdd ) by using band-gap.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2018 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. volatile uint32_t g_u32BandGapConvValue;

  16. /**
  17.   * @brief      Read Built-in Band-Gap conversion value
  18.   * @param[in]  None
  19.   * [url=home.php?mod=space&uid=266161]@return[/url]     Built-in Band-Gap conversion value
  20.   * [url=home.php?mod=space&uid=1543424]@Details[/url]    This function is used to read Band-Gap conversion value.
  21.   */
  22. __STATIC_INLINE uint32_t FMC_ReadBandGap(void)
  23. {
  24.     FMC->ISPCMD = FMC_ISPCMD_READ_UID;            /* Set ISP Command Code */
  25.     FMC->ISPADDR = 0x70u;                         /* Must keep 0x70 when read Band-Gap */
  26.     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;           /* Trigger to start ISP procedure */
  27. #if ISBEN
  28.     __ISB();
  29. #endif                                            /* To make sure ISP/CPU be Synchronized */
  30.     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {}  /* Waiting for ISP Done */

  31.     return FMC->ISPDAT & 0xFFF;
  32. }

  33. void SYS_Init(void)
  34. {
  35.     /* Unlock protected registers */
  36.     SYS_UnlockReg();

  37.     /* Enable HIRC clock (Internal RC 48 MHz) */
  38.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  39.     /* Wait for HIRC clock ready */
  40.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

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

  46.     /* Switch UART0 clock source to HIRC */
  47.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
  48.     /* Switch ADC clock source to HIRC, set divider to 2, ADC clock is 48/2 MHz */
  49.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(2));

  50.     /* Update System Core Clock */
  51.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  52.     SystemCoreClockUpdate();

  53.     /*----------------------------------------------------------------------*/
  54.     /* Init I/O Multi-function                                              */
  55.     /*----------------------------------------------------------------------*/

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

  59.     /* Lock protected registers */
  60.     SYS_LockReg();
  61. }

  62. void ADC_FunctionTest()
  63. {
  64.     int32_t  i32ConversionData;
  65.     int32_t  i32BuiltInData;

  66.     printf("\n");
  67.     printf("+----------------------------------------------------------------------------+\n");
  68.     printf("|     ADC for calculate battery voltage( AVdd ) by using band-gap test       |\n");
  69.     printf("+----------------------------------------------------------------------------+\n\n");

  70.     printf("+----------------------------------------------------------------------+\n");
  71.     printf("|   ADC clock source -> PCLK1  = 48 MHz                                |\n");
  72.     printf("|   ADC clock divider          = 2                                     |\n");
  73.     printf("|   ADC clock                  = 48 MHz / 2 = 24 MHz                   |\n");
  74.     printf("|   ADC extended sampling time = 71                                    |\n");
  75.     printf("|   ADC conversion time = 17 + ADC extended sampling time = 88         |\n");
  76.     printf("|   ADC conversion rate = 24 MHz / 88 = 272.7 ksps                     |\n");
  77.     printf("+----------------------------------------------------------------------+\n");

  78.     /* Enable ADC converter */
  79.     ADC_POWER_ON(ADC);

  80.     /* Set input mode as single-end, Single mode, and select channel 29 (band-gap voltage) */
  81.     ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, BIT29);

  82.     /* To sample band-gap precisely, the ADC capacitor must be charged at least 3 us for charging the ADC capacitor ( Cin )*/
  83.     /* Sampling time = extended sampling time + 1 */
  84.     /* 1/24000000 * (Sampling time) = 3 us */
  85.     ADC_SetExtendSampleTime(ADC, 0, 71);

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

  88.     /* Enable the sample module interrupt.  */
  89.     ADC_ENABLE_INT(ADC, ADC_ADF_INT);   // Enable sample module A/D interrupt.
  90.     NVIC_EnableIRQ(ADC_IRQn);

  91.     printf(" Press any key to start the test\n\n");
  92.     getchar();

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

  96.     /* Wait ADC conversion done */
  97.     while(g_u32AdcIntFlag == 0);

  98.     /* Disable the A/D interrupt */
  99.     ADC_DISABLE_INT(ADC, ADC_ADF_INT);

  100.     /* Get the conversion result of the channel 29 */
  101.     i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 29);


  102.     /* Enable FMC ISP function to read built-in band-gap A/D conversion result*/
  103.     SYS_UnlockReg();
  104.     FMC_Open();
  105.     i32BuiltInData = FMC_ReadBandGap();

  106.     /* Use Conversion result of Band-gap to calculating AVdd */

  107.     printf("      AVdd           i32BuiltInData                   \n");
  108.     printf("   ---------- = -------------------------             \n");
  109.     printf("      3072          i32ConversionData                 \n");
  110.     printf("                                                      \n");
  111.     printf("AVdd =  3072 * i32BuiltInData / i32ConversionData     \n\n");

  112.     printf("Built-in band-gap A/D conversion result: 0x%X (%d) \n", i32BuiltInData, i32BuiltInData);
  113.     printf("Conversion result of Band-gap:           0x%X (%d) \n\n", i32ConversionData, i32ConversionData);

  114.     printf("AVdd = 3072 * %d / %d = %d mV \n\n", i32BuiltInData, i32ConversionData, 3072*i32BuiltInData/i32ConversionData);
  115. }

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

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

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

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

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

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

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

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

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

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

  145.     while(1);
  146. }



小灵通2018 发表于 2020-7-5 14:00 | 显示全部楼层
认真学习学习
xinpian101 发表于 2020-7-5 15:36 | 显示全部楼层
内部参考电压非常有用。
jiekou001 发表于 2020-7-5 23:16 | 显示全部楼层
还要先读取唯一ID才能行啊
小灵通2018 发表于 2020-7-5 23:29 | 显示全部楼层
这个关系挺好,应该将那个内部的作为一个函数,直接调用。
SteveDing 发表于 2020-7-9 11:46 | 显示全部楼层
M0\M4的内部参考电压配置需要解锁
稳稳の幸福 发表于 2020-7-9 17:25 | 显示全部楼层
用过,非常给力
coshi 发表于 2020-8-3 17:37 | 显示全部楼层
非常感谢楼主分享
aoyi 发表于 2020-8-3 17:38 | 显示全部楼层
第一次了解vbg
drer 发表于 2020-8-3 17:38 | 显示全部楼层
支持楼主 呵呵
gwsan 发表于 2020-8-3 17:38 | 显示全部楼层
长知识了 很好
kxsi 发表于 2020-8-3 17:39 | 显示全部楼层
解释的很透彻
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

3475

帖子

13

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