/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 5 $
* $Date: 14/06/30 4:51p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrate how to use PWM0 Channel 0 to capture the PWM1 Channel 0 waveform.
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "M0518.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions */
/*---------------------------------------------------------------------------------------------------------*/
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
/**
* @brief PWM0 IRQ Handler
*
* @param None
*
* [url=home.php?mod=space&uid=266161]@return[/url] None
*
* [url=home.php?mod=space&uid=1543424]@Details[/url] ISR to handle PWM0 interrupt event
*/
void PWM1_IRQHandler(void)
{
uint32_t FlagTemp;
uint16_t DutyTemp,PeriodTemp,DutyTemp1,PeriodTemp1;
FlagTemp=PWM1->INTSTS0;
PWM1->INTSTS0=FlagTemp; //clear all no usefull flag
if(PWM_GetCaptureIntFlag(PWM1, 0) & 2)
{
PWM_ClearCaptureIntFlag(PWM1, 0, PWM_CAPTURE_INT_FALLING_LATCH);
DutyTemp=PWM_GET_CAPTURE_FALLING_DATA(PWM1, 0); //change PWM0 channel 0 duty
PWM0->CMPDAT0=0x10000-DutyTemp;
}
if(PWM_GetCaptureIntFlag(PWM1, 0) & 1)
{
PWM_ClearCaptureIntFlag(PWM1, 0, PWM_CAPTURE_INT_RISING_LATCH);
// PeriodTemp=PWM_GET_CAPTURE_RISING_DATA(PWM1,0);
// PWM0->PERIOD0=0x10000-PeriodTemp;
}
if(PWM_GetCaptureIntFlag(PWM1, 1) & 2)
{
PWM_ClearCaptureIntFlag(PWM1, 1, PWM_CAPTURE_INT_FALLING_LATCH);
DutyTemp1=PWM_GET_CAPTURE_FALLING_DATA(PWM1, 1); //change PWM0 channel 1 duty
PWM0->CMPDAT1=0x10000-DutyTemp1;
}
if(PWM_GetCaptureIntFlag(PWM1, 1) & 1)
{
PWM_ClearCaptureIntFlag(PWM1, 1, PWM_CAPTURE_INT_RISING_LATCH);
}
//--------------------------------------------------------------------------------
if(PWM_GetCaptureIntFlag(PWM1, 2) & 2)
{
PWM_ClearCaptureIntFlag(PWM1, 2, PWM_CAPTURE_INT_FALLING_LATCH);
DutyTemp=PWM_GET_CAPTURE_FALLING_DATA(PWM1, 2);
PWM0->CMPDAT0=0x10000-DutyTemp;
}
if(PWM_GetCaptureIntFlag(PWM1, 2) & 1)
{
PWM_ClearCaptureIntFlag(PWM1, 2, PWM_CAPTURE_INT_RISING_LATCH);
// PeriodTemp=PWM_GET_CAPTURE_RISING_DATA(PWM1,2);
// PWM0->PERIOD0=0x10000-PeriodTemp;
}
if(PWM_GetCaptureIntFlag(PWM1, 3) & 2)
{
PWM_ClearCaptureIntFlag(PWM1, 3, PWM_CAPTURE_INT_FALLING_LATCH);
DutyTemp1=PWM_GET_CAPTURE_FALLING_DATA(PWM1, 3);
PWM0->CMPDAT1=0x10000-DutyTemp1;
}
if(PWM_GetCaptureIntFlag(PWM1, 3) & 1)
{
PWM_ClearCaptureIntFlag(PWM1, 3, PWM_CAPTURE_INT_RISING_LATCH);
}
FlagTemp=PWM1->CAPIF; //clear all useless flag for safe
PWM1->CAPIF=FlagTemp;
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for IRC22M 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 12MHz XTAL, internal 22.1184MHz */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC22M_EN_Msk);
/* Enable PLL and Set PLL frequency */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable PWM module clock */
CLK_EnableModuleClock(PWM0_MODULE);
CLK_EnableModuleClock(PWM1_MODULE);
/* Select UART module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
/* Select PWM module clock source */
CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL3_PWM0_S_PLL, 0);
CLK_SetModuleClock(PWM1_MODULE, CLK_CLKSEL3_PWM1_S_PLL, 0);
// CLK->CLKSEL3=0;
/* Reset PWM0 and PWM1 */
SYS_ResetModule(PWM0_RST);
SYS_ResetModule(PWM1_RST);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
//SystemCoreClockUpdate();
PllClock = PLL_CLOCK; // PLL
SystemCoreClock = PLL_CLOCK / 1; // HCLK
CyclesPerUs = PLL_CLOCK / 1000000; // For SYS_SysTickDelay()
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
//enable PWM0 channel 0,1 on PA12, PA13
SYS->GPA_MFP = (SYS->GPA_MFP & (~(SYS_GPA_MFP_PA12_Msk|SYS_GPA_MFP_PA13_Msk)));
SYS->GPA_MFP |= SYS_GPA_MFP_PA12_PWM0_CH0|SYS_GPA_MFP_PA13_PWM0_CH1;
SYS->ALT_MFP4= (SYS->ALT_MFP4&(~(SYS_ALT_MFP4_PA12_Msk|SYS_ALT_MFP4_PA13_Msk)));
//enable PWM1 channel 0,1,2,3 on PA 2,3,10,11
SYS->GPA_MFP = (SYS->GPA_MFP & (~(SYS_GPA_MFP_PA2_Msk|SYS_GPA_MFP_PA3_Msk|SYS_GPA_MFP_PA10_Msk|SYS_GPA_MFP_PA11_Msk)));
SYS->GPA_MFP |= SYS_GPA_MFP_PA2_PWM1_CH0|SYS_GPA_MFP_PA3_PWM1_CH1|SYS_GPA_MFP_PA10_PWM1_CH2|SYS_GPA_MFP_PA11_PWM1_CH3;
SYS->ALT_MFP3 &= ~(SYS_ALT_MFP3_PA2_Msk|SYS_ALT_MFP3_PA3_Msk|SYS_ALT_MFP3_PA10_Msk|SYS_ALT_MFP3_PA11_Msk);
SYS->ALT_MFP3 |= SYS_ALT_MFP3_PA2_PWM1_CH0|SYS_ALT_MFP3_PA3_PWM1_CH1|SYS_ALT_MFP3_PA10_PWM1_CH2|SYS_ALT_MFP3_PA11_PWM1_CH3;
SYS->ALT_MFP4= (SYS->ALT_MFP4&(~(SYS_ALT_MFP4_PA2_Msk|SYS_ALT_MFP4_PA3_Msk)));
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t 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();
while(1)
{
/* set PWM0 output configuration */
PWM_ConfigOutputChannel(PWM0, 0, 16100, 5); //initial PWM0 channel 0 period, duty default 5%
PWM_ConfigOutputChannel(PWM0, 1, 16100, 5); //initial PWM0 channel 0 period, duty default 5%
/* Enable PWM Output path for PWM0 channel 0,1 */
PWM_EnableOutput(PWM0, PWM_CH_0_MASK|PWM_CH_1_MASK);
/* Enable Timer for PWM0 channel 0,1 */
PWM_Start(PWM0, PWM_CH_0_MASK|PWM_CH_1_MASK);
PWM0->POLCTL|=3; //inverse PWM 0 channel 0,1 out put
/* set PWM1 channel 0,1,2,3 capture configuration */
PWM_ConfigCaptureChannel(PWM1, 0, 1, 0);
PWM_ConfigCaptureChannel(PWM1, 1, 1, 0);
PWM_ConfigCaptureChannel(PWM1, 2, 1, 0);
PWM_ConfigCaptureChannel(PWM1, 3, 1, 0);
/* Enable capture falling edge interrupt for PWM1 channel 0,1,2,3 */
PWM_EnableCaptureInt(PWM1, 0, PWM_CAPTURE_INT_FALLING_LATCH);
PWM_EnableCaptureInt(PWM1, 1, PWM_CAPTURE_INT_FALLING_LATCH);
PWM_EnableCaptureInt(PWM1, 2, PWM_CAPTURE_INT_FALLING_LATCH);
PWM_EnableCaptureInt(PWM1, 3, PWM_CAPTURE_INT_FALLING_LATCH);
/* Enable Capture Function for PWM1 channel 0,1,2,3 */
PWM_EnableCapture(PWM1, PWM_CH_0_MASK|PWM_CH_1_MASK|PWM_CH_2_MASK|PWM_CH_3_MASK);
/* Enable falling capture reload for channel 0,1,2,3 */
PWM1->CAPCTL |= PWM_CAPCTL_RCRLDEN0_Msk|PWM_CAPCTL_RCRLDEN2_Msk;
/* Enable PWM1 NVIC interrupt */
NVIC_EnableIRQ(PWM1_IRQn);
/* Enable Timer for PWM0 channel 0,1,2,3 */
PWM_Start(PWM1, PWM_CH_0_MASK|PWM_CH_1_MASK|PWM_CH_2_MASK|PWM_CH_3_MASK);
/* Wait until PWM1 channel 0 Timer start to count */
while((PWM1->CNT0) == 0);
while(1);
}
}