void PWM0CH0_IRQHandler(void) { static uint32_t cnt; static uint32_t out; // Channel 0 frequency is 100Hz, every 1 second enter this IRQ handler 100 times. if(++cnt == 100) { if(out) PWM_EnableOutput(PWM0, 0x3F); else PWM_DisableOutput(PWM0, 0x3F); out ^= 1; cnt = 0; } // Clear channel 0 period interrupt flag PWM_ClearPeriodIntFlag(PWM0, 0); }
/** * @brief PWM0 IRQ Handler * * @param None * * @return None * * @details ISR to handle PWM0 interrupt event */ void PWM0_IRQHandler(void) { static int toggle = 0; // Update PWM0 channel 0 period and duty if(toggle == 0) { PWM_SET_CNR(PWM0, 0, 99); PWM_SET_CMR(PWM0, 0, 39); } else { PWM_SET_CNR(PWM0, 0, 399); PWM_SET_CMR(PWM0, 0, 199); } toggle ^= 1; // Clear channel 0 period interrupt flag PWM_ClearPeriodIntFlag(PWM0, 0); }
int32_t main (void) { /* 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(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code will output PWM channel 0 to with different duty\n"); printf(", and enable/disable Precise Center Align function.\n"); printf("Polling 1 period interrupt flag to get PWM channel 0 output.\n"); // PWM-Timer 0 enable and Auto-reload PWM->CTL = PWM_CTL_CNTEN0_Msk | PWM_CTL_CNTMODE0_Msk; PWM_SET_PRESCALER(PWM, 0, 1); PWM_SET_DIVIDER(PWM, 0, PWM_CLK_DIV_1); // Set the PWM aligned type PWM_SET_ALIGNED_TYPE(PWM, 0, PWM_CENTER_ALIGNED); // Enable PWM channel 0 output PWM_EnableOutput(PWM, BIT0); // Start PWM_Start(PWM, BIT0); /* Precise Center Align and Center Align PWM channel 0 waveform of this sample shown below: |<- CNR-(2*(CMR+1)) ->| CNR-(2*(CMR+1) = 401 -(2*(100+1)) CLKs |<- CNR -( 2 *( CMR + 1)) ->| CNR-(2*(CMR+1) = 402 -(2*(99+1)) CLKs |<- 2 *(CNR-CMR) clk ->| 2 * (CNR - CMR) = 2 * (401-100) CLKs |<- 2 * (CNR-CMR) clk ->| 2 * (CNR - CMR) = 2 * (402-99) CLKs ________ ____________ ________ ____________ ________ ____________ ________ ____________ ____| 7.96us |_8.08us_| 24.08us |_8.08us_| 8.08us |_8us_| 24.24us |_8us_| 7.96us |_8.08us_| 24.08us |_8.08us_| 8.08us |_8us_| 24.24us |_8us_ */ while(1) { // Enable PWM Precise Center Aligned Type PWM->PCACTL = PWM_PCACTL_PCAEN_Msk; // PWM Channel 0 Output : duty = 7.96u, low = 8.08u PWM_SET_CMR(PWM, 0, 100); PWM_SET_CNR(PWM, 0, 401); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Disable PWM Precise Center Aligned Type PWM->PCACTL &= ~(PWM_PCACTL_PCAEN_Msk); // PWM Channel 0 Output : duty = 24.08u, low = 8.08u PWM_SET_CMR(PWM, 0, 100); PWM_SET_CNR(PWM, 0, 401); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Enable PWM Precise Center Aligned Type PWM->PCACTL = PWM_PCACTL_PCAEN_Msk; // PWM Channel 0 Output : duty = 8.08u, low = 8u PWM_SET_CMR(PWM, 0, 99); PWM_SET_CNR(PWM, 0, 402); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Disable PWM Precise Center Aligned Type PWM->PCACTL &= ~(PWM_PCACTL_PCAEN_Msk); // PWM Channel 0 Output : duty = 24.24u, low = 8u PWM_SET_CMR(PWM, 0, 99); PWM_SET_CNR(PWM, 0, 402); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); } }