[DemoCode下载] 如何通过使用片上基准电压(band-gap)来计算电池电压(AVdd)

[复制链接]
1193|4
 楼主| 小明的同学 发表于 2024-2-19 21:38 | 显示全部楼层 |阅读模式
  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. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NuMicro.h"


  12. /*---------------------------------------------------------------------------------------------------------*/
  13. /* Define global variables and constants                                                                   */
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. volatile uint32_t g_u32AdcIntFlag;
  16. volatile uint32_t g_u32BandGapConvValue;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  146.     while(1);
  147. }


 楼主| 小明的同学 发表于 2024-2-19 21:39 | 显示全部楼层
首先,在 SYS_Init 函数中进行了系统初始化的配置,包括解锁保护寄存器、启用内部的48MHz的高精度时钟(HIRC),配置时钟源、时钟分频、模块时钟,以及初始化 UART0 模块作为 printf 的输出通道。

在 ADC_FunctionTest 函数中,首先打印了一些关于 ADC 的配置信息,然后启用 ADC 模块,并设置 ADC 的工作模式和采样通道为单端模式和片上基准电压通道(channel 29),同时设置 ADC 的采样时间为扩展采样时间(extended sampling time)加1,用于确保对片上基准电压的准确采样。

接着清除了 ADC 的中断标志位,并启用了 ADC 的转换完成中断,并在主程序中注册了 ADC 的中断处理函数 ADC_IRQHandler。然后程序等待用户按下任意键开始 ADC 转换。

当用户按下键后,程序启动 ADC 转换,并进入循环等待 ADC 转换完成的标志位 g_u32AdcIntFlag 被置位。

当 ADC 转换完成后,中断服务程序 ADC_IRQHandler 会将转换完成标志位 g_u32AdcIntFlag 设置为1,并清除 ADC 转换完成中断标志位。

在转换完成后,程序会读取片上基准电压的 ADC 转换值,并根据片上基准电压和 ADC 转换的采样值计算出电池电压 AVdd 的值,并打印出来。

最后,在 main 函数中进行了系统初始化,初始化了 UART0,并通过 printf 打印系统时钟频率信息,然后调用了 ADC 函数测试,最后关闭了 ADC 模块的时钟,并禁用了 ADC 的中断,并进入了一个无限循环。
wahahaheihei 发表于 2024-2-27 11:53 | 显示全部楼层
就是按照比例 关系实现测量未知,由一个已知的计算未知的。
21mengnan 发表于 2024-2-27 21:34 | 显示全部楼层
可以评估电池剩余电量。
21mengnan 发表于 2024-2-27 21:34 | 显示全部楼层
不过电池的电量与电压是非线性的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

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