10 Replies Latest reply on Nov 12, 2011 12:22 PM by gautamd_

    Frequency measurement using Timer



      I would like to use the a Timer in Psoc creator to determine the period of a signal and to deduce from it its frequency. I know I can use a frequency counter with a PWM but I do not want to use this technique. I would like to have a very fast update on my frequency measurement in order to be implemented in a control law.


      I have attached a very simple project where I try to make sense of the Timer function. I have tried to read the signal Period using the ReadPeriod() function but it always display 0xFFFF no matter what. I have tried most of the other functions and I cannot make much sense of them, I have read the documentation and I thought this ReadPeriod() was the way to go. Can somebody help me seting up the Timer correctly and tell me how to read the period of the signal?


      Thanks a lot,



        • 2. Re: Frequency measurement using Timer

          Hi Ben,




          It is possible to measure frequency using the Timer component available.


          Can you please let us know what is the maximum frequency you are intending to measure?




          You'll have to choose the proper resolution, clock and period of the Timer according to your requirement.

          • 3. Re: Frequency measurement using Timer





            The project you had attached is modified. The changes done are as follows:




            1) The clock is set to 1 MHz


            2) The period is set to 10000


            3) The Timer resolution is set to 16 bits


            4) An ISR Component is added which is connected to the capture_out terminal of the Timer component.








            1) A flag is set and Timer Count is reset to 10000 in the ISR when the capture_out is triggered.


            2) In the main function, the flag is plooed. If set, then the captured value is read.


            3) Since the Timer is a down counter, the captured value is subtracted from 10000 to obtain the measured period in micro seconds (as the clock is 1 MHz).


            4) The measured value is displayed on LCD and the Flag is reset.




            The max period that can be measured in this case is 10,000uS (100Hz signal).




            The project is attached.





            • 4. Re: Frequency measurement using Timer

              The use of Counter Component has the following advantage in this context:




              1) There is an option to configure it as an Up Counter. Hence the captured value will be the direct measure of the time period in micro seconds.


              2) There is an option to reload on Capture and Terminal Count. This will free the user from doing the same using APIs. This will be particularly advantageous in time critical applications such as Frequency Measurement. Upon Capture, the counter will reload to zero.




              The project using Counter is attached.

              • 5. Re: Frequency measurement using Timer

                 i use the attach code" Frequency measurement using Timer"  on my CY8Ckit-030,but it dosent work well.


                i inject the Function Generator 1000Hz signal in P27,but LCD display value is incorrect. And i change the frequency ,the display value doesnt follow change. Is the code have some Problem?


                At tht same time,i plug my oscilator proble in P27,the sine wareform is not perfect(a little distortion), but i remove them(Function Generator  and oscilator probe).the wareform is perfect .why ?

                • 6. Re: Frequency measurement using Timer

                  Hi Xiaowei,




                  In the projecxt attached, it is assumed that the input signal which is received is a square wave.




                  If you are trying to measure the frequency of an analog signal such as sine, triangular, sawtooth, etc, then is necessary to first convert it to a square wave.




                  This can be done using a comparator at the front end.




                  1) Care must be taken that the amplitude of the sine wave is within the Vssa and Vdda power supply of PSoC.


                  2) The Vmin of the sine wave should be greater than or equal to Vssa. The Vmax of the sine wave should be less than or equal to Vdda.


                  3) The Analog signal whose frequency is to be measured should be connected to the noninverting terminal of the comparator. The inverting input terminal should be connected to a reference voltage. It can be a VDAC which is configured to half the voltage between Vmin and Vmax of the input signal. You may also use VRef such as Vdda/2.


                  4) The output of the comparator should be connected to the capture input of the Counter.




                  The figure below shows the schematic.





                  • 7. Re: Frequency measurement using Timer

                    The modified project which can measure the frequency of analog signal such as sine, ramp, triangular, sawtooth, etc is attached.




                    The VDAC value is set to 1V. The user can adjust the VDAC to any suitable value depending upon the amplitude of the input signal.

                    • 8. Re: Frequency measurement using Timer



                      i have modified the project.


                      1)As the timer2.0 datasheet described,the timer is common used to measure the number of clocks between two rising edges as might be generated by a tachometer sensor.i want to use the counter component to achieve it. The tachometer sensor(flow sensor) output rang: 10Hz~100Hz  , 0.5V~4.5V  , square wave.


                      2) attachment is my project file. it can display the Frequency collectly ,but when i insert a breakpoint in the ISR Rounting ,i found it doesnt get the period every 250ms.so i cant get the Frequency every 250ms collectly. I think the code may have little bug?


                      3)Now the problem annoy me is how to measure the flow sensor output frequency at intervals of 250ms(4 times in 1S),so  i can approximatly calculate the integrated flow of 1 second. The flow sensor Linear equation: F(hz) = 8.1 *Q(L/Min);                     Q means instantaneous flow.Calculate the flow value in every 250ms accroding to the (F/8.1 *60,000)/250,so i can accumulate the flow value 4 time in 1 Second.  Is there a better way to calculalte the integrated flow ?

                      • 10. Re: Frequency measurement using Timer

                        Hi xiaowei.




                        After looking at your project and going by your requirement, my suggestion is to go with Timer component (UDB Based) and use the "Trigger" feature available in it.




                        You can generate a 4 Hz signal (using a PWM) for 250 ms and feed it to the Trigger input of Timer component (Rising Edge triggered). This starts the Timer down-counting. The Capture feature (Rising Edge) can be used to determine the Time Period between two rising edges and hence the frequency. The difference between the two capture values gives the period.




                        Care should be taken to compensation for if a terminal count occurs between two captures. Ideally, the previous capture value should be greater than the present capture value in the case of a down counter.