Hi all,
I am trying to measure the pulse width of PWM using PWT (Pulse Width Timer) module of KEA128. The frequency which I am measuring is 1Khz(50% duty cycle, can be changed) on PTD5 (PWINT0 channel). I have configured the PWT with bus clock of 20Mhz and pre-scale of 64. I have also enabled the interrupts for Data Ready and PWT interrupt so as to read the PPW (Positive Pulse width) and NPW (Negative PW) values in Reg PWT_R1 and PWT_R2,summing both value to count of one period.
Below is the my code
/************************************************************************************************************/
void PWT_Init(void)
{
SIM_SCGC |= SIM_SCGC_PWT_MASK; /* Enable Clock for PWT module */
PWT_R1 |= PWT_R1_PWTEN_MASK; /* Enable PWT module*/
PWT_R1 |= PWT_R1_PRE(6); /* Set Prescaler: 64. 20 Mhz/64= 312.5KHz.*/
PWT_R1 |= PWT_R1_EDGE(2); /* first falling edge starts measurement, and on all the subsequent rising and falling edges*/
PWT_R1 |= PWT_R1_PWTSR_MASK; /* Soft module reset */
PWT_R1 |= PWT_R1_PRDYIE_MASK; /* PWT Pulse Width Data Ready Int Enable*/
PWT_R1 |= PWT_R1_PWTIE_MASK; /* PWT Module Interrupt Enable*/
SIM_PINSEL1 &= ~SIM_PINSEL1_PWTIN0PS_MASK; /* PWT channel mapped to pin PTD5 (PWINT0) */
}
ISR of PWT is, I have enabled NVIC and all in main......
void PWT_IRQHandler()
{
if (((PWT_R1 & PWT_R1_PWTRDY_MASK)>>PWT_R1_PWTRDY_SHIFT == 1))
{
LED0_TOGGLE;
pwm_rd = (PWT_R2 & PWT_R2_NPW_MASK) >> PWT_R2_NPW_SHIFT;
pwm_rd1 = (PWT_R1 & PWT_R1_PPW_MASK);
}
PWT_R1 &= ~PWT_R1_PWTRDY_MASK; /* Clear flag: read reg then write 0 to PWTRDY */
}
/************************************************************************************************************/
Now, My problem is that when i am trying to print values of both NPW and PPW in main, only NPW is getting updated but not PPW. PPW is also NOT getting updated even when I change EDGE[1:0] bits to PWT_R1_EDGE(1);
For 1Khz input pulse width, the value in NPW is 156, it should be 156 in PPW so that I can calculate how much is the duty cycle of the PWM pulse.
Freq. can be calculated as below
PWT Clock / (PPW ? NPW), PWT Clock = 20Mhz/64 = 312500/312 = 1000 (!Khz pwm)
Pls shed some light on this, if anybody has done this before. WHERE I AM GOING WRONG!!!!
已解决! 转到解答。
Hi Robin,
thanks for the reply.
@The pwm_rd and pwm_rd1 in your PWT_IRQHandler() seems to be error.
here pwm_rd and pwm_rd1 are two diffrent uint16_t volatile variables (used in ISR) which i use to get values from NPW and PPW registers.
@o not change any PWT configurations as long as PWTEN is set.
Now I have changed that line and enabled tha PWT module after configuring the PWT(clock,egde etc..)
But still, I am not bale to get the result. The thing is that PPW val is not getting update in any case whenever I change the selection bits of EDGE[1:0].
ISR is gettting serviced,vals are being printed but only of NPW....
Hi
The pwm_rd and pwm_rd1 in your PWT_IRQHandler() seems to be error. Should be like below:
u32PositivePulse = (PWT_R1 & PWT_R1_PPW_MASK)>> PWT_R1_PPW_SHIFT;
u32NegativePulse = (PWT_R2 & PWT_R2_NPW_MASK) ;
To avoid unexpected behavior, do not change any PWT configurations as long as PWTEN is set. I saw that you enabled that bit at the beginning of PWT_Init().
I think you can refer the PWT_Init() in pwt.c.
The pwt.c can be found in below folder. For example: ...:\Program Files\Freescale\S32DS_ARM_v1.3\S32DS\S32_SDK_KEA_1_0_0\sources
The S32 Design Studio for ARM v1.3 IDE can be download from here.
Best Regards,
Robin
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
---------------------------------------------------------------------------------------------------------------------
Hi Robin,
thanks for the reply.
@The pwm_rd and pwm_rd1 in your PWT_IRQHandler() seems to be error.
here pwm_rd and pwm_rd1 are two diffrent uint16_t volatile variables (used in ISR) which i use to get values from NPW and PPW registers.
@o not change any PWT configurations as long as PWTEN is set.
Now I have changed that line and enabled tha PWT module after configuring the PWT(clock,egde etc..)
But still, I am not bale to get the result. The thing is that PPW val is not getting update in any case whenever I change the selection bits of EDGE[1:0].
ISR is gettting serviced,vals are being printed but only of NPW....
Hi
Please see the Register of PWT_R1 and PWT_R2 first.
The PositivePulse = (PWT_R1 & PWT_R1_PPW_MASK)>> PWT_R1_PPW_SHIFT; due to it's located at the higher 16bits.
The NegativePulse = (PWT_R2 & PWT_R2_NPW_MASK) ; due to it's located at the lower 16bits.
Best Regards,
Robin