AnsweredAssumed Answered

KEA128 PWT help

Question asked by Maharaj Shree Kumar Sood on Dec 9, 2016
Latest reply on Dec 12, 2016 by Maharaj Shree Kumar Sood

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()
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!!!!