Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

figure time between two pulses

Status
Not open for further replies.

jcastillo

New Member
I would like to measure the time between two pulses. I will then input this number (a variable) into a mathematical equation and use the result later on in the program. How would I get started.

I am kind of experienced in prog. PICs. I have a few projects under my belt, but I've never had to do this type (timing) of programming before.
 

kinjalgp

Active Member
Its easy. You require to do the following steps in you PIC uC.
1) Initialize the counter to max. value on startup
2) Start internal interrupt based count-down timer when you receive the first pulse.
3) Stop the timer on receiving second pulse.
4) Now based on the crystal speed, you know what time it takes to decrement the count by one. Subtract the remaining count from initial loaded value and multiply with the time taken to decrement count by one. Thats the time between two pulses.
 

jcastillo

New Member
Thanks,

I was thinking along that line but it seemed too easy, figured there may be a more clever way of doing it.

Just to be sure I got it. I call a register count1 for example and load 255h into it. when i recieve my first trigger pulse I start the count down. when i recieve the second trigger pulse i stop the count down. Then do the math. I think thats it.

joe
 

kinjalgp

Active Member
Yes you got it right. But 255 (8-bit) is too less for RISC processor like PIC. It will reach 0 in no time. Use 16-bit timer for longer delay between pulses.
 

jcastillo

New Member
Well, I'm using a PIC16f84a running on a 100KHz external RC oscillator.
The pulses will average around 30 to 40 millisec apart. I do have an option to read off another two pulse which average 5 seconds apart, but of these pulses one is positive and one is negative so i will have to do some signal conditioning. (Both signals are positive for the first option). What do you think?
 

kinjalgp

Active Member
The problem of two different pulses can be solved by using "Edge Triggered" Interupt. I don't know whether PIC16F84 has edge triggered interrupt pin. The uC is interrupted when it senses a transition from High level to Low Level or vice versa on this pin. This method is quite simple but has one drawback. For ex. consider that the interrupt is falling edge triggered, then on receiving a falling edge of positive pulse, it will start counting. Here, the counting is delayed by the amount of pulse duration since it has not started counting when the pulse went from 0 to 1 but it will start when it goes from 1 to 0. Similar delay will be observed when stopping the counter. But if the pulse is short, error will be very small.

For getting both pulses on same wire, you need to combine both the signals using "OR" gate.
 
Status
Not open for further replies.

Latest threads

Top