time interval between two pulses using counters

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 hai frnds

   

i post image below

   

1)now i want to calcute the time interval between two pulses using counters?

   

2)And i want know about how much i will give values for vc1,vc2,vc3,and periods of both counter?

   

3) procedure for coding? plz help me

0 Likes
13 Replies
Anonymous
Not applicable

You can use the timer capture feature. Feed the start and stop pulse (ORed) to the capture terminal of timer. On the rising edge of the start pulse, timer count will be captured. Read this captured value (let us say this is count1). On the rising edge of stop pulse, timer count will be once again captured. Read this captured value also (let us say this is count2). The difference in count (count1-count2) will give the time difference between start and stop pulse.

   

Please note that timer is a down counter. For a 16bit timer, it starts from 0xffff to 0000 and starts again from 0xffff. It is not necessary that count1 will always be greater than count2. As you are not initializing the timer in the beginning of each start pulse, it is possible for count2 to be greater sometimes. You need to take care of this with little bit of firmware.

   

The timer input clock frequency depends on the precision with which you want to do the time measurement. For example, timer input clock frequency of 1MHz, will give you precision of 1us. Configure VC1/VC2/VC3 accordingly. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Might help if you can clear up what you are trying to measure -

   

 

   

1) One signal, a pulse train, and measure between any two adjacent pulses

   

     a. Rising edge pulse1 to rising edge pulse2

   

     a. Rising edge pulse1 to falling edge pulse2

   

     a. Falling edge pulse1 to rising edge pulse2

   

     a. Falling edge pulse1 to falling edge pulse2

   

 

   

2) Two signals, pulse trains, and measure between any two adjacent pulses

   

     a. Rising edge sig1 to rising edge sig2

   

     a. Rising edge sig1 to falling edge sig2

   

     a. Falling edge sig1 to rising edge sig2

   

     a. Falling edge sig1 to falling edge sig2

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 the difference between two counters it will give number of pulses?

   

how can i measure time?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

In order to help we need a little more info -

   

 

   

1) One signal, a pulse train, and measure between any two adjacent pulses ?

   

     a. Rising edge pulse1 to rising edge pulse2 ?

   

     a. Rising edge pulse1 to falling edge pulse2 ?

   

     a. Falling edge pulse1 to rising edge pulse2 ?

   

     a. Falling edge pulse1 to falling edge pulse2 ?

   

 

   

2) Two signals, two pulse trains, and measure between any two adjacent pulses ?

   

     a. Rising edge sig1 to rising edge sig2 ?

   

     a. Rising edge sig1 to falling edge sig2 ?

   

     a. Falling edge sig1 to rising edge sig2 ?

   

     a. Falling edge sig1 to falling edge sig2 ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Time between two pulses can be derived from following simple equation:

   

Time (sec) = (Count 1 - Count 2) / Timer Input Clock Frequency (Hz)

   

 

   

Make sure you take care of overflow situation in which count 2 is higher than count 1. 

0 Likes
Anonymous
Not applicable

 plz post sample program

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You still have to answer this question, or draw a timing diagram and label

   

it with what you are trying to measure -

   

 

   

1) One signal, a pulse train, and measure between any two adjacent pulses ?

   

     a. Rising edge pulse1 to rising edge pulse2 ?

   

     a. Rising edge pulse1 to falling edge pulse2 ?

   

     a. Falling edge pulse1 to rising edge pulse2 ?

   

     a. Falling edge pulse1 to falling edge pulse2 ?

   

 

   

2) Two signals, two pulse trains, and measure between any two adjacent pulses ?

   

     a. Rising edge sig1 to rising edge sig2 ?

   

     a. Rising edge sig1 to falling edge sig2 ?

   

     a. Falling edge sig1 to rising edge sig2 ?

   

     a. Falling edge sig1 to falling edge sig2 ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 rising of 1  to rising edge of 2?

   

falling of 1 to falling of 2?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Is this one pulse train (signal) or two pulse trains (signals) ? If two signals

   

are they async or sync to each other ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 for 0ne signal

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

So basic way to do this is -


1) Lets assume for now the signal is repetitive, say 1 Khz. We want a measurement that
is accurate to 1%, so for 1 Khz there must be 100 clks or counts. So thats a clk rate
to a counter of 1 Khz x 100, or 100 Khz = 10 uS. Further lets say the freq of the pulse train
varies over a 10 to 1 range. So counter width must take care of 100 clks X 10 = 1000.
So use a 16 bit counter.


2) Place a 16 bit counter, chose its clk input to be 100 Khz (setting VC1/2/3 dividers
to produce that, and choose VC3 as clk source). Tie its enable high so that it runs
when you start it.


3) Place signal on a pin, enable it to ISR on positive edge.


4) Create a flag, call it first pulse flag, fpflag.


5) In ISR do the following

      if fpflag = 0 then load cntr period with 0xFFFF, start cntr, and set fpflag.
      if fpflag = 1 then stop cntr, read counter, subtract it from 0xFFFFFF, thats your measurement, reset fpflag



In our example each count in the measurement is 10 uS, counts x 10 uS = total time between pulses.


There is some latency variation in using ISR approach, that will affect measurement accuracy, especially if
pulse train frequency is >> 1 Khz.


Another approach is to use more HW, to start counter via enable using a toggled F-F
approach. This may be of interest -


http://www.psocdeveloper.com/uploads/tx_piapappnote/an2358.pdf


Regards, Dana.

0 Likes
Anonymous
Not applicable

 thanks for your reply

   

plz post sample program

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I do not have a sample program to post.

   

 

   

Regards, Dana.

0 Likes