Time diference in microseconds


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79

    Question Time diference in microseconds

    Iīm trying to measure the time diference between two signals and I donīt know how to do. The time diference is in order of microsecods or miliseconds, and I have to have an accurate precision.
    Can anybody help in the code that I have to write?
    Thank you.

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


    Did you find this post helpful? Yes | No

    Default

    the easiest way, and the most accurate one, will be to use internal timer. Start the timer with a rising or falling edge of your first signal, stop it at the next falling or rising edge, place the result of the counter into a variable, thennn switch to your next signal and do the same test. at the end compare both results

    At :
    4MHZ you'll have a accuracy of 1uSec
    8MHZ you'll have a accuracy of .5uSec
    and such

    Which PIC are you using ??? at which speed?
    Steve

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

  3. #3
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Using Pic16F874

    I'm thinking to use 16F874 at 4MHz.
    Itīs possible to post some code, if for example we have one signal in RA1 and another in RA2 because Iīm not too familiar with assembler... Iīm working with PICBasicPro.
    Thank you.

  4. #4
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Some Code

    Look if this code it works to measure two signals...

    if portb.1=1 then
    T1CON=1 " start timer
    while portb.2 = 0
    if portb.2=1 then
    T1CON=0 " stop timer
    var = TMR1 " my variable comes with the value of the time diference in microseconds
    endif
    wend
    endif

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


    Did you find this post helpful? Yes | No

    Default

    try this one

    Code:
    ' Program to measure time on two pins in a 1uSec increment
    ' Using PIC16F874 with TIMER1
    
    TRISA=255  ' Set PORTA as INPUT
    ADCON1 = 7 ' disable analog to digital converter
    
    T1CON = 0  ' set timer clock source to internal (fosc/4)
    
    Signal1TIME var word   
    Signal2TIME var word
    Signal1     var PORTA.1
    Signal2     var PORTA.2
    
    Start:
    
    ' measure period on RA.1
    '
    TMR1L=0
    TMR1H=0
    while signal1=0 ' waiting for rising edge
    wend
    T1CON.0 = 1 ' start Timer1
    While signal1 ' wait for falling edge
    wend
    while signal1 = 0 ' wait for the next rising edge
    wend
    T1CON = 0 ' stopTimer
    signal1time.lowbyte = TMR1L
    Signal1time.highbyte = TMR1H
    
    
    ' measure period on RA.2
    '
    TMR1L=0
    TMR1H=0
    while signal2=0 ' waiting for rising edge
    wend
    T1CON.0 = 1 ' start Timer1
    While signal2 ' wait for falling edge
    wend
    while signal2 = 0 ' wait for the next rising edge
    wend
    T1CON = 0 ' stopTimer
    signal2time.lowbyte = TMR1L
    Signal2time.highbyte = TMR1H
    
    lcdout $fe,1  ,"RA1 : ",#signal1," uSec",_
           $fe,$c0,"RA2 : ",#signal2," uSec"
    
    pause 2000
    goto start
    This is suppose to work.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    oups change the variable in the LCDOUT...

    must be
    Code:
    lcdout $fe,1  ,"RA1 : ",#signal1TIME," uSec",_
           $fe,$c0,"RA2 : ",#signal2TIME," uSec"
    Steve

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

  7. #7
    Join Date
    Jan 2005
    Posts
    21


    Did you find this post helpful? Yes | No

    Default

    Mister e,
    How would you do it if you wanted to use 1 pin as an input?

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


    Did you find this post helpful? Yes | No

    Default

    you mean??? check two different signal on one pin?
    Steve

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

  9. #9
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Thumbs up Code - Time stays high

    Hi. Thanks for your code...
    What about using this code (your code with a little changes ) to measure the time that the signal stays high, because I think that with your code I have the problem that I could find the signal in an high level and didnīt count the correct time!
    Take a look...

    code:


    ' Program to measure time on two pins in a 1uSec increment
    ' Using PIC16F874 with TIMER1

    TRISA=255 ' Set PORTA as INPUT
    ADCON1 = 7 ' disable analog to digital converter

    T1CON = 0 ' set timer clock source to internal (fosc/4)

    Signal1TIME var word
    Signal2TIME var word
    Signal1 var PORTA.1
    Signal2 var PORTA.2

    Start:


    ‘ I will measure the time staying high, but only in the second period
    ‘ because I have a little chances that when I go to next pin “find” 'the pin in middle of a high level and I didnīt count the correct time

    ' measure time that RA.1 stays high
    TMR1L=0
    TMR1H=0
    while signal1=0 ' waiting for rising edge
    wend
    While signal1 ' wait for falling edge
    wend
    while signal1 = 0 ' wait for the next rising edge
    wend
    T1CON.0 = 1 ' start Timer1
    While signal1 ' wait for falling edge
    Wend
    T1CON = 0 ' stopTimer

    signal1time.lowbyte = TMR1L
    Signal1time.highbyte = TMR1H


    ' measure time that RA.2 stays high

    TMR1L=0
    TMR1H=0
    while signal2=0 ' waiting for rising edge
    wend
    While signal2 ' wait for falling edge
    wend
    while signal2 = 0 ' wait for the next rising edge
    wend
    T1CON.0 = 1 ' start Timer1
    While signal2 ' wait for falling edge
    Wend
    T1CON = 0 ' stopTimer
    while signal2 = 0 ' wait for the next rising edge
    wend

    signal2time.lowbyte = TMR1L
    Signal2time.highbyte = TMR1H

    lcdout $fe,1 ,"RA1 : ",#signal1TIME," uSec",_
    $fe,$c0,"RA2 : ",#signal2TIME," uSec"
    pause 2000
    goto start

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


    Did you find this post helpful? Yes | No

    Default

    try with this

    Code:
    ' measure time that RA.1 stays high
    TMR1L=0
    TMR1H=0
    
         ' test if the signal is already high
         '
    Signal1HIGH:
    if signal1 then Signal1HIGH
    
         ' wait for the rising edge
         '
    while signal1 = 0
    wend
    
         ' signal is now high... let's count the "hi" time 
         '
    T1CON.0 = 1 ' start Timer1
    
    While signal1 ' wait for falling edge
    wend
    
    T1CON = 0 ' stopTimer
    
         ' store the result
         '
    signal1time.lowbyte = TMR1L
    Signal1time.highbyte = TMR1H
    Last edited by mister_e; - 14th February 2005 at 15:04.
    Steve

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

  11. #11
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default It works... !??

    Thankīs a lot...
    I suppose your code works but I donīt think these week Iīll have time to test it, because Iīm working with the hardware...
    But when I have news Iīll tell you.
    Regards
    Leonel Monteiro

  12. #12
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default wrong time

    Can anybody explain me whatīs wrong in these code...
    I want to turn led on and off in each 5 seconds, but it didnīt wait 5 seconds. It is much less, about 50ms
    Sorry but variables and comments are in portuguese

    'este ficheiro é apenas para testar algumas funcionalidades da placa

    DEFINE OSC 4 'o programa irá correr a 4MHz

    ADCON1 = $06 'indica que todas as entradas são digitais
    TRISA = $3F 'estou a colocar RA.0, RA.1, RA.2, RA.3, RA.4 e RA.5 como entradas e o resto como saídas
    TRISB = $FF 'estou a colocar tudo como entradas
    TRISC = $00 'estou a colocar tudo como saídas
    variavel var word
    start:
    portc.3 = 1
    gosub tempo_ligado
    portc.3 = 0
    portc.2 = 1
    gosub tempo_ligado
    portc.2 = 0
    portc.1 = 1
    gosub tempo_ligado
    portc.1 = 0
    goto start

    tempo_ligado:
    T1CON.0 = 1 'inicio o timer
    while variavel < 80
    if pir1.0 = 1 then
    variavel = variavel + 1 'a variavel incrementa a cada 0.06seg.
    'colocando a 80 esperamos 5seg
    endif
    wend
    pir1.0 = 0
    T1CON.0 = 0 'paro o timer
    variavel = 0
    TMR1L = 0
    TMR1H = 0
    return

  13. #13
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Try ....

    tempo_ligado:
    T1CON.0 = 0 'paro o timer
    pir1.0 = 0
    variavel = 0
    TMR1L = 0
    TMR1H = 0
    T1CON.0 = 1 'inicio o timer
    while variavel < 77
    if pir1.0 = 1 then
    variavel = variavel + 1 'a variavel incrementa a cada 0.06seg.
    'colocando a 80 esperamos 5seg
    pir1.0 = 0
    endif
    wend
    T1CON.0 = 0 'paro o timer
    return

  14. #14
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    Thankīs...
    Your code works.

Similar Threads

  1. Hello, long time no post (ADC sample rate)
    By Jhong2 in forum General
    Replies: 0
    Last Post: - 14th April 2009, 04:13
  2. I don't understand this code!
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 13th February 2008, 02:55
  3. Measuring time
    By AugustoPedrone in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 30th July 2007, 23:46
  4. Serout2/serin2 Pbp Problem
    By SOMRU in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 19:55
  5. Timer in real time
    By martarse in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th July 2005, 14:24

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