11 Replies Latest reply on Oct 19, 2020 9:37 PM by EktaN_26

# Timer parameter calculation

Hi..

I am using psco4 device cy8c4025azi-s413...Want to implement the timer with interrupt....I am using the timer component as timer counter(TCPWM mode)[v2.10]... clock frequency is 12Mhz...

how to set the Timer/counter page setting??? please let me know how to calculate the period, prescaler, clk frequency??? and relation between then....

• ###### 1. Re: Timer parameter calculation

BhSa,

As you noticed, the Timer has numerous settings affecting it's operation. To help you better, please describe what you need to achieve with your project. If you already have some project for review, you may attach it to the post by using File->Create Archive bundle -> minimal.

/odissey1

• ###### 2. Re: Timer parameter calculation

BhSa,

If you can share your intent for the timer and the interrupt with the forum, we may be able to help you better.

In general: You need to determine the desired output frequency or range of output frequencies your application needs.

Since the TCPWM v2.10 has a 16-bit max period value, you need to determine the prescaler value you need so that your lowest output frequency can work with the Period max of 65535.

Your input clock (12MHz) and prescaler (1x to 128x) is your granularity (or resolution) control.

Your general formulas to use are:

Fout = (Fin/prescale)/period

Therefore if:

Fin  = 12MHz

prescale = 1x

period = 65535 (+1)  [for the lowest output frequency]

Fout = (12000000/1)/65536

then Fout = 183.1Hz

If only the prescaler is changed to 128x

Fout = (12000000/128)/65536    [for the lowest output frequency]

then Fout = 1.143Hz

I hope this helps.

Len

1 of 1 people found this helpful
• ###### 3. Re: Timer parameter calculation

Hi,

Maybe, I'm trying to explain something redundant...

Here I tried to explain

(1) Terminologies around the Timer

(2) A simple sample using Timer (TCPWM)

(3) A simple sample using SysTick

(1) Terminologies around the Timer

First of all , IMHO, the word "Period" in timer/pwm is misleading.

It actually is the Maximum value of the counter.

So I use "Max_Value " instead of "Period" to avoid confusion, here.

As the counter starts from 0 to the Max_Value , the real "Period" becomes Max_Value + 1.

And the real time it takes is the period x clock's period.

And clock's period = 1/(clock's frequency).

So the interrupt period of the timer will be

Timer's Period = (Max_Value+1) x clock's period = (Max_Value+1) / (clock's frequency)

But usually the clock could be very fast, so using the Prescaler, clock's frequency can be divided

or clock's period can be multiplied.

Timer's Period =  (Max_Value+1) / (clock's frequency / prescaler) = (Max_Value+1) x prescaler / (clock's frequency)

Setting the Clock Frequency as a Fractional Value of Source Clock in PSoC 4/PSoC 6 MCU - KBA229104

(2) A simple sample using Timer (TCPWM)

To test this I wrote a sample project for CY8C4025AZI-S413

test_timer_201006

Schematic

Note: To make it easer to see the result, I changed the clock frequency to 10kHz. (10,000 Hz)

clock configuration

Pins

Note: Since I don't have a board with this MCU, I could not physically test it.

Please specify appropriate pin number for LED.

(It worked with CY8CKIT-044 (CY8C4247AZI-M485))

main.c

======================

#include "project.h"

CY_ISR(pit_isr)

{

LED_Write(!LED_Read()) ; /* toggle LED */

}

/*

* Timer Period = Timer Period / Clock Frequency

*/

int main(void)

{

uint16_t period = 9999 ; /* make period to 10,000 */

CyGlobalIntEnable; /* Enable global interrupts. */

pit_int_ClearPending() ;

pit_int_StartEx(pit_isr) ;

Timer_Init() ;

Timer_WritePeriod(period) ;

Timer_WriteCounter(0) ;

Timer_Enable() ;

for(;;)

{

/* Place your application code here. */

}

}

======================

(3) A simple sample using SysTick

After  writing above, I have another suggestion.

If all you need is a pit (periodical interrupt time) of ms order,

cortex-M has SysTick Timer in the core.

Which does not consume the timer/pwm component.

Following is a sample of using SysTick

sys_tick_test_201006

Schematic

Pins

Note: Since I don't have a board with this MCU, I could not physically test it.

Please specify appropriate pin number for LED.

(It worked with CY8CKIT-044 (CY8C4247AZI-M485))

main.c

======================

#include "project.h"

volatile uint32_t led_tick_count = 0 ;

CY_ISR(SysTick_ISR)

{

led_tick_count++ ;

if ((led_tick_count % 1000) == 0) {

led_tick_count = 0 ;

}

}

int find_empty_slot(void)

{

int result = -1 ;

uint32_t i ;

for (i = 0 ; i < CY_SYS_SYST_NUM_OF_CALLBACKS ; i++ ) {

if (CySysTickGetCallback(i) == NULL) {

result = i ;

break ;

}

}

return(result) ;

}

int main(void)

int sys_tick_slot = 0 ;

CyGlobalIntEnable; /* Enable global interrupts. */

sys_tick_slot = find_empty_slot() ;

if (sys_tick_slot < 0) {

while(1) { } /* halting here */

} else {

CySysTickStart() ;

CySysTickSetCallback(sys_tick_slot, SysTick_ISR) ;

}

CySysTickStart();

for(;;)

{

}

}

======================

moto

1 of 1 people found this helpful
• ###### 4. Re: Timer parameter calculation

Thank you very much for your support & suggestions....

The basic intent for the timer & interrupt is, i want the timer interrupt for every second.....

For that i have set the clock frequency at 1KHz to make timer count tick at every 1ms....

I have set the Period & Compare value at 1000 to get the interrupt after every 1000 ticks....

That is after every 1sec the terminal count will reach & interrupt is generated...

I got the perfect result....But as increase the time from my application lets say using this timer i am blink the led at 10 min, then i got the blinking result at 9.58 min.......(I am using smart phone stopwatch to test this time...  )

how can i rectify the lagging period of 2 sec ???

• ###### 5. Re: Timer parameter calculation

Hi,

If all you need is 1sec pit, I would recommend to use SysTick.

About the 10min vs 9.58min, you (we) must aware of the accuracy of the clock.

(You can access this window by selecting Clocks in the Design Wide Resources, then double clicking a clock shown in the table.)

As you can see, the Accuracy of IMO is +/- 2%. So 2sec in 10mins (600sec) seems to be doing OK (within the spec))

You could add an external crystal with higher accuracy, but unless you can provide a method to re-adjust the timer

it will not be possible to keep "exact" time for very long time (I mean months, years, centuries...)

For 2 sec interval, you can

(1) For TCPWM sample

uint16_t period = 9999 ; /* make period to 10,000 */

<     uint16_t period = 1999 ; /* make period to 20,000 */

(2) For SysTick sample

>     if ((led_tick_count % 1000) == 0) {

<    if ((led_tick_count % 2000) == 0) {

moto

P.S. Oops, I misunderstood your last line, but I hope that you understand the idea ;-)

1 of 1 people found this helpful
• ###### 6. Re: Timer parameter calculation

Hello BhSa,

As correctly pointed out by Moto, the IMO clock being used in the application has an accuracy of +/2 % thus you are not seeing the exact 10 mins.

In order to increase the accuracy we will suggest you the following:

1. Use an EXTCLK or ECO as the the source

2. In case you are using PSoC 4 MCU with provision to connect WCO, You can enable the WCO (under low frequency clocks tab), and the enable Trim with WCO (Under High frequency clocks tab)

Best Regards

Ekta

1 of 1 people found this helpful
• ###### 7. Re: Timer parameter calculation

Thank you Ekta....

As per your suggestions, i tried to enable the Extclk and disable the IMO....but i am little bit confused about how to connect the external crystal in actual hardware....i found only one pin in cydwr pin section page....

Another attempt was used the WCO in IMO i tried to used trim with WCO but unfortunately the error found that the selected device(i.e CY8C4025AZI-S413) does not support the WCO.....

Can i get any help to solve this.....

• ###### 8. Re: Timer parameter calculation

BhSa,

i tried to enable the Extclk and disable the IMO....but i am little bit confused about how to connect the external crystal in actual hardware....i found only one pin in cydwr pin section page....

The part you've chosen (CY8C4025AZI-S413) does not have the ability to connect a XTAL directly to it.  There is NO ECO (External Crystal Oscillator) option.

You can insert an Externally generated high frequency clock (ExtClk) into P0[6].   This comes from another device like a Clock Oscillator (eq. Frequency Control CTS Corporation ) where the crystal and oscillation circuit are packaged together.  For most of these device you just need to supply power and if it has it an enable signal.

This part does support a WCO (Watch Crystal Oscillator).  You would place the watch crystal (32.768Khz) on pins P0[4] and P0[5] along with the required caps on each leg to GND.  The cap values depend on the crystal manufacturer recommended value.  Usually it ranges from 10pF to 25pF.

In general, it is quite common to use a WCO with very good PPM accuracy for long timing such as the 10 minutes you are targeting.

The watch crystal is usually cheaper than a high frequency crystal or clock oscillator.

Additionally using the WCO, you can also use the RTC component on the PSoC4 which doesn't use additional TCPWM or UDB resources.  With the RTC, you can set it to provide an interrupt at long time intervals (down to one second) and it can be still operational in lower power modes where as the TCPWMs and UDBs cannot operate beyond active and 'low power' modes.

Len

1 of 1 people found this helpful
• ###### 9. Re: Timer parameter calculation

Hi...

In general, it is quite common to use a WCO with very good PPM accuracy for long timing such as the 10 minutes you are targeting.

The watch crystal is usually cheaper than a high frequency crystal or clock oscillator.

I tried with this above...I have used 32768 Hz crystal at pin 32 & 33 with 10pf capacitor...and set the timer for 5 min.

still i get the 2sec lag..

Here i have attached sample code...Please have a look if anything is missed in setting

HFC remains at IMO

• ###### 10. Re: Timer parameter calculation

BhSa,

I downloaded your project.  The reason that you still get the 2 sec lag is that you are still using the IMO clock (24MHz +/- 2%) as your clock source.  This is the case even when you enable the 32.768KHz WCO.

Sadly the part you have chosen (CY8C4025AZI-S413) does not have the ability to connect the WCO output to the Timer.

Also sadly, it appears not to have the ability to connect the WCO to the LfClk which in turn would allow you to source to the WDT (WatchDog Timer) so that you could source it to the RTC.

Here is a snip from the CySysClkWcoStart() function:

```    /* \note In PSoC 4000S / PSoC 4100S / PSoC Analog Coprocessor devices
* WCO cannot be a source for the LFCLK.
*/
```

Other PSoC4s have this ability but not the one you have chosen.  The part you have is a PSoC 4000S.

Personally I don't see why the part has a WCO since I cannot find anyway to use the output of the WCO using HW or SW.

Maybe someone else monitoring this forum knows how to use it.

Using this part, the only way to improve the accuracy is to use the external Clock (ExtClk) connected to a crystal oscillator.

Len

• ###### 11. Re: Timer parameter calculation

Hello BhSa_4078721,

For the MPN (CY8C4025AZI-S413) that you are using it is not possible to use WCO for trimming the IMO.

Also, LFCLK can only be sourced from ILO even though WCO is available. Since RTC can only be clocked with devices that have WDTs or Deep Sleep Timers, if WDT which is connected to LFClk cannot be sourced by WCO, then it basically means for this particular device, RTC cannot be used with WCO.

Thus, the only option left in this case to get a high accuracy clock is to use an EXTCLK.

EXTCLK can be provided to PSoC 4 by providing a clock signal as input through one of the pins.

The allowable range of external clock frequencies is 1–48 MHz. Please note that the device always starts up using the IMO and the external clock must be enabled in user mode; so the device cannot be started from a reset, which is clocked by the external clock. When manually configuring a pin as the input to the EXTCLK, the drive mode of the pin must be set to high-impedance digital to enable the digital input buffer.

Best Regards

Ekta