time measurement between 2 pulses


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Default time measurement between 2 pulses

    Hello friends! Continuous trying to measure (with the PIC16F628) the time between the arrival of a pulse and another one. Approximately the pulses arrive with a frequency around 5 at 7Hz. The procedure that elaborates is the following: In interruption input RB0/INT I connect the pulse generator to. With the arrival of the first pulse an interruption is caused and beginning to the account of TMR1 occurs. With the arrival of the second pulse, return to interrupt and stop the account of the TMR1. The read value in TMR1 must be the time between pulse and pulse that is what I want to know. Can ypu say me if this procedure is correct?
    Thank you a lot

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Vlad XX

    Just add to that resetting TMR1 after each measurement - OR - use the free running timer method : read TMR1 @ each interrupt and calculate the difference between two measurements.

    BUT ... I think the 628 has a " Capture " feature with the CCP Module that avoids to use PortB.0 interrupt ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Hi Alain,
    Thank you for your answer.
    In the attachment are the code in PBP and a scheme in Proteus.
    Please, May you see it?

    I cannot interpret the numbers on LCD display.

    Thank you again.
    Attached Files Attached Files

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Vlad

    @ 5 Hz ... period is 200 ms ... Timer1 has a 65536 µs range, so just can use a prescaler value of 4 ...

    to read Timer1 ... better try :

    Code:
    
    mainloop:  
             
              If counter = 2 then
                 T1CON.0 = 0 
                 time.LowByte = TMR1L
                 time.HighByte = TMR1H 
                 TMR1L = 0
                 TMR1H = 0         
                 lcdout $FE, 1
                 lcdout $FE, $82, dec time, " "
                 pause 300
                 counter = 0           
              endif
    Goto mainloop

    note ,here, you only read the Pic Timer ... you have to add 2 times 65536 units ( timer overflows )

    so, your count value is not good ...

    200000 = 3.05 ... times 65536 ! AND overall you do not stop timer @ the end of the period ... but just when counter reaches value of 2 ...

    so, doing it like you began to ...

    1) TMR1 interrupt just has to increment the "counter" value

    2) you must use another interrupt ( RB0 INT i.e.) to start or stop your timer1 ...

    AND

    3) ON Interrupt cannot be used for a precise timing measurement as it waits for the current PbP Command to complete ... so latency is variable, and overall ... far from negligible.

    So, ....

    Talking Frankly ... this way to measure pulses won't give satisfactory results ...

    a ( much ) simpler way would be to try ...

    Code:
    MeasLoop:
    
    Pulsin PortB.0,1,PulsHi
    RCTime PortB.0,0,Pulslo
    
    Time = PulsHi+PulsLo
    
    LCD Out $FE,$82, dec Time ' etc,etc ...
    
    Pause 500
    
    Goto measLoop
    and result will be Time X 10 µsecs ... I encourage you to read your relevant Manual chapters to understand what's happening with this short example ...

    You know what ??? pulse measurements with interrupts are not exactly the best way to begin learning PbP ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Wonderful!!It seems very simple of the way that you indicate to me. I have proven it and works perfectly. The only problem that I see is that to very low frecuency (< 1Hz ) it value is erroneous and to high frequencies (> 1 kHz) the read value is erroneous too.

    Code:
    pulsHi var word
    pulsLo var word
    Time var word
    
    
    MeasLoop:
    
    Pulsin PortB.0,1,PulsHi
    RCTime PortB.0,0,Pulslo
    
    Time = PulsHi+PulsLo
    
    LCDOut $FE,1, dec Time ' etc,etc ...
    
    Goto measLoop
    Thank you very very much Alain!!!!

    Flavio
    Last edited by xvladx; - 15th April 2010 at 21:47.

  6. #6
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Alain
    I can not understand the sense of RCTime. Why it is used?

    Thanks a lot

  7. #7
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    ok ok, I undestood the usin of RCTime. I was read an old post yours.
    The last question is...exists some process to measure to low frequencies lower than 0,8 Hz??

    thank you

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Ok, Vlad

    Erroneous, erroneous ... you're joking, I hope !!! 2x10µs for a 1ms period is 2% ... use a 20 Mhz Xtal and it will be 2x2µs ... .4% !!!

    The limitation is in maths involved.

    You want a high precision wide range freq. meter ??? ... begin by considering your Xtals precision and The Pic Osc stability vs temp. ...

    the only way is to use timers and interrupts ... with additionnal variables that extend timers range.

    somewhat like you started with ...

    but AT LEAST using Darrel's instant interrupts ... and probably the "N Bits maths" both from Darrel's goodies.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Alain
    I call erroneous when the counter pass through 65535 and then the counter start again. When de width of pulse is too big I must to handler the final count.
    Please be patient with me! I´m newbie!

  10. #10
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    The maximum time between pulse and pulse is of 65535 us x 2, that is 1,310s. How can I do to measure the value of the time between each pulse if this time is greater to 2 seconds, for example?

    Thank you!

  11. #11
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Alain, are you there? Help me please!!

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