/*---------------------------------------------------------------------------------------------------------*/ 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(); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWMB channel 2 to capture\n the signal from PWMB channel 1.\n"); printf(" I/O configuration:\n"); printf(" PWM5(P2.5 PWMB channel 1) <--> PWM6(P2.6 PWMB channel 2)\n\n"); printf("Use PWMB Channel 2(P2.6) to capture the PWMB Channel 1(P2.5) Waveform\n"); while(1) { printf("Press any key to start PWM Capture Test\n"); getchar(); /*--------------------------------------------------------------------------------------*/ /* Set the PWMB Channel 1 as PWM output function. */ /*--------------------------------------------------------------------------------------*/ /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows. duty ratio = (CMR+1)/(CNR+1) cycle time = CNR+1 High level = CMR+1 PWM clock source frequency = __HXT = 12000000 (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 23999 duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30% CMR = 7199 Prescale value is 1 : prescaler= 2 Clock divider is PWM_CSR_DIV1 : clock divider =1 */ /* set PWMB channel 1 output configuration */ PWM_ConfigOutputChannel(PWMB, PWM_CH1, 250, 30); /* Enable PWM Output path for PWMB channel 1 */ PWM_EnableOutput(PWMB, 0x2); /* Enable Timer for PWMB channel 1 */ PWM_Start(PWMB, 0x2); /*--------------------------------------------------------------------------------------*/ /* Set the PWMB channel 2 for capture function */ /*--------------------------------------------------------------------------------------*/ /* If input minimum frequency is 250Hz, user can calculate capture settings by follows. Capture clock source frequency = __HXT = 12000000 in the sample code. (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 0xFFFF (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.) */ /* set PWMB channel 2 capture configuration */ PWM_ConfigCaptureChannel(PWMB, PWM_CH2, 166, 0); /* Enable capture falling edge interrupt for PWMB channel 2 */ PWM_EnableCaptureInt(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH); /* Enable PWMB NVIC interrupt */ NVIC_EnableIRQ((IRQn_Type)(PWMB_IRQn)); /* Enable Timer for PWMB channel 2 */ PWM_Start(PWMB, 0x4); /* Enable Capture Function for PWMB channel 2 */ PWM_EnableCapture(PWMB, 0x4); /* Wait until PWMB channel 2 Timer start to count */ while(PWMB->PDR2 == 0); /* Capture the Input Waveform Data */ CalPeriodTime(PWMB, PWM_CH2); /*------------------------------------------------------------------------------------------------------*/ /* Stop PWMB channel 1 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */ /*------------------------------------------------------------------------------------------------------*/ /* Set PWMB channel 1 loaded value as 0 */ PWM_Stop(PWMB, 0x2); /* Wait until PWMB channel 1 Timer Stop */ while(PWMB->PDR1 != 0); /* Disable Timer for PWMB channel 1 */ PWM_ForceStop(PWMB, 0x2); /* Disable PWM Output path for PWMB channel 1 */ PWM_DisableOutput(PWMB, 0x2); /*------------------------------------------------------------------------------------------------------*/ /* Stop PWMB channel 2 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */ /*------------------------------------------------------------------------------------------------------*/ /* Disable PWMB NVIC */ NVIC_DisableIRQ((IRQn_Type)(PWMB_IRQn)); /* Set loaded value as 0 for PWMB channel 2 */ PWM_Stop(PWMB, 0x4); /* Wait until PWMB channel 2 current counter reach to 0 */ while(PWMB->PDR2 != 0); /* Disable Timer for PWMB channel 2 */ PWM_ForceStop(PWMB, 0x4); /* Disable Capture Function and Capture Input path for PWMB channel 2*/ PWM_DisableCapture(PWMB, 0x4); /* Clear Capture Interrupt flag for PWMB channel 2*/ PWM_ClearCaptureIntFlag(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH); } }
/*---------------------------------------------------------------------------------------------------------*/ 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(); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWM0 channel 0 to capture\n the signal from PWM1 channel 0.\n"); printf(" I/O configuration:\n"); printf(" PWM0_CH0(PA.12 PWM0 channel 0) <--> PWM1_CH0(PA.2 PWM1 channel 0)\n\n"); printf("Use PWM0 Channel 0(PA.12) to capture the PWM1 Channel 0(PA.2) Waveform\n"); while(1) { printf("Press any key to start PWM Capture Test\n"); getchar(); /*--------------------------------------------------------------------------------------*/ /* Set the PWM1 Channel 0 as PWM output function. */ /*--------------------------------------------------------------------------------------*/ /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows. duty ratio = (CMR+1)/(CNR+1) cycle time = CNR+1 High level = CMR+1 PWM clock source frequency = __HXT = 12000000 (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 23999 duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30% CMR = 7199 Prescale value is 1 : prescaler= 2 Clock divider is PWM_CSR_DIV1 : clock divider =1 */ /* set PWM1 channel 0 output configuration */ PWM_ConfigOutputChannel(PWM1, 0, 250, 30); /* Enable PWM Output path for PWM1 channel 0 */ PWM_EnableOutput(PWM1, PWM_CH_0_MASK); /* Enable Timer for PWM1 channel 0 */ PWM_Start(PWM1, PWM_CH_0_MASK); /*--------------------------------------------------------------------------------------*/ /* Set the PWM0 channel 0 for capture function */ /*--------------------------------------------------------------------------------------*/ /* If input minimum frequency is 250Hz, user can calculate capture settings by follows. Capture clock source frequency = __HXT = 12000000 in the sample code. (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 0xFFFF (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.) */ /* set PWM0 channel 0 capture configuration */ PWM_ConfigCaptureChannel(PWM0, 0, 166, 0); /* Enable capture falling edge interrupt for PWM0 channel 0 */ //PWM_EnableCaptureInt(PWM0, 0, PWM_CAPTURE_INT_FALLING_LATCH); /* Enable PWM0 NVIC interrupt */ //NVIC_EnableIRQ(PWM0_IRQn); /* Enable Timer for PWM0 channel 0 */ PWM_Start(PWM0, PWM_CH_0_MASK); /* Enable Capture Function for PWM0 channel 0 */ PWM_EnableCapture(PWM0, PWM_CH_0_MASK); /* Enable falling capture reload */ PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN0_Msk; /* Wait until PWM0 channel 0 Timer start to count */ while((PWM0->CNT[0]) == 0); /* Capture the Input Waveform Data */ CalPeriodTime(PWM0, 0); /*---------------------------------------------------------------------------------------------------------*/ /* Stop PWM1 channel 0 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */ /*---------------------------------------------------------------------------------------------------------*/ /* Set PWM1 channel 0 loaded value as 0 */ PWM_Stop(PWM1, PWM_CH_0_MASK); /* Wait until PWM1 channel 0 Timer Stop */ while((PWM1->CNT[0] & PWM_CNT_CNT_Msk) != 0); /* Disable Timer for PWM1 channel 0 */ PWM_ForceStop(PWM1, PWM_CH_0_MASK); /* Disable PWM Output path for PWM1 channel 0 */ PWM_DisableOutput(PWM1, PWM_CH_0_MASK); /*---------------------------------------------------------------------------------------------------------*/ /* Stop PWM0 channel 0 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */ /*---------------------------------------------------------------------------------------------------------*/ /* Disable PWM0 NVIC */ //NVIC_DisableIRQ(PWM0_IRQn); /* Set loaded value as 0 for PWM0 channel 0 */ PWM_Stop(PWM0, PWM_CH_0_MASK); /* Wait until PWM0 channel 0 current counter reach to 0 */ while((PWM0->CNT[0] & PWM_CNT_CNT_Msk) != 0); /* Disable Timer for PWM0 channel 0 */ PWM_ForceStop(PWM0, PWM_CH_0_MASK); /* Disable Capture Function and Capture Input path for PWM0 channel 0 */ PWM_DisableCapture(PWM0, PWM_CH_0_MASK); /* Clear Capture Interrupt flag for PWM0 channel 0 */ PWM_ClearCaptureIntFlag(PWM0, 0, PWM_CAPTURE_INT_FALLING_LATCH); } }