Capturing PWM signal in MC9S08QG8

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Capturing PWM signal in MC9S08QG8

1,975 Views
sripada
Contributor I
Dear all
 
I am working on 8 bit controller MC9S08QG8.In my application i would like to capture PWM signal from external Module( Circuit).
 
So please help me out on the configuration of ports regarding this.
 
I am having idea about capturing the PWM signal But i am unable to find how i can use my ports for capturing PWM signal
 
thank you
 
Regards
Srinivas Sripada
Labels (1)
0 Kudos
3 Replies

384 Views
Ake
Contributor II
Hello,
To capture a PWM signal, I assume that the frequency is constant, you must measure the pulse length of the incoming signal.
So set the TPMCnSC to interrupt to Capture on a rising edge. Set a RisingEdgeFlag. Enable the interrupt.
 
In the interrupt routine, you read the TPMCnV register and save it.
You change the TPMCnSC to Capture on falling edge. Clear the RisingEdge flag.
 
The next time you get an interrupt, read the TPMCnV register and subtract the last TPMCnV register.
There you have the pulse length.
Note that the subtraction must be a 16 bit subtraction.
Also the TPMMOD register should not be changed.
This will take automatically take care of when the the timer counter overlaps.
 
At last, you must adjust the frequency to the incoming pulses. This is done by adjusting the PS2:smileytongue:S1:smileytongue:S0 bits in the TPMSC register.
Adjust it so that the longest pulse lenght does not make the 16 bit counter to wrap.
 
Yes, I think that is about it.
 
Regards,
Ake
 
0 Kudos

384 Views
Ake
Contributor II
Hi again,
If you need to capture very narrow pulses, you can use two TPM inputs and set up the TPMs to be input capture on the rising edge, and input capture on the falling edge.
On the falling edge interrupt, subtract the rising edge time to get tha absolute timing.
 
Regards,
Ake
0 Kudos

384 Views
PeterHouse
Contributor I
Make sure to set the dividers in such a way that you have the correct timing resolution AND a long enough time window to capture your narrowest and widest PWM pulses.

I also like to zero the counter on the beginning edge and read the time on the falling edge and not have to subtract anything.  I also use the overflow flag to detect if the ending edge never happened or for longer time periods it can be used to increment and counter variable.

Good Luck,

Peter House


0 Kudos