/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* Writed by Nuvoton(SH) CF10. 2013/02/25 */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvGPIO.h"
#include "DrvFMC.h"
#include "DrvSYS.h"
#include "DrvPWM.h"
#include "DrvADC.h"
#include "DrvTIMER.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
void TMR0_Callback(uint32_t u32Param)
{
GPIOC->DOUT ^= (1 << 3); //定时器中断取反IO口
}
void CalPeriodTime(uint8_t u8Capture)
{
uint16_t u32Count[4];
uint32_t u32i;
uint16_t u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid;
/* Clear the Capture Interrupt Flag */
DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);
/* Wait for Interrupt Flag (Falling) */
while (DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG) != 1);
/* Clear the Capture Interrupt Flag */
DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);
u32i = 0;
while (u32i < 4)
{
/* Wait for Interrupt Flag (Falling) */
while(DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG) != 1);
/* Clear the Capture Interrupt Flag */
DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);
/* Clear the Capture Rising Interrupt Flag */
DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG);
/* Get the Falling Counter Data */
u32Count[u32i++] = DrvPWM_GetFallingCounter(u8Capture);
/* Wait for Capture Rising Interrupt Flag */
while(DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG) != 1);
/* Clear the Capture Rising Interrupt Flag */
DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG);
/* Get the Rising Counter Data */
u32Count[u32i++] = DrvPWM_GetRisingCounter(u8Capture);
}
//因为使能了下降沿中断,所以下降沿会复位计数器的值,上升沿不会复位计数器
u16RisingTime = u32Count[1]; //检测到上升沿时计数器的值
u16FallingTime = u32Count[0]; //检测到下降沿时计数器的值
u16HighPeroid = u32Count[1] - u32Count[2]; //高电平的时间
u16LowPeroid = 0x10000 - u32Count[1]; //低电平的时间
u16TotalPeroid = 0x10000 - u32Count[2]; //周期时间
printf("Capture Test Result:\nRising Time = %d, Falling Time = %d.\nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
}
void Test_TIMER(void)
{
DrvTIMER_Init();
DrvTIMER_Open(E_TMR0, 4, E_PERIODIC_MODE); //配置定时器为周期模式
DrvTIMER_SetTimerEvent(E_TMR0, 2, (TIMER_CALLBACK)TMR0_Callback, 1);//配置每秒中断两次
DrvTIMER_EnableInt(E_TMR0); //使能定时器中断
DrvTIMER_Start(E_TMR0);
}
void Test_PWM_CAP(void) //需要PWM0和PWM3连在一起
{
uint8_t u8Timer, u8CapTimer;
S_DRVPWM_TIME_DATA_T sPt;
/* Enable PWM clock */
DrvPWM_Open();
/* Set PWM pins */
DrvGPIO_InitFunction(E_FUNC_PWM01);
DrvGPIO_InitFunction(E_FUNC_PWM23);
/* PWM Timer property */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; //配置为自动重载初值模式
sPt.u32Frequency = 1000; //PWM0输出频率1K
sPt.u8HighPulseRatio = 30; //占空比30%
sPt.i32Inverter = 0; //输出不反向
u8Timer = DRVPWM_TIMER0; //PWM0
/* Select PWM engine clock */
DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(u8Timer, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(u8Timer, 1);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(u8Timer, 1);
/*--------------------------------------------------------------------------------------*/
/* Set the PWM Capture 3 for capture function */
/*--------------------------------------------------------------------------------------*/
/* PWM Timer property for Capture */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u32Frequency = 100; /* Set the proper frequency to capture data (Less than the input data)*/
sPt.u8HighPulseRatio = 50; /* High Pulse peroid : Total Pulse peroid = 50 : 100 (Set a non-zero value) */
sPt.u32Duty = 0x10000; //计数器初值/* Set the counter to the maximum value */
sPt.i32Inverter = 0;
u8CapTimer = DRVPWM_CAP3; //配置PWM3作为捕获通道
/* Select PWM engine clock */
DrvPWM_SelectClockSource(u8CapTimer, DRVPWM_EXT_12M);//选择外部12M晶振做时钟源
/* Set PWM Timer 3 for Capture */
DrvPWM_SetTimerClk(u8CapTimer, &sPt);
/* Enable Interrupt Sources of PWM Capture3 */
DrvPWM_EnableInt(u8CapTimer, DRVPWM_CAP_FALLING_INT, NULL);//使能下降沿中断
/* Enable Input function for PWM Capture 3 */
DrvPWM_SetTimerIO(u8CapTimer, 1);
/* Enable the PWM Capture3 */
DrvPWM_Enable(u8CapTimer, 1);
/* Capture the Input Waveform Data */
CalPeriodTime(u8CapTimer); //计算捕获时间
}
void Test_ADC(void)
{
int32_t i32ConversionData;
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0, EXTERNAL_12MHZ, 1);
/* Disable the digital input path */
DrvGPIO_DisableDigitalInputBit(E_GPA, 0); //关闭ADC0数字输入通道
/* Configure the corresponding ADC analog input pin */
DrvGPIO_InitFunction(E_FUNC_ADC0); //使能ADC0管脚功能
DrvADC_SetADCChannel(1); //使能ADC0通道
while(1)
{
/* start A/D conversion */
DrvADC_StartConvert();
/* Wait conversion done */
while(DrvADC_IsConversionDone()==FALSE);
ADC->ADSR.ADF=1; //清转换完成标志
i32ConversionData = DrvADC_GetConversionData(0); //读取转换结果
printf("Conversion result: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData);
DrvSYS_Delay(200000);
}
}
void Test_DataFlash(void)
{
uint32_t base,Data;
uint32_t config0;
UNLOCKREG();
SYSCLK->PWRCON.OSC22M_EN = 1;
DrvSYS_Delay(22);
SYSCLK->AHBCLK.ISP_EN = 1;
FMC->ISPCON.ISPEN = 1;
//这一部分可以在烧录代码时配置好
//enable data flash for 128K APROM
DrvFMC_Read(0x300000, &config0); //读Cfg0
config0 &= ~0x1; //使能数据Flash,(仅对于128K的型号)
FMC->ISPCON.CFGUEN = 1; //使能ISP更新Config功能
DrvFMC_Erase(0x300000);
DrvFMC_Write(0x300000, config0);
DrvFMC_Write(0x300004, 0x1F000); //配置好config要复位芯片后才生效
//用户配置
base = DrvFMC_ReadDataFlashBaseAddr(); //读一下数据Flash的基址
DrvFMC_Erase(base); //erase 0x1F000 - 0x1F200
DrvFMC_Write(base, 0x3333CCCC); //写一个字到基地址
DrvFMC_Write(base+4, 0x55555555); //写第二个字
DrvFMC_Read(base, &Data); //读出第一个数据校验
if(Data!=0x3333CCCC)
printf("DataFlash test fail\n");
else
printf("DataFlash test succeed\n");
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main()
{
/* SYSCLK =>12Mhz*/
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_XTL12M,ENABLE);
DrvSYS_Delay(5000);
DrvSYS_Open(50000000);
DrvSYS_Delay(5000);
DrvGPIO_Open(E_GPC, 3, E_IO_OUTPUT);
Test_DataFlash();
Test_TIMER();
Test_PWM_CAP();
Test_ADC();
while(1);
}