[Solved]Could you help me to understand the timer interrupt

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I am trying to understand how timer interrupts work in a microcontroller. I want to understand how the timer interrupt works in any microcontroller Before to understand how a timer interrupt works in a particular microcontroller,

I am assuming there is a timer that starts from 0, 1, 2, 3 . . and ends up to 255. I want the timer interrupt to be generated when timer reaches at 30

  1. When the timer interrupt generates, will the timer increment beyond 30 (30, 31, 32, ... 255)?
  2. When will timer interrupt finish?
  3. Does the processor complete its tasks from O to 30 and After that the task written in the interrupt routine is completed?
 

jpanhalt

Joined Jan 18, 2008
11,087
I want the timer interrupt to be generated when timer reaches at 30
I am only conversant with PIC's, but I believe the principle is the same in others. Details may be different.

When you want an interrupt, you first decide on a condition that causes that. For example, a rising edge, falling edge, a specific input, or count. The simplest counter interrupt is based on when it rollsover from, say 255 to 0. Of course, you can set up counters to rollover on virtually any value depending on the width of the counter. There is often the option for pre- or post-scalers (e.g., 1:2 to 1:256 pre, post, or both). Common widths in 8-bit controllers are 8-bit (256) and 16-bit (65536).

To get an interrupt when count is "30", pre-load the counter with a count of 255-30 (e.g., 225). The count after 30 will cause an rollover and so forth. On PIC's and others (probably) you can set a condition for "matching." When a match between the count and the value in another register occurs, then you get an interrupt. That method is used in creating PWM with the PIC's I have mentioned.

I suggest doing the pre-load method first. For the latter, read about the compare/capture module.

Rather than considering all of the variations, I think it would be considerably easier to grasp the concept if you picked a specific MCU to discuss. The 12F1840 is an enhanced mid-range, 8-bit controller and might be a good place to start. What's important is to pick a specific chip that has interrupts. What you chose really doesn't matter much. NB: the 12F5xx chips and some others do not have interrupts.

What does an interrupt do? When it occurs, the MCU jumps to a specific location in the code ("interrupt vector") and executes the code there. When done, it usually returns to where it was when the interrupt occurred, unless you write code to do something else. That is only practical with MCU's that allow access to the "stack."
 
Last edited:

MrChips

Joined Oct 2, 2009
30,708
There are many different MCUs and there are many different Timer Modules. Each one is different. In order to learn how each one works you need to read the User Manual of the specific MCU.

I will describe the basic function of any Timer Module.

Each module has many different functions and different types of interrupts. Interrupts are triggered by timer events.
A basic 8-bit timer will count from 0 to 255 using a Counter Register. One option may be to count in reverse order (from 255 to 0). One option would be to count from 0 to 255 and stop. Another option would be to count from 0 to 255 and continue.

A basic 8-bit timer will count from 0 to 255 and repeat without stopping.
When the counter overflows from 255 to 0, this is one example of a timer event. If you wish, you may enable this event to trigger an interrupt. The function of the interrupt service routine (ISR) is totally independent of the Timer Module. You can do whatever you want in the ISR.

Another basic Timer Module function is called Output Compare. This uses a separate OC register which you can set from software. As the counter counts from 0 to 255, if the Counter Register matches the value in the OC register, an OC timer event is triggered in the similar manner as an overflow timer event. This in turn can trigger a different interrupt. On some Timer Modules, this feature may be given a different name such as Timer Upper Limit or Terminal Count.

These are only two types of timer events that can trigger interrupts. There are many more timer events and hence interrupts.

Edit: jp beats me to it. He does mention one way of the using the Timer Register without needing OC register. Via software you may preload the Timer Register with any value, for example, 200. The Timer Register will count from 200 to 255 and overflow to 0 triggering a timer event.
 

MrChips

Joined Oct 2, 2009
30,708
What you are showing are the control bits and interrupt flags for each type of interrupt.
For example,

TMR0IE = Timer 0 Interrupt Enable
This must be set by software in order to recognized interrupts from Timer 0.

TMR0IF = Timer 0 Interrupt Flag
This is set by hardware to indicate that a Timer Event causing an interrupt has occurred.

None of this tells you what is the Timer Event that is triggering the interrupt. You need to read the rest of the Timer Module description.
 

jpanhalt

Joined Jan 18, 2008
11,087
Also GIE (global interrupt enable) must be set.

When an interrupt occurs, that is cleared (disabled) so another interrupt cannot occur while the interrupt code is being executed. Then on return from interrupt, it is enabled again. The RETFIE instruction handles that (return from interrupt, enable interrupts). TMR0IE does not change.

TMR0IF is set automatically, but you must clear that in software before returning or it will immediately cause another interrupts.

Note: Some IF's are cleared by reading the register; others require code.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
None of this tells you what is the Timer Event that is triggering the interrupt. You need to read the rest of the Timer Module description.
To generate the timer interrupt many registers have to be set. If you look at the given image carefully, there is a table and in the last column of the table, page numbers and registers information has given.

We have to configure the register given in the table. It is not known from the data sheet which order to configure which register. Do they have to be configured in a sequence?

Looking for Timer 0
Section 20 (page 160)
Screenshot_2020-08-01-18-35-41-622_com.google.android.apps.docs.jpgIMG_20200801_184829.jpg
 

MrChips

Joined Oct 2, 2009
30,708
There is no particular order or sequence required.
Here is an analogy.

Your house has many rooms. Each room has a light switch by the door to turn on the room light. Some switches activate the wall outlet. In order to turn on the bedside lamp you need to have the room switch turned on as well as the switch on the lamp.

The whole house wiring is connected to the service panel. There are many circuit breakers on the panel. One such breaker controls the power to two or more rooms. That breaker must be turned on for the rooms to receive power.

The entire service panel has a main breaker. There is no power to the entire panel and house until the main breaker is turned on. Think of this as the GIE switch (Global Interrupt Enable). No interrupt will be recognized if GIE is off. (Note that on some MCU the GIE is a mask, i.e. the mask must be OFF in order to accept interrupts.)

All appropriate switches must be on for the particular light to turn on. There is not particular order or sequence required to get one light bulb to turn on.
 

jpanhalt

Joined Jan 18, 2008
11,087
To generate the timer interrupt many registers have to be set. If you look at the given image carefully, there is a table and in the last column of the table, page numbers and registers information has given.

We have to configure the register given in the table. It is not known from the data sheet which order to configure which register. Do they have to be configured in a sequence?
No. The sequence per se doesn't matter, but you need to think about it. For example, if you set TMR0IE, while GIE is set, a pending interrupt, say from the TMR0IF will cause an interrupt.

Thus, before enabling GIE (assuming you have only one interrupt), I usually enable TMR0IE and ensure that TMR0IF is clear. That is, if you do it bitwise.

But since it is all one byte, all three can be set in one instruction. In C, that may be different. And, if you are doing it bitwise, clear TMR0IF first*, then enable TMR0IE, then GIE. Or, enable TMR0IE, then clear TMR0IF, then set GIE. In other words, set GIE last after you know everything else is OK.

*You have to remember that TMR0 is free running if you are using the instruction clock, so you may want to zero it first so it doesn't rollover while the other bits are being set.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
No. The sequence per se doesn't matter, but you need to think about it. For example, if you set TMR0IE, while GIE is set, a pending interrupt, say from the TMR0IF will cause an interrupt.

Thus, before enabling GIE (assuming you have only one interrupt), I usually enable TMR0IE and ensure that TMR0IF is clear. That is, if you do it bitwise.

But since it is all one byte, all three can be set in one instruction. In C, that may be different. And, if you are doing it bitwise, clear TMR0IF first*, then enable TMR0IE, then GIE. Or, enable TMR0IE, then clear TMR0IF, then set GIE. In other words, set GIE last after you know everything else is OK.
I have made a few points to generate timer interrupts. That I find important to generate time interrupt

  1. Clear timer interrupt flag
  2. Load the value into the timer0 register
  3. Enable timer 0
  4. Enable timer 0 interrupt
  5. Enable global interrupt

Am i missing something?
 

MrChips

Joined Oct 2, 2009
30,708
I have made a few points to generate timer interrupts. That I find important to generate time interrupt

  1. Clear timer interrupt flag
  2. Load the value into the timer0 register
  3. Enable timer 0
  4. Enable timer 0 interrupt
  5. Enable global interrupt

Am i missing something?
Yes. Do you have an oscilloscope? Having one will help in testing interrupt activity.
If you do not have an oscilloscope then you have to be creative in developing your tests.
 

jpanhalt

Joined Jan 18, 2008
11,087
I have made a few points to generate timer interrupts. That I find important to generate time interrupt

  1. Clear timer interrupt flag
  2. Load the value into the timer0 register (NOTE by responder: The timer starts timing at that instant.)
  3. Enable timer 0 (Note by responder: see below.)
  4. Enable timer 0 interrupt (Note by responder: Why was it ever disabled?)
  5. Enable global interrupt (Note by responder: And return from interrupt)

Am i missing something?
Can you expand on what you mean to "Enable timer 0?" It does not have an on/off bit per se, unlike Timer 1.

That is what I meant by "free running." Yes, I know how to turn it off, but that is not what we are discussing. You can change its input to a pin, and then tie that pin high or low. That pin is used usually for counting.

Here's a question for you:
When TMR0 rolls over and gives an interrupt, what are your plans for resetting it while keeping accurate timings? (Since the 12F1840 has automatic context saving, don't worry about that.)
 

MrChips

Joined Oct 2, 2009
30,708
I don't have a scope. What kind of test are you talking about? I would like to do an interrupt test for a led.
You need to confirm that the ISR (Interrupt Service Routine) is being entered.
You can set a breakpoint in your debugger.
You can also flash an LED for 10ms.
 

jpanhalt

Joined Jan 18, 2008
11,087
I don't have a scope. What kind of test are you talking about? I would like to do an interrupt test for a led.
Be sure the LED is on a pin set to output ( TRIS =1)

Just write your ISR like this (assuming only one interrupt):
1) Toggle that pin (XOR works great for that)
2) Clear TMR0IF
3) Set TMR0 to your pre-load
4) RETFIE
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Here's a question for you:
When TMR0 rolls over and gives an interrupt, what are your plans for resetting it while keeping accurate timings? (Since the 12F1840 has automatic context saving, don't worry about that.)
Why need to pre-load counter register ?

I think If we do not preload the value in the timer register then the timer will start from 0. That's why we have to pre-load the timer
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
Your original question was to count to 30, then rollover. With TMR0 you have to reload the starting count every cycle, or it restarts at 0.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I have a hypothetical question, For example assuming we have counter that run from 0 to 255.

What happens when we need to generate two timer interrupt , first at 30 counts and second at 20 counts. Does it possible? How it would be possible?
 
Top