Measure time in mS between two pulses


Closed Thread
Results 1 to 9 of 9

Hybrid View

  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,612


    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,612


    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.

Members who have read this thread : 0

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