Newbie timer help.

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

Newbie timer help.

3,569 Views
Seegoon
Contributor I
Hi to all.
I'm new to using this processor(MC9S12Gc32) and a a bit confused about some of the Timer module functionality.
I have read the datasheet , but still have a few questions.
If I want to measure the time that a input on say IOC4 is high I have to do the following. Set the input to trigger on high and low edges. When the edge goes high I take the value of TC4 and store it. When the edge goes low I take the TC4 reading again and subtract the 2 to get a time/count difference?
 
This seems to work , but seems cumbersome. It would be easier to set the counter to 0 on the rising edge and then take one reading on the falling edge to get a result. The only way I can see to do this is to set the main counter TCNT to 0(this can't be done directly anyway)
but this would mess up any other timers that are running.
Am I missing something here ?
Cheers
Seegoon
Labels (1)
0 Kudos
Reply
6 Replies

925 Views
rhinoceroshead
Contributor I
The way you described it initially is the way I do it.  You also need to handle the timer overflows, of course.  One problem with setting the timer to trigger on high and low edges is that it becomes possible to get out of sync with the pulses such that you may measure the time between pulses instead of measuring the pulse itself.  Your software should check the pin to see if it is high or low to determine whether to store the value or to subtract from the previously stored value.  If the pulses are extremely short, you can tie the two pins together with a resistor and configure one to interrupt on high edges and configure the other to interrupt on low edges and write the ISRs accordingly.
 
As you said, you can reset the counter, but that interferes with multiple timers happening concurrently.  With this 'reset counter' method, you are only really saving yourself one subtraction which is only a few cycles - and the end result will be exactly the same either way.
0 Kudos
Reply

925 Views
bigmac
Specialist III

Hello,

You would need to take into account timer overflows only If the pulse width to be measured exceeds the timer overflow period.  For pulse widths less than the timer overflow period, a simple 16-bit subtraction of the two values would suffice.

If the two timer values have an intervening timer overflow, the second value would be less than the first value, but the first value would still need to be subtracted.  The correct value will be obtained if the result is simply considered unsigned.  For assembler this is straightforward - not sure of the implications for C code.

The pulse duration measurement getting "out of step" shouldn't be a problem provided the input capture is set up to occur on either a positive or a negative edge - but not for the timer mode to capture on either edge.  The opposite edge for the next capture should be set from within the ISR for the timer channel.

Regards,
Mac

 

Message Edited by bigmac on 2006-07-05 12:20 PM

0 Kudos
Reply

925 Views
Seegoon
Contributor I

Hi guys.

Thanks for the help. Nice to know that I'm at least on the right track.

My code is set to trigger on both edges , but I do check the current pin condition

to make sure I know which edge has occured. The pulse widths are in the order of about 12ms and shorter. The prescalar is set such that the timer would overflow only after about 20ms. I currently take 1 reading on the rising edge and one on the falling edge. If the second reading is bigger than the first I subtract the 2 to get a result. If it's smaller I assume the counter has rolled over and I subtract the first result from 65535 and add the second reading.

Is there a better weay to do this.

Cheers

Seegoon

 

0 Kudos
Reply

925 Views
rhinoceroshead
Contributor I
As Bigmac said, there is no need to subtract from 65535 (actually it would be 65536) since that number needs 17 bits to be represented and it will be interpreted as 0 anyway when truncated to 16 bits.  In assembly you would just treat the normal subtraction result as unsigned and ignore the carry bit.  In C you would declare or cast the integer holding the result to an 'unsigned int'.
 
Example:
 
Rising edge counter value = 0x0100
Falling edge counter value = 0x0080
 
0x10000 - 0x0100 + 0x0080 = 0xFF80
 
0x0080 - 0x0100 = 0xFF80 (there is a 1 in the C bit of CCR, but ignore it)
 
Using either method, you have to interpret the result as unsigned if you expect the pulse width to be longer than 32767 timer clock cycles.
0 Kudos
Reply

925 Views
Seegoon
Contributor I

Thanks for all the help guys. Think I've got it sorted now.

Now I need to figure how to save things to internal eeprom/ flash. A quick look through the datasheet shows that it looks to be quite a task :0(

Oh well.

Thanks again.

Cheers

Seegoon

0 Kudos
Reply

925 Views
Steve
NXP Employee
NXP Employee
Don't forget about the pulse accumulator input which can measure the width of pulses directly. It uses the timer clock/64 so the resolution is more limited that the IC channels.
0 Kudos
Reply