PDA

View Full Version : time measurement between 2 pulses



xvladx
- 13th April 2010, 19:58
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

Acetronics2
- 13th April 2010, 20:20
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

xvladx
- 13th April 2010, 21:18
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.

Acetronics2
- 15th April 2010, 10:31
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 :





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 ...



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

xvladx
- 15th April 2010, 20:58
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.


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

xvladx
- 16th April 2010, 01:06
Alain
I can not understand the sense of RCTime. Why it is used?

Thanks a lot

xvladx
- 16th April 2010, 01:53
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

Acetronics2
- 16th April 2010, 16:20
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

xvladx
- 16th April 2010, 19:48
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!

xvladx
- 20th April 2010, 15:31
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!

xvladx
- 27th April 2010, 17:33
Alain, are you there? Help me please!!