View Full Version : Time diference in microseconds
leonel
- 10th February 2005, 11:41
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.
mister_e
- 10th February 2005, 17:24
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?
leonel
- 11th February 2005, 08:14
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.
leonel
- 11th February 2005, 12:33
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
mister_e
- 11th February 2005, 17:55
try this one
' 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.
mister_e
- 13th February 2005, 01:06
oups change the variable in the LCDOUT...
must be
lcdout $fe,1 ,"RA1 : ",#signal1TIME," uSec",_
$fe,$c0,"RA2 : ",#signal2TIME," uSec"
Kman
- 13th February 2005, 17:23
Mister e,
How would you do it if you wanted to use 1 pin as an input?
mister_e
- 13th February 2005, 20:43
you mean??? check two different signal on one pin?
leonel
- 14th February 2005, 09:30
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
mister_e
- 14th February 2005, 15:02
try with this
' 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
leonel
- 14th February 2005, 16:25
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
leonel
- 10th March 2005, 16:41
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
Ingvar
- 11th March 2005, 07:32
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
leonel
- 14th March 2005, 08:25
Thankīs...
Your code works.
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.