[DemoCode下载] M091的带隙基准读取

[复制链接]
1203|2
 楼主| yiyigirl2014 发表于 2023-11-12 22:59 | 显示全部楼层 |阅读模式
  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]    Convert Band-gap (channel 29) and print conversion result.
  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/1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  25.     /* Set both PCLK0 and PCLK1 as HCLK/1 */
  26.     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV1);

  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 2 */
  34.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(2));

  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.     /* Lock protected registers */
  45.     SYS_LockReg();
  46. }

  47. void ADC_FunctionTest()
  48. {
  49.     int32_t  i32ConversionData;

  50.     printf("\n");
  51.     printf("+----------------------------------------------------------------------+\n");
  52.     printf("|                  ADC for Band-gap test                               |\n");
  53.     printf("+----------------------------------------------------------------------+\n");

  54.     printf("+----------------------------------------------------------------------+\n");
  55.     printf("|   ADC clock source -> PCLK1  = 48 MHz                                |\n");
  56.     printf("|   ADC clock divider          = 2                                     |\n");
  57.     printf("|   ADC clock                  = 48 MHz / 2 = 24 MHz                   |\n");
  58.     printf("|   ADC extended sampling time = 71                                    |\n");
  59.     printf("|   ADC conversion time = 17 + ADC extended sampling time = 88         |\n");
  60.     printf("|   ADC conversion rate = 24 MHz / 88 = 272.7 ksps                     |\n");
  61.     printf("+----------------------------------------------------------------------+\n");

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

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

  66.     /* To sample band-gap precisely, the ADC capacitor must be charged at least 3 us for charging the ADC capacitor ( Cin )*/
  67.     /* Sampling time = extended sampling time + 1 */
  68.     /* 1/24000000 * (Sampling time) = 3 us */
  69.     /* Set sample module external sampling time to 71 */
  70.     ADC_SetExtendSampleTime(ADC, 0, 71);

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

  73.     /* Enable the sample module interrupt.  */
  74.     ADC_ENABLE_INT(ADC, ADC_ADF_INT);   /* Enable sample module A/D interrupt. */
  75.     NVIC_EnableIRQ(ADC_IRQn);

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

  79.     /* Wait ADC conversion done */
  80.     while(g_u32AdcIntFlag == 0);

  81.     /* Disable the A/D interrupt */
  82.     ADC_DISABLE_INT(ADC, ADC_ADF_INT);

  83.     /* Get the conversion result of the channel 29 */
  84.     i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 29);
  85.     printf("ADC Conversion result of Band-gap: 0x%X (%d)\n", i32ConversionData, i32ConversionData);
  86.     printf("Band-gap voltage is %dmV if Reference voltage is 3.3V\n", (3300*i32ConversionData)/4095);
  87. }

  88. void ADC_IRQHandler(void)
  89. {
  90.     g_u32AdcIntFlag = 1;
  91.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Clear the A/D interrupt flag */
  92. }

  93. /*----------------------------------------------------------------------*/
  94. /* Init UART0                                                           */
  95. /*----------------------------------------------------------------------*/
  96. void UART0_Init(void)
  97. {
  98.     /* Reset UART0 */
  99.     SYS_ResetModule(UART0_RST);

  100.     /* Configure UART0 and set UART0 baud rate */
  101.     UART_Open(UART0, 115200);
  102. }

  103. int32_t main(void)
  104. {
  105.     /* Init System, IP clock and multi-function I/O. */
  106.     SYS_Init();

  107.     /* Init UART0 for printf */
  108.     UART0_Init();

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

  110.     /* ADC function test */
  111.     ADC_FunctionTest();

  112.     /* Disable ADC IP clock */
  113.     CLK_DisableModuleClock(ADC_MODULE);

  114.     /* Disable External Interrupt */
  115.     NVIC_DisableIRQ(ADC_IRQn);

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

  117.     while(1);
  118. }


huangcunxiake 发表于 2023-11-30 19:58 | 显示全部楼层
用于测量系统温度还是很不错的。
EmmaTT 发表于 2023-12-6 10:37 来自手机 | 显示全部楼层
这个是干嘛用的啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

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