/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Website: http://www.nuvoton.com
// E-Mail : MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//***********************************************************************************************************
// File Function: ML51 series use bandgap result to calculate VDD demo code
//***********************************************************************************************************
#include "ML51.h"
double VDD_Voltage,Bandgap_Voltage; /*please always use "double" mode for this */
unsigned char xdata ADCdataH[5], ADCdataL[5];
unsigned int ADCsumH=0, ADCsumL=0;
/******************************************************************************
The main C function. Program execution starts
here after stack initialization.
******************************************************************************/
void main (void)
{
unsigned int ADC_BG_Result;
MFP_P31_UART0_TXD; /* UART0 define @P0.5 & P0.6 multi function setting */
P31_QUASI_MODE;
UART_Open(24000000,UART0_Timer3,115200); /* set UART0 output, baudrate 115200 generate by timer1 */
ENABLE_UART0_PRINTF;;
/* ADC bandgap initial
* include adc.c in Library for initial setting
*/
ADC_Open(ADC_SINGLE,VBG);
/* For the best result wait 10us delay for each sampling, ADCDIV=3, ADCAQT=7 is better */
ADC_ConvertTime(3,7);
// ADC_Calibration();
while (1)
{
clr_ADCCON0_ADCF;
set_ADCCON0_ADCS;
while(!(ADCCON0&SET_BIT7));
ADC_BG_Result = (ADCRH<<4) + ADCRL;
printf ("\n ADC_BG_Result = 0x%02X",ADC_BG_Result);
/***************************************************************************************/
/* VDD Now READ_BANDGAP() VALUE
/* ------------------------------------- = ----------------------------------
/* 3072mV(Storage value test condition) NOW ADC Bandgap convert reuslt
/***************************************************************************************/
VDD_Voltage = ((float)READ_BANDGAP()/(float)ADC_BG_Result)*3.072;
printf ("\n VDD_Voltage = %e",VDD_Voltage);
/***************************************************************************************/
/* Bandgap real voltage READ_BANDGAP() VALUE
/* ------------------------------------- = ----------------------------------
/* 3072mV(Storage value test condition) 4096(12bit ADC)
/***************************************************************************************/
Bandgap_Voltage = ((float)READ_BANDGAP()*3/4/1000);
printf ("\n Bandgap_Voltage = %e", Bandgap_Voltage);
ADCsumH = 0;
ADCsumL = 0;
printf ("\n " );
printf ("\n " );
}
}
|