cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1 MCU

Anonymous
Not applicable

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

0 Likes
12 Replies
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

What is the frequency and resolution you need?

0 Likes
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

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(;;);

}

0 Likes
Anonymous
Not applicable

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

0 Likes
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

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

0 Likes
ETRO_SSN583
Esteemed Contributor

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

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 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

   

}

   

 

   

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

0 Likes
HeLi_263931
Honored Contributor II

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?

0 Likes
Anonymous
Not applicable

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. 

0 Likes
JoMe_264151
Expert II

@Ambarish

   

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.

   

 

   

Bob

0 Likes