EC_Nano100_Use_BandGap_Measure_VDD_And_Temperature_V1.00.zip
(4.98 MB)
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 6 $
* $Date: 14/05/08 4:24p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrate measure internal temperature and supply voltage of IC using internal BandGap 2.5V voltage.
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano100Series.h"
volatile float fTmp = 0;
volatile float fAvdd = 0;
void ADC_IRQHandler(void);
void ADC_IRQHandler(void)
{
uint32_t u32Flag;
// Get ADC conversion finish interrupt flag
u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT);
if (u32Flag & ADC_ADF_INT)
{
uint32_t u32Result;
u32Result = ADC_GET_CONVERSION_DATA(ADC, 15);
fAvdd = 2.5 * 0xfff / u32Result;
printf(" Channel 15 conversion result is %d, VDD is %f volt \n", u32Result, fAvdd);
u32Result = ADC_GET_CONVERSION_DATA(ADC, 14);
fTmp = (740 - (1000 * u32Result / 4096) * fAvdd) / 1.73 ;
printf(" Channel 14 conversion result is %d, Temperature is %f degree \n", u32Result, fTmp);
}
ADC_CLR_INT_FLAG(ADC, u32Flag);
}
void SYS_EnableTempCtl()
{
SYS->TEMPCTL |= (SYS_TEMPCTL_VTEMP_EN_Msk);
}
void SYS_DisableTempCtl()
{
SYS->TEMPCTL = ((SYS->TEMPCTL & ~0x1));
}
void SYS_EnableIntVRefBGP(uint8_t iValue)
{
if (iValue)
SYS->Int_VREFCTL = ((SYS->Int_VREFCTL & ~0x7) | (SYS_VREFCTL_BGP_EN_Msk | SYS_VREFCTL_REG_EN_Msk | 0x4)); // BGP 2.5V
else
SYS->Int_VREFCTL = ((SYS->Int_VREFCTL & ~0x7) | (SYS_VREFCTL_BGP_EN_Msk | SYS_VREFCTL_REG_EN_Msk)); // BGP 1.8V
}
void SYS_DisableIntRefBGP()
{
SYS->Int_VREFCTL &= ~0x7 ;
}
void ADC_SetVrefVoltage(uint8_t iValue)
{
ADC->CR &= ~ADC_CR_REFSEL_Msk;
if (iValue == 1) // Select Int_Vref as Voltage
{
ADC->CR |= (0x1 << ADC_CR_REFSEL_Pos);
}
else if (iValue == 2) // Select Vref as Voltage
{
ADC->CR |= (0x2 << ADC_CR_REFSEL_Pos);
}
else // default Select Power as Voltage
{
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Set HCLK source form HXT and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_HCLK_CLK_DIVIDER(1));
/* Enable external 12MHz HXT, 32KHz LXT and HIRC */
CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk | CLK_PWRCTL_LXT_EN_Msk | CLK_PWRCTL_HIRC_EN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_HXT_STB_Msk | CLK_CLKSTATUS_LXT_STB_Msk | CLK_CLKSTATUS_HIRC_STB_Msk);
/* Set HCLK frequency 42MHz */
CLK_SetCoreClock(32000000);
/* Enable IP clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable ADC clock */
CLK_EnableModuleClock(ADC_MODULE);
/* Select IP clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_UART_CLK_DIVIDER(1));
CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HXT, CLK_ADC_CLK_DIVIDER(200));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD and TXD */
SYS->PB_L_MFP &= ~(SYS_PB_L_MFP_PB0_MFP_Msk | SYS_PB_L_MFP_PB1_MFP_Msk);
SYS->PB_L_MFP |= (SYS_PB_L_MFP_PB0_MFP_UART0_RX | SYS_PB_L_MFP_PB1_MFP_UART0_TX);
/* Set PB multi-function pins for Clock Output */
SYS->PB_H_MFP = (SYS->PB_H_MFP & ~SYS_PB_H_MFP_PB12_MFP_Msk) | SYS_PB_H_MFP_PB12_MFP_CKO;
/* Set PA.0 multi-function pin for ADC channel 0 */
SYS->PA_L_MFP = (SYS->PA_L_MFP & ~SYS_PA_L_MFP_PA0_MFP_Msk) | SYS_PA_L_MFP_PA0_MFP_ADC_CH0;
/* Disable PA.0 digital input path */
PA->OFFD |= ((1 << 0) << GP_OFFD_OFFD_Pos);
SYS_EnableTempCtl(); // Enable Temperature Sensor
SYS_EnableIntVRefBGP(1); // Enable Vref is BGP
/* Lock protected registers */
SYS_LockReg();
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
UART_Open(UART0, 115200);
}
int32_t main(void)
{
//uint32_t u32Result;
/* Init System, IP clock and multi-function I/O
In the end of SYS_Init() will issue SYS_LockReg()
to lock protected register. If user want to write
protected register, please issue SYS_UnlockReg()
to unlock protected register if necessary */
SYS_Init();
/* Init UART0 for printf */
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
printf("\n This sample code demonstrate measure VDD and Temperature.\n");
printf(" It convert ADC channel 14, 15 and print conversion result \n");
printf(" ADC channel 15 is VDD \n");
printf(" ADC channel 14 is Temperature \n\n");
// Set ADC
ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_SINGLE_CYCLE, ADC_CH_14_MASK | ADC_CH_15_MASK);
ADC_SetExtraSampleTime(ADC, ADC_CH_14_MASK, 1024);
ADC_SetVrefVoltage(0); // Set Vref voltage
// Power on ADC
ADC_POWER_ON(ADC);
// Enable ADC ADC_IF interrupt
ADC_EnableInt(ADC, ADC_ADF_INT);
NVIC_EnableIRQ(ADC_IRQn);
ADC_START_CONV(ADC);
while (1);
}
/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
|