13 Replies Latest reply on Mar 17, 2013 4:28 AM by DaKn_263916

    time interval between two pulses using counters

       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

        • 1. Re: time interval between two pulses using counters

          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. 

          • 2. Re: time interval between two pulses using counters
            DaKn_263916

            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.

            • 3. Re: time interval between two pulses using counters

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

                 

              how can i measure time?

              • 4. Re: time interval between two pulses using counters
                DaKn_263916

                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.

                • 5. Re: time interval between two pulses using counters

                  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. 

                  • 6. Re: time interval between two pulses using counters

                     plz post sample program

                    • 7. Re: time interval between two pulses using counters
                      DaKn_263916

                      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.

                      • 8. Re: time interval between two pulses using counters

                         rising of 1  to rising edge of 2?

                           

                        falling of 1 to falling of 2?

                        • 9. Re: time interval between two pulses using counters
                          DaKn_263916

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

                             

                          are they async or sync to each other ?

                             

                           

                             

                          Regards, Dana.

                          • 11. Re: time interval between two pulses using counters
                            DaKn_263916

                            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.

                            • 12. Re: time interval between two pulses using counters

                               thanks for your reply

                                 

                              plz post sample program

                              • 13. Re: time interval between two pulses using counters
                                DaKn_263916

                                I do not have a sample program to post.

                                   

                                 

                                   

                                Regards, Dana.