You are here

Frequency measurement using Timer | Cypress Semiconductor

Frequency measurement using Timer

Summary: 12 Replies, Latest post by Bob Marlowe on 17 Feb 2016 05:49 AM PST
Verified Answers: 0
Last post
Log in to post new comments.
Benj123's picture
3 posts


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,



Laurent_T's picture
4 posts

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.


user_460349's picture
1362 posts

What is the frequency and resolution you need?

dasg's picture
Cypress Employee
730 posts

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.

user_66945721's picture
256 posts

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.



user_66945721's picture
256 posts

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

dasg's picture
Cypress Employee
730 posts

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.

pnielsen3's picture
130 posts

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,



user_14586677's picture
7646 posts

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


Regards, Dana.

kdpatel's picture
2 posts

 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_REV_EXPECT <= CYDEV_CHIP_REV_LEOPARD_ES2) && (isr2__ES2_PATCH ))      







user_78878863's picture
2551 posts

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?

Log in to post new comments.