/******************************************************************************
* @file main.c
* @version V1.00
* $Revision: 7 $
* $Date: 14/03/27 1:43p $
* @brief PWM sample for NANO1X2 series MCU
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano1X2Series.h"
#define SAMPLE_CNT 32
static uint8_t volatile cap_index;
static uint32_t cap_val[SAMPLE_CNT >> 1][2];
void PWM0_IRQHandler(void);
void PWM0_IRQHandler(void)
{
static uint8_t token = 0;
uint32_t u32CapIntFlag;
uint8_t u8Count = cap_index;
if(u8Count >= SAMPLE_CNT)
return;
// Get channel 2 capture interrupt flag
u32CapIntFlag = PWM_GetCaptureIntFlag(PWM0, 2);
// Rising latch condition happened
if ((u32CapIntFlag & PWM_RISING_LATCH_INT_FLAG) && token == 0) {
cap_val[u8Count >> 1][0] = PWM_GET_CAPTURE_RISING_DATA(PWM0, 2);
cap_index++;
token = 1;
}
// Falling latch condition happened
if ((u32CapIntFlag & PWM_FALLING_LATCH_INT_FLAG) && token == 1) {
cap_val[u8Count >> 1][1] = PWM_GET_CAPTURE_FALLING_DATA(PWM0, 2);
cap_index++;
token = 0;
}
// Clear channel 2 capture interrupt flag
PWM_ClearCaptureIntFlag(PWM0, 2, PWM_RISING_FALLING_LATCH_INT_FLAG);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable HIRC */
CLK_EnableXtalRC(CLK_PWRCTL_LXT_EN_Msk | CLK_PWRCTL_HIRC_EN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_LXT_STB_Msk | CLK_CLKSTATUS_HIRC_STB_Msk);
/* Enable PWM clock */
CLK_EnableModuleClock(PWM0_CH01_MODULE);
CLK_EnableModuleClock(PWM0_CH23_MODULE);
/* Set HIRC as PWM clock source */
CLK_SetModuleClock(PWM0_CH01_MODULE, CLK_CLKSEL1_PWM0_CH01_S_HIRC, 0);
CLK_SetModuleClock(PWM0_CH23_MODULE, CLK_CLKSEL1_PWM0_CH23_S_HIRC, 0);
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PC.0 and PC.2 multi-function pins for PWM channel 0 and 2 */
SYS->PC_L_MFP = (SYS->PC_L_MFP & ~SYS_PC_L_MFP_PC0_MFP_Msk) | SYS_PC_L_MFP_PC0_MFP_PWM0_CH0;
SYS->PC_L_MFP = (SYS->PC_L_MFP & ~SYS_PC_L_MFP_PC2_MFP_Msk) | SYS_PC_L_MFP_PC2_MFP_PWM0_CH2;
/* Lock protected registers */
SYS_LockReg();
}
int32_t main (void)
{
// uint8_t i;
/* 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();
// PWM0 frequency is 9000000Hz, duty 50%,
PWM_ConfigOutputChannel(PWM0, 0, 9000000, 50);
// Enable output of channel 0
PWM_EnableOutput(PWM0, PWM_CH_0_MASK);
// Start
PWM_Start(PWM0, PWM_CH_0_MASK);
while(1);
}
|