/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 2 $
* $Date: 15/08/18 11:54a $
* [url=home.php?mod=space&uid=247401]@brief[/url] NuEdu Basic01 Volume Knob Sample Code
*
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include "NuEdu-Basic01.h"
volatile uint8_t g_u8ADF;
#define VBG_VOLTAGE (1250) /* 1.20V = 1200 mV (Typical band-gap voltage) */
//#define ADC_SAMPLE_COUNT 128 /* The last line of GetAVDDCodeByADC() need revise when ADC_SAMPLE_COUNT is changed. */
#define ADC_SAMPLE_COUNT 128 /* The last line of GetAVDDCodeByADC() need revise when ADC_SAMPLE_COUNT is changed. */
void ADC_IRQHandler(void)
{
uint32_t u32Flag;
/* Get ADC conversion finish interrupt flag */
u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT);
/* Check ADC conversion finish */
if(u32Flag & ADC_ADF_INT)
g_u8ADF = 1;
/* Clear conversion finish flag */
ADC_CLR_INT_FLAG(ADC, u32Flag);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: GetAVDDCodeByADC */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* ADC code of AVDD voltage. */
/* */
/* Description: */
/* Get ADC conversion result of Band-gap voltage. */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t GetAVDDCodeByADC(void)
{
uint32_t u32Count, u32Sum, u32Data;
/* Enable ADC module clock */
CLK_EnableModuleClock(ADC_MODULE);
/* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(22));
/* Configure ADC: single-end input, single scan mode, enable ADC analog circuit. */
ADC_Open(ADC, NULL, ADC_ADCR_ADMD_SINGLE, BIT7);
/* Configure the analog input source of channel 7 as internal band-gap voltage */
ADC_CONFIG_CH7(ADC, ADC_ADCHER_PRESEL_INT_BANDGAP);
/* Power on ADC */
ADC_POWER_ON(ADC);
/* Clear conversion finish flag */
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
/* Enable ADC conversion finish interrupt */
ADC_EnableInt(ADC, ADC_ADF_INT);
NVIC_EnableIRQ(ADC_IRQn);
g_u8ADF = 0;
u32Sum = 0;
/* sample times are according to ADC_SAMPLE_COUNT definition */
for(u32Count = 0; u32Count < ADC_SAMPLE_COUNT; u32Count++)
{
/* Delay for band-gap voltage stability */
CLK_SysTickDelay(1000);
/* Start A/D conversion */
ADC_START_CONV(ADC);
u32Data = 0;
/* Wait conversion done */
while(g_u8ADF == 0);
g_u8ADF = 0;
/* Get the conversion result */
u32Data = ADC_GET_CONVERSION_DATA(ADC, 7);
/* Sum each conversion data */
u32Sum += u32Data;
}
/* Disable ADC interrupt */
ADC_DisableInt(ADC, ADC_ADF_INT);
/* Disable ADC */
ADC_POWER_DOWN(ADC);
/* Return the average of ADC_SAMPLE_COUNT samples */
return (u32Sum >> 7);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: GetAVDDVoltage */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* AVDD voltage(mV). */
/* */
/* Description: */
/* Use Band-gap voltage to calculate AVDD voltage */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t GetAVDDVoltage(void)
{
uint32_t u32ConversionResult;
uint64_t u64MvAVDD;
/* Calculate Vref by using conversion result of VBG */
u32ConversionResult = GetAVDDCodeByADC();
/* u32ConversionResult = VBG * 1024 / Vref, Vref = AVDD */
/* => AVDD = VBG * 4096 / u32ConversionResult */
u64MvAVDD = (VBG_VOLTAGE << 12) / (uint64_t)u32ConversionResult;
//printf("Conversion result: 0x%X\n", u32ConversionResult);
return (uint32_t)u64MvAVDD;
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
uint32_t u32AVDDVoltage;
//Initial System
SYS_Init();
UART0_Init();
/* Measure AVDD */
u32AVDDVoltage = GetAVDDVoltage();
printf("AVDD Voltage: %dmV\n", u32AVDDVoltage);
while(1);
}
|