*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*---------------------------------------------------------------------------------------------------------*/
/* Include hear file
*/
/*---------------------------------------------------------------------------------------------------------*/
#include "DrvTIMER.h"
#include "DrvUART.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "assert.h"
#include "DrvPWM.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t volatile complete = 0;
static int cnt = 0;
void TMR1_Callback(uint32_t u32Param)
{
printf("%d=>%d Hz\n", ++cnt, (12000000 * 100) / DrvTIMER_GetCaptureData(E_TMR1));
complete = 1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* PWM Timer Callback function */
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function
*/
/*---------------------------------------------------------------------------------------------------------*/
int32_t main (void)
{
S_DRVPWM_TIME_DATA_T sPt;
STR_UART_T param;
uint8_t u8Timer = DRVPWM_TIMER1;
UNLOCKREG();
CLK->PWRCTL_BITS.LXT_EN = 1;
CLK->PWRCTL_BITS.HXT_EN = 1;
DrvSYS_Open(32000000);//将时钟设定为32MHz
while(1)
if (CLK->CLKSTATUS_BITS.LXT_STB == 1)
break;
//DrvSYS_SelectHCLKSource (1);
//DrvSYS_Delay(5000);
//DrvGPIO_InitFunction (E_FUNC_CLKO);//将GPIOB.12设置为CLKO输出
//DrvSYS_SelectIPClockSource (FRQDIV_CLK_SET, 0x02);// 分频源选择
//DrvSYS_SetFreqDividerOutput (1, 0); // 使能与分频
DrvGPIO_InitFunction(E_FUNC_PWM01); // 设置GPIO口的PWM01功能
DrvPWM_Open(DRVPWM_TIMER1); //使能PWM时钟并复位
DrvSYS_Delay(500);
/* PWM Timer property */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u8HighPulseRatio = 20; /* High Pulse peroid : Total Pulse peroid = 20 : 100 */
sPt.i32Inverter = 0;
sPt.u32Frequency = 600000; // Set 1KHz to PWM timer output frequency
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER1,DRVPWM_HCLK); //选择系统时钟作为PWM的时钟
//DrvPWM_SelectClockSource(u8Timer, DRVPWM_EXT_12M);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(u8Timer, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(u8Timer, 1);
/* Enable Interrupt Sources of PWM Timer 0 and install call back function */
//DrvPWM_EnableInt(u8Timer, 0, DRVPWM_PwmIRQHandler);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(u8Timer);
//DrvSYS_SelectHCLKSource(0);
/* Select UART Clock Source From 12Mhz*/
DrvSYS_SelectIPClockSource(UART_CLK_SET, 0);
DrvGPIO_InitFunction(E_FUNC_UART0);
param.u32BaudRate = 115200;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
DrvUART_Open(UART_PORT0, ¶m);
DrvTIMER_Init();
printf(" TIMER Sample Code \n");
/* Set all the TIMER clock source are from 12MHz */
DrvSYS_SelectIPClockSource(TMR0_CLK_SET, 0);
DrvSYS_SelectIPClockSource(TMR1_CLK_SET, 0);
/* Using TIMER0 in E_ONESHOT_MODE, take frequency every 100 pulse */
DrvTIMER_OpenCounter(E_TMR0, 100, E_ONESHOT_MODE);
DrvTIMER_Stop(E_TMR0);
// Don't need to timer start now. OpenCounter automatically starts timer
/* Using TIMER1 in E_PERIODIC_MODE, counter counter works at 12MHz*/
DrvTIMER_Open(E_TMR1, 1000000, E_PERIODIC_MODE);
DrvTIMER_Stop(E_TMR1);
DrvTIMER_SetPrescale(E_TMR1, 0);
// Overwrite DrvTIMER_Open() setting
DrvTIMER_SetCompareValue(E_TMR1, 0xFFFFFF); // Full scale
/* Install callback "TMR1_Callback" and trigger callback when interrupt occures */
DrvTIMER_SetTimerEvent(E_TMR1, 1, (TIMER_CALLBACK)TMR1_Callback, 1);
/* Enable TIMER1 Intettupt */
DrvTIMER_EnableInt(E_TMR1, E_TIMER_TCAP_INT);
while(1) {
complete = 0;
DrvTIMER_EnableInterTimerTriggerMode(E_TMR0);
DrvTIMER_Start(E_TMR0);
//DrvTIMER_EnableExternalEventMode (E_TMR0, E_TIMER_EVENT_FALLING_EDGE);
GCR->PB_H_MFP_BITS.PB8 = 2;
while(complete == 0);
GCR->PB_H_MFP_BITS.PB8 = 0;
}
} |