I want to measure the time between two pulses in milliseconds, how can I do that in PBP?
![]()
I want to measure the time between two pulses in milliseconds, how can I do that in PBP?
![]()
Start timer1 on rising edge of first cycle and stop it on rising edge of second cycle.
Check the count accumulated and work out the time elapsed in function of fosc/4 and prescaler.
Cheers
Al.
All progress began with an idea
Never used the PICīs timer before, Anyone have a short example of how to use timers?
Many ways to do it. A simple, non elegant, way:Obviously this will "hang" the PIC while measuring and there may be some (in the us region) error due to polling the inputs but it should work. Another, more elegant way, would be to use the timer in conjuction with one of the CCP modules set up in capture mode. As it happens they have a mode where it captures the value of the timer, in the backgroud, on every rising edge.Code:TimerTicks VAR WORD TMR1H = 0 TMR1L = 0 'Reset TMR1 While PortB.0 = 0 : WEND 'Wait for rising edge T1CON.0 = 1 'Start timer WHILE PortB.0 = 1 : WEND 'Wait for falling edge WHILE PortB.0 = 0 : WEND 'Wait for second rising edge T1CON.0 = 0 'Stop timer TimerTicks.HighByte = TMR1H TimerTicks.LowByte = TMR1L HSEROUT [#TimerTicks, 10,13]
/Henrik.
How does TMR1 know that it should count in ms?
It doesn't. As Al wrote previously it measures in "ticks" where one "tick" is equal to 1/(Fosc/4). At 4MHz one tick is 1us, at 20Mhz one tick is 200ns and so on. This is with the timers prescaler set to 1:1.
You need to convert from "ticks" to ms when the measurement is done.
In what range are we talking here, 10ms, 100ms, 50000000ms? TMR1 is 16 bits wide so it can measure 65536 "ticks", if you need more than that you need to use the prescaler at the cost of a drop in resolution but that doesn't sound like a problem right now.
Bookmarks