Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > Frequency measurement using Timer

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Frequency measurement using Timer
Moderator:
RKRM

Post Reply
Follow this topic



Frequency measurement using Timer

Benj123 posted on 16 Aug 2011 11:56 AM PST
Member
3 Forum Posts

Greetings,

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,

Ben

 




Re: Frequency measurement using Timer

Laurent_T posted on 10 Nov 2011 09:30 PM PST
Member
4 Forum 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.

 



Re: Frequency measurement using Timer

H L posted on 10 Nov 2011 09:50 PM PST
Top Contributor
679 Forum Posts

What is the frequency and resolution you need?



Re: Frequency measurement using Timer

Gautam Das posted on 12 Nov 2011 09:40 AM PST
Cypress Employee
742 Forum 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.



Re: Frequency measurement using Timer

kmmankad posted on 12 Nov 2011 10:27 AM PST
Top Contributor
268 Forum 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;

CY_ISR(Cap){
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. */
LCD_PrintNumber(period);
}

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

    for(;;);

}



Re: Frequency measurement using Timer

kmmankad posted on 12 Nov 2011 10:29 AM PST
Top Contributor
268 Forum Posts

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



Re: Frequency measurement using Timer

Gautam Das posted on 12 Nov 2011 10:36 AM PST
Cypress Employee
742 Forum 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.



Re: Frequency measurement using Timer

pnielsen3 posted on 25 Oct 2012 08:35 AM PST
Top Contributor
107 Forum 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,

 

Philip



Re: Frequency measurement using Timer

danaaknight posted on 25 Oct 2012 04:02 PM PST
Top Contributor
1773 Forum Posts

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

 

Regards, Dana.



Re: Frequency measurement using Timer

kdpatel posted on 11 Apr 2013 03:48 AM PST
Member
2 Forum 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)

{

LCD_Seg_1_Start();

Counter_1_Start() ;

isr2_Start();

}

 

void calculation(void)

{

V=m;

l=12000000/V;

}

 

void dataupdate(void)

{

    E=l;

state ^=0xFF;       //for led toggel

out1_Write(state);

 

}

 

void main()

{

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

 

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

init();

    for(;;)

    {

if(A==1)

{

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

  dataupdate();

  A=0;

   }

    }

}

 

/* [] END OF FILE */

 

 

CY_ISR(isr2_Interrupt)

{

    /*  Place your Interrupt code here. */

    /* `#START isr2_Interrupt` */

//{

//z++;

Counter_1_ReadStatusRegister();

m=Counter_1_ReadCapture();

 

A=1;

 

    /* `#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 ))      

            isr2_ISR_PATCH();

        #endif

    #endif

}

 

-------------------------------------------------------------------------------------------------------------------------------------------------



Re: Frequency measurement using Timer

hli posted on 11 Apr 2013 04:30 AM PST
Top Contributor
675 Forum 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?






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: KXP; Secondary Owner: VWA; Sunset Date: 01/01/20