/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.0
* $Revision: 5 $
* $Date: 14/01/28 11:44a $
* [url=home.php?mod=space&uid=247401]@brief[/url] M051 Series ADC Interface Controller Driver Sample Code
*
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#define PLL_CLOCK 50000000
#define VBG 1.20
#define Offset 724
#define Gain -1.75
/*---------------------------------------------------------------------------------------------------------*/
/* Define Function Prototypes */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void);
void UART0_Init(void);
void AdcContScanModeTest(void);
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC 22.1184MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for Internal RC clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
/* Enable external XTAL 12MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
/* Waiting for external XTAL clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable ADC module clock */
CLK_EnableModuleClock(ADC_MODULE);
/* Select UART module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
/* ADC clock source is 22.1184MHz, set divider to 100, ADC clock is 22.1184/100 MHz */
CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(100));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_Init()
{
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: AdcContScanModeTest */
/* */
/* Parameters: */
/* None. */
/* */
/* Returns: */
/* None. */
/* */
/* Description: */
/* ADC continuous scan mode test. */
/*---------------------------------------------------------------------------------------------------------*/
void AdcContScanModeTest()
{
int32_t i32ConversionData;
double Vref;
double TempData;
printf("+----------------------------------------------------------------------+\n");
printf("| ADC for temperature sensor sample code |\n");
printf("+----------------------------------------------------------------------+\n");
printf("\nIn this test, software will get value from temperature sensor.\n");
/* Enable Tempreture Sensor function */
SYS->TEMPCR = SYS_TEMPCR_VTEMP_EN_Msk;
/* Set the ADC operation mode as continuous scan, input mode as single-end and
enable the analog input channel 7 */
ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, 0x80);
/* Configure the analog input source of channel 7 */
ADC_CONFIG_CH7(ADC, ADC_ADCHER_PRESEL_INT_BANDGAP);
/* Power on ADC module */
ADC_POWER_ON(ADC);
/* clear the A/D interrupt flag for safe */
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
/* start A/D conversion */
ADC_START_CONV(ADC);
while(1)
{
/* Wait conversion done */
while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT));
/* Stop A/D conversion */
ADC_STOP_CONV(ADC);
/* clear the A/D interrupt flag for safe */
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
/* Calculate Vref by using conversion result of VBG */
/* ConversionData = VBG * 4096 / Vref */
i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 7);
Vref = VBG * 4096 / (double)i32ConversionData;
/* Delay */
CLK_SysTickDelay(500000);
/* Configure the analog input source of channel 7 */
ADC_CONFIG_CH7(ADC, ADC_ADCHER_PRESEL_INT_TEMPERATURE_SENSOR);
/* start A/D conversion */
ADC_START_CONV(ADC);
/* Wait conversion done */
while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT));
/* Stop A/D conversion */
ADC_STOP_CONV(ADC);
/* clear the A/D interrupt flag for safe */
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
/* Calculate value from temperature sensor */
/* ConversionData = Vtemp(V) * 4096 / Vref */
/* Vtemp(mV) = Gain * Temperature + Offset */
i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 7);
TempData = (((double)i32ConversionData * Vref / 4096) * 1000 - Offset) / Gain;
printf("Conversion result of channel %d: 0x%X (%.2f oC)\n", 7, i32ConversionData, TempData);
/* Delay */
CLK_SysTickDelay(500000);
/* Configure the analog input source of channel 7 */
ADC_CONFIG_CH7(ADC, ADC_ADCHER_PRESEL_INT_BANDGAP);
/* start A/D conversion */
ADC_START_CONV(ADC);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
printf("\nSystem clock rate: %d Hz\n", SystemCoreClock);
/* Continuous scan mode test */
AdcContScanModeTest();
/* Disable ADC module */
ADC_Close(ADC);
/* Disable ADC IP clock */
CLK_DisableModuleClock(ADC_MODULE);
/* Disable External Interrupt */
NVIC_DisableIRQ(ADC_IRQn);
printf("\nExit ADC sample code\n");
while(1);
}
|