Timer interrupt

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 I want to use the timer interrupt of say 50ms... .. how to configure for this..i didnt ger the logic,,, 

   

 

   

i have give 1 Mhz clock to timer midule,, and write the 50000 count in period.. so it is 50ms count period.. but i am getting interrupt on every count... i have configured interrupt on tc mode...

   

so i think i should get interrupt on zero count if it is down counter,, but i am not getting..

0 Likes
64 Replies
Anonymous
Not applicable

 my code is like this..

   

 

   

   

LCD_Char_1_Start();

   

Timer_1_Start();

   

Timer_1_WritePeriod(50000);

   

isr_1_Enable();

   

isr_1_Start();

   

   

LCD_Char_1_Position(0,0);

   

LCD_Char_1_PrintString("      WELCOME      ");

   

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

   

    for(;;)

   

    {

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 i have configured like this.. i have attache image

0 Likes
Anonymous
Not applicable

Hi Dharmin,

   

 

   

You can set the interrupt on Terminal Count on as shown in the figure below.

   

 

   

   

 

   

 

   

 

   

Next in the isr.c file generated, the "Timer_1.h" file has to be included and the variables which are to be used between the isr and the main( ) are declared as extern. Example, a variable named "flag" is declared as extern. 

   

This section has to be written between the #START and #END section. If written outside this section, it will be erased upon build. The snap below shows the same.

   

 

   

   

 

   

 

   

 

   

Now, the code which has to be implemented in the ISR has to be written within CY_ISR( ) function between the #START and #END section.

   

Care must be taken to read the Status Register of the Timer Component in order to clear the Interrupt bit (as it is a sticky bit). Then the code can be written as per your logic. The snap-shot is provided below.

   

 

   

   

 

   

This should get your project working.

   

 

   

Another way would be to connect the Interrupt component directly to the TC Terminal present on the Timer component. In this case, it is not required to enable Interrupt in the Timer component and also reading the Status Register in the ISR is not required.

0 Likes
Anonymous
Not applicable
        hello sir, i am using stepper motor controller using the cypress appilcation note, and code i have one que: how to decide the clock of the PWM module for given RPM, i am not able to get full 200 rpm by your application code,,,,   
0 Likes
Anonymous
Not applicable

The speed at which a stepper motor rotates is determined by many factors. One of them is the rate at which the coils are pulsed. In the PSoC3 project a timer generates periodic interrupts that generate each step (or microstep) of the motor. This timer can be used to run the motor at a specific speed, or to a specific position (exact number of steps). To set the speed of the motor, the interrupt period of the timer is updated by firmware.

   

Also note that every motor has its own limit on speed due its construction, size, number widning in coil and so on. The same will be mentioned in the datasheet of the motor.

0 Likes
Anonymous
Not applicable

but if i am changing the PWM clock, perforamance changed,,,, if i increse the PWM clock,, higher RPM can get,, so what is the rule?

0 Likes
Anonymous
Not applicable

Hi Dharmin,

   

 

   

The speed of a stepper motor depends on the delay between two subsequent steps. Smaller the delay, faster the steps and hence higher the speed.

   

If you want to increase the speed, then decrease the Time Period in the Timer Component.

   

The calculation will become straight forward once you get the specifications of the stepper motor from its datasheet. If it moves 1.8 degrees per step, then 200 such sequence will give one complete rotation (360 degrees).

   

You can use the appropriate clock depending upon the speed required in your application.

0 Likes
Anonymous
Not applicable

i know this about the timer to change the RPM, but the problem is with PWM clock,, if i change the timer interupt according to 150RPM ,torque of motor reduced, and at 200 RPM it need the starting torqe,, if i change the PWM clock,, the reulse get better,, but no proper,, still torque is so low..

0 Likes
Anonymous
Not applicable

Hi dasg, is there any complete PSoC3/5 example code on how to use timers?

0 Likes
Anonymous
Not applicable

PSoC timer is very easy to use. I think the best way is to build a simple application with a timer to turn on/off an LED. The software should be very straght forward. and should get you hands on experience.

0 Likes
Anonymous
Not applicable

For each component in PSoC Creator, there is an associated simple code example. This can be found as follows,

   

component (right click) --> Find Example project. I hope this helps. Moreover reading through the component datsheet will explain the operation and configuration of the timer component.

0 Likes
Anonymous
Not applicable

I can not see any pictures in dasq's post, does anyone else have silimar problem?

   

Anyway, I need to build timer, so I configure the count down to be 0.5sec. However, the interrupt stop being triggered after a while. If I raise the timer priority, then the UART interrupt wouldn't be triggered. It seems that I always have problem with the lowest priority interrupt. Any suggestions?

0 Likes
Anonymous
Not applicable

some update, I change the timerISR priority to be 5, higher than UART interrupt and my DAC_I2C. When I am running in debug mode, the interrupt triggered fine, however, if I let it run freely for a while, then I hit pause, then set break point inside timer interrupt, it will not trigger any more.

0 Likes
Anonymous
Not applicable

Can you test JUST the timer part first. ie just run the code to start and run the time and it's interrupt, If it goes well. Make sure it works correct by itself first before adding other functions to the firmware. See how it goes.

0 Likes
Anonymous
Not applicable

OK, let me build a new project with just timer and give it a try.

   

Before I have SPI, ADC, I2C and UART interrupts and I had so much trouble to get them working all at the same time. It seems if I have two interrupts at same priority, one of them will fail.

   

Anyway, let me try this see what will happen

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I setup a test project with just timer itself with 1kHz clock and Period sets to 500, so it gives me 500ms. My interrupts is set "On TC"

   

The timer suppose to stop after one hour, but after a few seconds, the timer reaches the maximum value.

   

I am attaching my main.c below.

0 Likes
Anonymous
Not applicable

forgot to mention, I only set flag in the isr routine like below:

   

what is wrong here. It seems the interrupt triggered very fast, not 500ms per interrupt, which is just like the first person's problem.

   

   

CY_ISR(TimerISR_Interrupt)

   

{

 

   

   

  /* Place your Interrupt code here. */

   

 

          /* `#START TimerISR_Interrupt` */   

  gRunTimerProcess = DEF_TRUE;

 

      /* `#END` */   

 

 

 

  TimerISR_ISR_PATCH();

 

      /* PSoC3 ES1, ES2 RTC ISR PATCH */          #if    (CYDEV_CHIP_FAMILY_USED == CYDEV_CHIP_FAMILY_PSOC3)      #if    ((CYDEV_CHIP_REVISION_USED <= CYDEV_CHIP_REVISION_3A_ES2) &&    (TimerISR__ES2_PATCH ))       #endif   

 

      #endif   

}

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Would you mind to post your complete project in zipped form here, there's more than only the main.c in a project.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I noticed that in dasq's response:

   

"Care must be taken to read the Status Register of the Timer Component in order to clear the Interrupt bit (as it is a sticky bit). "

   

is that the cause? Too bad, I can not see any of the attached pictures in his post. They are just a small red cross.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is the zip for entire project.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Yes, I see: you must clear the interrupt source by reading the timer-status-register in your interrupt-routine.

   

I use for (all my) interrupts a different  approach. I never insert code in the ISR.C files but I declare my own interrupt routines using

   

CY_PROTO (IntRoutine); 

   

void IntRoutine(void)

   

{

   

   Timer_ReadStatusReg(); // I Don't remember the exact name of the function, look at datasheet, please

   

   MyIntflag = TRUE;

   

}

   

in main()

   

Interrupt_StartEX(IntRoutine);

   

 

   

So no including or external vars are used

   

 

   

Hope it helps

   

Bob

0 Likes
Anonymous
Not applicable

 I think it is a good pratise to always redirect the build in routines to  one that writen yourself. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The most profit you get from that style is, when you switch from PSoC3 to PSoC5 you have to change --- nothing at all ---

   

Otherwise you have to insert all your changes again (don't forget any bit).

   

Just to repeat the main reason for my reply: the timer interrupt (Tc or compare) stays set in the status-register, so it can be examined after the interrupt occured. When it is not cleared, the interrupt is risen again when the interrupt-routine finishes wiht its RETI - instruction.

   

The datasheet of timer explains that some of the bits in the status-register are "Sticky", they do not reflect the actual state, but get set and stay set until the status-register is read. So the read of the status-register in the interrupt-routine is a MUST-DO !

   

Merry x-mas and may your code never fail

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello,

   

I've also had problems with Timer Interrupt. When you use the UDB Timer, all works fine. The problem appears with the FF(Fixed Function) Timer, as the "CY8C38xxx Errata.pdf" says:

   

44. Fixed Function Timer ISR ■    Problem Definition
Using an external clock supplied to an I/O pin may result in an ISR becoming executed multiple times for a single interrupt condition. This issue does not apply when using a clock generated internal to the chip.

   

But even when you use an internal CLK source (ILO 1Khz in my case) the problem persists. I haven't achieve make the FF Timer works properly. I attach the Cypress Timer Interrupt example. By default, a UDB timer is selected. In order to try what I'm saying, it is necessary change to FF Timer. My device is CY8C3866AXI-040ES2.

   

Regards.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I have a PSoC5 processor-module stuck into my developement kit, too bad. I control everything with TightVNC, but it takes a few days until I can change that module in my office. Then I'll try the timer on a PSoC3. With the PSoC5 everything runs fine.

   

 

   

Merry x-mas

   

Bob 

0 Likes
Anonymous
Not applicable

Finally, I have got it work, despite there are many nonsense:

   

- First of all, when you're selecting the CLK source, you are not allowed to choose in the "Existing" sources the ILO. However, you can choose the ILO in the "New" tick.

   

- Plus, also in the "New" tick, if you select the Source "Auto" and configure a 1Khz freq (the same as ILO) the device works wrong showing the problem of the multiple ISR jump.

   

- But the most terrifying is that sometimes, even when the ILO source is selected, the device works wrong. Instead of specify the frecuency, I chose the "Divider" by 1 and the same. But I chose the "Divider" by 2 and... works properly!!!

   

In conclusion, all the problems come from the CLKs sources. Another solution using the FF Timer is use for the Interrupt Controler the Timer TC output.

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I tried some more with a timer and PSoC5 ES1.

   

An Interrupt was connected to TC, Frequency 100 KHz, Period 500, 16bit-wide 

   

Fixed function:  Interrupt comes continously

   

UDB: works as expected.

   

Then I put a d-FF between TC output and ISR input and the fixed-function block works!

   

 

   

Happy coding

   

Bob

0 Likes
Anonymous
Not applicable

Bob,

   

Thanks a lot for the help and the example code.

   

You said you never insert the code in ISR.C files, but I thought as long as you insert the code between their commented # define lines, it will be fine. And you are saying when convert from PSoC3 to PSoC5, it is not the case?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When changing the part ie. from PSoC3 to PSoC5 all the files are generated anew. so all the inserts in the files, though the places are documented well, have to be made anew.

   

My experiences with the API ISR_StartEx(IntRoutine) and the declaration of the routine are very good. Especially if you consider, that some kinds of interrupts have to be cleared, the chance of forgetting somthing is there and then Murphy's law will get you!

   

 

   

Bob

0 Likes
Anonymous
Not applicable

When I started to use PSoC3 several months ago, I had so much trouble with these generated code because everytime I made some changes, Creator will erase my code... Until one day I found that I suppose to put my code between the # define lines. Any way, I think it is a good idea to take control. Well, this means I need to go back to change all my code.

   

I have several interrupts in my projects, include ADC, UART, SPI, Timer, and I2C. When I search " ReadStatusRegister" it seems only Timer has it, not any other interrupts. Does this mean I don't have to manually clear the flag for other interrupts?

0 Likes
Anonymous
Not applicable

never mind my question.

   

I did some digging, and it seems they all have some sort of clear-on-read status registers.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

This is beyond my knowledge of PSoCs. Many components have a state-register that can be red, if it has something to do with a bit to clear a generated interrupt depends on the implementation. I'm afraid, we have to study the datasheets intensely (sigh).

   

But there are new upcoming components and you may even create your own ones, which in turn may contain a status register to which other hardware (ISR) can be connected.

   

You can see it as a matter of programming style which is very (capital VERY) important for clean and easy readable code, which is easier to maintain and so contains less bugs and so - is cheaper.

   

Bob

0 Likes
Anonymous
Not applicable

Bob,

   

In your example, what's the benefit of using DFF between tc and interrupt comparing to tc output directly to interrupt?

   

Thanks,

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

He, he,

   

without the DFF it doesn't work at all! have a look at the previous posts.

   

or

   

If you can check it in hardware (should work with a PSoC3 as well) remove the FF, set the Block to Fixed function (doesn't work as expected) set the block to UDB - works. that's the behaveour of my PSoC5 ES1 silicon.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I need to search the DFF thing. The example comes with creator does not have DFF.

   

I tried both, with it or without it, neither of them works. With DFF, and uncheck Interrupt on TC in timer configuration page as you have in your example,  the interrupt only triggers once, then it would not trigger any more. I have break point inside the timer_isr, and it only stops once.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I have the Run Mode in Continuous, so it should not just trigger once then stop. I have everything setup the same way as you have in the example except my clock is slower so the Period is 500ms.

   

This is frustrating... I am attaching the main.c file, can you help and take a look?

   

Thanks,

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Better post the complete project, the main() looks ok as far as I can see. I live in UTC+1 Timezone, so I'll go to bed right now.

   

Until tomorrow

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is the entire project. I tried configuring timer as Fixed Function and UDB, did not make any difference.

   

Why the timer I added imprint "Prototype" on top of it?

   

I am off for the rest of the week, so I will have to try your solution next week, but thanks so much for your help!

0 Likes