Measure time in mS between two pulses


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187

    Default Measure time in mS between two pulses

    I want to measure the time between two pulses in milliseconds, how can I do that in PBP?


  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    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

  3. #3
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    Never used the PICīs timer before, Anyone have a short example of how to use timers?

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    Many ways to do it. A simple, non elegant, way:
    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]
    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.

    /Henrik.

  5. #5
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    How does TMR1 know that it should count in ms?

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    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.

  7. #7
    Join Date
    Jan 2008
    Location
    Sweden
    Posts
    187


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    How do I change the timers prescaler?
    The range is from 1ms up to 10 seconds or more.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    You look in the datasheet for the particular PIC you're using, look up the section on the particular timer you're about to use, say TMR1. It'll tell you where the prescaler selection bits are (usually in T1CON for TMR1) and how to set them to get various prescaler ratios.

    IF you run the PIC at 4Mhz the timer ticks at 1Mhz, it's 16bits wide so the longest time it can measure is 65.5ms, each "tick" is 1us. On the 16F877, for example, the highest prescaler ratio is 1:8 so the longest time possible to measure is 524ms....

    TMR2 has a prescaler possible of 1:16 which will give you just over one second. EDIT: Sorry TMR2 may only be 8bits but look it for the PIC in question.

    Depending on what else you're going to do with the PIC there are various ways to go about this. You can run the PIC at a much lower frequency, like 100kHz or something like that. Or you can use an external oscillator to clock TMR1. If you design a 1kHz oscillator and set TMR1 up derive its clock extarnally (again, see T1CON register for TMR1 details) you'll be able to measte 65535ms or ~65seconds with a resolution of 1ms.

    Make the oscillator 5kHz and you'll get 200us resolution and you'll still be able to measure up to ~13 seconds.

    /Henrik.

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Measure time in mS between two pulses

    Rough explanation of something.

    Use an interrupt to start/stop the timebase/counter. Use a fixed timebase and monitor the timer overflow. at each overflow, increment a variable.

    Select a interuptable pin
    Set it to rising edge
    once the interrupt happen Start the Timebase/counter

    At next interrupt. Stop timer and process the maths.

    Overflow can be managed with an ISR.

    When well done, you can have a really accurate reading in uSec precision or so. Sounds like a OlympicTimer
    Last edited by mister_e; - 7th March 2011 at 08:10.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts