Continue to Site

# Understanding Timer Interrupt

Status
Not open for further replies.

#### Mithun_K_Das

I want to understand about timer interrupt. What actually I wanna know if it is that timer which will create an interrupt after a pre-fixed time interval whatever the MCU was working?

Need help.

#### rohitkhanna

##### Banned
In general terms - the answer is YES. Provided of course that the interrupt has been enabled at all levels.

#### Mithun_K_Das

can anyone provide any good and easy to understandable document and examples?

#### wp100

can anyone provide any good and easy to understandable document and examples?

Hi,

When you set a Timer running, it is a piece of hardware/software separate from your main program loop.

When the timer is full,depending on the conditions you originally specified, it will set an Interrupt Flag.

This can be tested for from withing your program loop.

Often its left to the control of the Interrupt Service Routine, which, if activated, will suspend the main program loop code and invoke the ISR code, which would update a certain function due to the timer being flagged.
The Timer is typically reset and started again, the ISR code then hands back control to the main program loop.

For greater detail just google 'pic timer interrupts tutorial' + whatever complier you are using.

Mithun_K_Das

### Mithun_K_Das

Points: 2

#### Mithun_K_Das

Nice explanation. But I need to try first.

- - - Updated - - -

If you can provide an example code it will be easy to understand. Thanks in advance.

#### wp100

Nice explanation. But I need to try first.

- - - Updated - - -

If you can provide an example code it will be easy to understand. Thanks in advance.

There are loads of sites out there giving clear details and sample code in C

#### T3STY

##### Full Member level 4
I don't agree that the timer is a separated piece of hw/sw as wp100 says. Yes, the timer circuit makes an interrupt after a pre-fixed time interval; but the interval depends on the main clock of the PIC device and on the software that will release the interrupt itself. The time is managed via prescalers, which can make the timer interrupt occur every 1:2 to 1:256 of the main clock. If the prescaler is set to 1:2 the timer will make an interrupt every 500 kHz on a 1MHz clock, while on a 4MHz clock the timer interrupt will occur every 2MHz with the same prescaler value. Also, when an interrupt occurs the interrupt routine has to release the interrupt or the main sw of the PIC won't be able to proceed, nor the interrupt itself will be triggered again until the previous is released.
As you can see it's not as hw/sw separated as it seems.

#### Mithun_K_Das

Will the Timer Interrupt work if there is a delay called in the main loop?

#### Mithun_K_Das

Yes!!! I've done it. Its working....

here is the code I did with PIC16F73
Code:
void interrupt()
{
RB2_bit = ~RB2_bit;
//Delay_ms(10);
TMR0 = 0;                            //reset TIMER0 value;
INTCON = 0x20;                       //clear T0IF, Bit T0IF=0, T0IE=1;
}

void main()
{

TRISB = 0x00; // Set PORTB direction to be output
PORTB = 0x00;    // Turn OFF LEDs on PORTB
TRISA = 0b11110000; // RA5 is input only

// Timer settings
OPTION_REG = 0b00000100;                   // Set timer TMR0;
TMR0 = 0;
INTCON = 0xA0;                       // Disable interrupt PEIE,INTE,RBIE,T0IE
while(1)
{
RB5_bit = ~RB5_bit;
Delay_ms(6000);

}                       //Endless loop;
}                                    //End.

Can I use more than 1 timer interrupt at the same MCU?

V
Points: 2

#### doraemon

##### Super Moderator
Staff member
Hello!

Note that on some processors, you don't necessarily need a while(1) loop.
The processor will sleep as long as there is no interrupt and wake up only when
there is one.

Here is a (modified) sample code from TI:

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "msp430x54xA.h"

void main(void) {
WDTCTL = WDTPW + WDTHOLD;               //  Stop watch dog
TA1CCTL0 = CCIE;                        //  Enable timer interrupt
TA1CCR0 = 50000;                        //  Load the timer with some value
TA1CTL = TASSEL_2 + MC_1 + TACLR;       //  SMCLK, upmode, clear TAR
__bis_SR_register(LPM0_bits + GIE);     //  Enter LPM0, enable interrupts
}

// Timer A0 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void) {
//  Do your interrupt processing here
}

Have fun!

Dora.

#### Mithun_K_Das

Good job! Its really amazing. but I was full before, didn't used as I didn't tried. Thanks everyone. can you give me any example that having 2 timer interrupts? and also what is the effect of using 2 different timer interrupt? will they harm each other? or will they work in parallel?

#### Tahmid

The two timers will work independently - they won't affect each other's operation.

You need to make 2 changes now. You obviously first need to initialize both timers as required in the main code (instead of for 1 timer). Then, in the interrupt you need to check which timer interrupt has occurred. You do this by checking which interrupt flag has been set.

Code:
void interrupt(void){
if (TMR0IF_bit == 1){
//Do whatever
//.....
//.....
TMR0IF_bit = 0;
}
if (TMR1IF_bit == 1){
//Do whatever
//.....
//.....
TMR1IF_bit = 0;
}
}

Hope this helps.
Tahmid.

#### wp100

Good job! Its really amazing. but I was full before, didn't used as I didn't tried. Thanks everyone. can you give me any example that having 2 timer interrupts? and also what is the effect of using 2 different timer interrupt? will they harm each other? or will they work in parallel?

Hi,

As well as the Timers many other functions of the Pics have interrupts, so they all have to run along side each other.

Look at the Datasheet Interrupt Section to see a diagram of them all and how they are used.

Just try using another fuction with interrupt like Timer1 or Timer2 , the coding is very similar, just that you have to address things for the new Timer 1 or 2.

#### Mithun_K_Das

How it can be generate a timer1 interrupt for pic16f73? any example in MicroC?

#### T3STY

##### Full Member level 4
The same way you do it for all the other PICs, the examples are above. If you mean how to manually generate an interrupt it depends on which PIC you're using. Basically, you usually just set to 1 an interrupt flag and it will jump to the routine intended for that interrupt; for instance, if you want to manually create a TMR0 interrupt, just use T0IF=1; which will run the TMR0 interrupt routine.

Status
Not open for further replies.