# PIC16F84A TIMER0

#### Damian P

Joined Mar 23, 2020
10
Hi everyone,
I was wondering if anyone could help.

I have written a little code, essentially I'm using External RB0 interrupt, Port change RB4 interrupt and TIMER_0 interrupt.

When bit 5 of INTCON is set to 1 (Enable timer), my program jumps automatically to the ISR; skipping the while loop.
But If the bit 5 INTCON is set to 0, the program works fine and I can use my OTHER interrupts.

The code:

C:
void setup(void);

void __interrupt() inSetup(void){

DO SOMETHING !

return;
}

//Main Function

void main(void)
{
setup();

while(1){

}
}

void setup(void) {

// Port configuration
TRISB = 0B11111111;// Setting 'B' as input
TRISA = 0B00000;// Setting 'A' as output
OUTPUT_PIN1 = 0;
OUTPUT_PIN2 = 0;
// TIMER_0 & INTERRUPT SETUP
INTCON = 0b10100000;
OPTION_REG = 0b10000111; //bit 2 - bit 0 pre-scaler assignment 1:2,
}
Mod edit: code tags

Last edited by a moderator:

#### BobTPH

Joined Jun 5, 2013
3,308
An interrupt handler must clear the interrupt flag before exiting. If it does not, it will just be called again immediately.

Bob

#### Damian P

Joined Mar 23, 2020
10
Hi Bob,

I've tried clearing the flag right after I set the bit 5 in INTCON, no luck; still jumps straight to ISR and for some reason stops there.

#### jpanhalt

Joined Jan 18, 2008
11,088
Hi Bob,

I've tried clearing the flag right after I set the bit 5 in INTCON, no luck; still jumps straight to ISR and for some reason stops there.
You must clear the flag BEFORE enabling the interrupt.

#### upand_at_them

Joined May 15, 2010
766
Does your compiler handle context saving/restoring for you? Also: Why are you using an ancient chip? Modern PIC's are better in EVERY way, including cost.

#### JohnInTX

Joined Jun 26, 2012
4,438
Hi Bob,

I've tried clearing the flag right after I set the bit 5 in INTCON, no luck; still jumps straight to ISR and for some reason stops there.
As @BobTPH said, you must clear timer interrupt flag INSIDE the interrupt routine then do your processing. When the interrupt routine exits, main will execute until the timer rolls over again and raises T0IF to trigger another interrupt.
When setting up, clear the interrupt flags before enabling interrupts.
You don't need a return instruction at the end of the interrupt routine. The compiler will do that for you and manage the interrupt re-enable as well.
As for writing for an old chip, you can write good code or bad code for any chip. The 'F84 will foul your plans as well as a PIC32 if you don't use it correctly. The basics are the same for either. Once you have those settled, you would be wise to consider a newer part.

#### Damian P

Joined Mar 23, 2020
10
if(INTCONbits.INTF){ // Test for EXTERNAL interrupt flag

OUTPUT_PIN1 = 1;//RA0
delay_traffic();
delay_traffic();
delay_traffic();
OUTPUT_PIN1 = 1;//RA0
delay_traffic();
delay_traffic();
delay_traffic();
OUTPUT_PIN1 = 1;//RA0

}

#### Damian P

Joined Mar 23, 2020
10
Hi John,

I understand what you saying, but the program gets stuck at this line: "
void __interrupt() inSetup(void){".

It stops at this line and doesn't want to go any further.