12 Replies Latest reply on Feb 17, 2016 5:49 AM by user_1377889

    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,



        • 1. Re: Frequency measurement using Timer

          Timer_ReadPeriod() return the period of the timer, default is 0xff for 8 bits, 0xffff for 16 bits, etc.


          For the function you want to implement, I would suggest to use the capture function of a timer.


          Connect the clock input to a fixed frequency.  ex: 1 MHz


          then connect the capture pin to the signal you want to measure.


          the Delta between two capured event will give you the period of your signal.

          • 2. Re: Frequency measurement using Timer

            What is the frequency and resolution you need?

            • 3. Re: Frequency measurement using Timer

              Hi Benj123,




              You can use the Timer with suitable resolution as per your requirement (8, 16, 24 or 32 bits).




              The time period of the square wave you want to measure is equal to the period between two rising edges. Hence, you can configure the Timer Capture mode to Rising edge. The difference between two rising edges gives the time period.




              The schematic of the Timer will look something like this.








              In this case, the frequency of clock used for counting is 1MHz.

              • 4. Re: Frequency measurement using Timer

                might I suggest a Counter in Capture Mode(Set to Auto Reload on Capture,and Interrupt on Capture?)


                All you'd need to do would be read the period in the Interrupt routine,and bang,thats your Time period(of the input signal).




                And your code would simply be,


                #include <device.h>

                uint16 period=0;

                Counter_ReadStatusRegister();// This will clear the interrupt
                period=Counter_ReadCapture();//Read the Capture Value.
                /*Capture value will give us the period of the sq. wave.
                in microseconds. */

                void main()
                /*Disable Interrupts to prevent accidental triggering
                   while setting up.*/
                   LCD_Start(); //Start LCD .
                   ISR_Start();//Start ISR.
                   ISR_SetVector(Cap);//Set ISR Vector to above written ISR.
                   Counter_Start();//Start Counter.



                • 5. Re: Frequency measurement using Timer

                  Ofcourse,you'll need to tweak the frequencies on the Count Clock etc as per your signal's expected maximum freq etc.

                  • 6. Re: Frequency measurement using Timer

                    Yes kmmankad, you are right !




                    Having Reload On Capture feature in Counter makes computation easy. If frees the user from subtracting the present capture value from the previous one.

                    • 7. Re: Frequency measurement using Timer

                      Your example for a counter was excelent. It was very easy to code in PSoC1. But I had some problems with the counter module.


                      Thank you very much for sharing this example and code. It is a tremendous help to me.




                      Thank You,





                      • 8. Re: Frequency measurement using Timer

                        Some useful info on precison period and frequency measurement, attached.




                        Regards, Dana.

                        • 9. Re: Frequency measurement using Timer

                           i also want to massure frequency here i am useing counter as kmmankand suggest.its easy way then useing timer but here i get some problem here at every 100 hz pssout frequency display more than real


                          foe example after 100hz


                          120display as 103on lcd


                          after 200hz input


                          210 display as 212hz   and so on ..........


                          i dont know why this hapen.....


                          here i attech my project and code






                          here is code








                          #include <device.h>


                           uint16 K,V,C,D,E,f,m;


                          uint8 A,state,Z,O;


                           float freq;


                          unsigned long int l;


                          void init (void)








                          Counter_1_Start() ;








                          void calculation(void)














                          void dataupdate(void)






                          state ^=0xFF;       //for led toggel










                          void main()




                              /* Place your initialization/startup code here (e.g. MyInst_Start()) */




                               CYGlobalIntEnable;  /* Uncomment this line to enable global interrupts. */












                                 calculation(); /* Place your application code here. */














                          /* [] END OF FILE */










                              /*  Place your Interrupt code here. */


                              /* `#START isr2_Interrupt` */




















                              /* `#END` */




                              /* PSoC3 ES1, ES2 RTC ISR PATCH  */ 


                              #if(CYDEV_CHIP_DIE_EXPECT == CYDEV_CHIP_DIE_LEOPARD)


                                  #if((CYDEV_CHIP_REV_EXPECT <= CYDEV_CHIP_REV_LEOPARD_ES2) && (isr2__ES2_PATCH ))      













                          • 10. Re: Frequency measurement using Timer

                            First, please don't hijack a thread wich has been closed since half a year. If you have a new question, open a new thread. That way, more people will see it.


                            Second, please attach your full project instead of just an image and some copied code. We cannot see, for example, the component configuration or if something else is amiss (the counter might just be configured not properly).


                            To your problem: I think you need to use a 24bit counter. I suspect you get overflows with your configuration. I also don't see in the code where you update the LCD with your measured frequency?

                            • 11. Re: Frequency measurement using Timer

                              I am using PWM in my project, i am giving clock frequency 100KHz ,  PWM output frequency is 395.6Hz.


                              I thought formula for this is PWM frequency = Clock frequency / Number of counts.


                              in my case , number of count is 256(0-255),


                              please let me know formula for PWM frequency. 

                              • 12. Re: Frequency measurement using Timer



                                Please have a look at hli's last comment. This is true for you too.


                                To post a complete project here, use
                                Creator->File->Create Workspace Bundle (minimal)
                                and attach the resulting file.