Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

TMR0 wrong interrupt occures

Not open for further replies.


Full Member level 5
May 28, 2011
Reaction score
Trophy points
Activity points
i have write the following programme for the TMR0 interrupt.

processor 16f84

include <>

__config _RC_OSC & _WDT_OFF & _PWRTE_ON
org 00h
goto main
org 0x04
goto interrupt
banksel OPTION_REG
movlw B'11000111'
banksel INTCON
bsf INTCON , 7
bcf INTCON , 5
bcf INTCON , 2
banksel TRISA
movlw B'00000'
movwf TRISA
banksel PORTA
movlw B'01010'
movwf PORTA
clrf TMR0
bsf INTCON , 5
goto fin
comf PORTA , f
clrf TMR0


according to the interrupt routine the interrupt should only once comf Porta . as i have not clear the overflow and ser tmr0 interrupt again in intcon after the interrupt occurs. but it is continuously comf PORTa. what is wrong.

I think you need to clear the interrupt flag in the interrupt?

T0IF = 0;

btbass is correct, clear the interrupt flag inside the interrupt routine. If you don't do that, the flag remains set and as soon as you 'retfie' back to the main code the interrupt is seen as still pending and the interrupt routine is called again.

Incidentaly, the value of TMR0 when the interrupt is triggered is 0x00 and you don't pre-load it again with another value so you can leave the 'clrf TMR0' line out of the interrupt routine altogether, the value must already be zero to have reached that line of code.


but i think when interrupt occurs then for the next interrupt i need to set tmr0 interrupt on pin5 intcon again for the next interrupt and need to clear the interrupt falg. if i dont do this the interrupt could not occur again???????

---------- Post added at 09:44 ---------- Previous post was at 09:39 ----------

when i used clrf tmr0
it only set the tmr0 to zero or also clear its prescaler and TOIF bit?

The interrupt Enable bit, once set, stays set until you clear it. So you don't need to set it again.
The interrupt flag is set when the interrupt occurs, and then you need to clear it in the interrupt routine.

The interrupt flag is set when the interrupt occurs, and then you need to clear it in the interrupt routine.

thats mean if i dont clear it the next interrupt should not occurs as the programme is in infinite loop fin:

When the interrupt flag gets set and interrupts are enabled, the program will jump to the interrupt routine. If you don't clear the interrupt flag in the interrupt routine, then when the program returns from the interrupt routine, as the interrupt flag is still set, it will immediately jump back to the interrupt routine again.
So the program is stuck in a never ending interrupt cycle.
You have to clear the interrupt flag before you exit the interrupt routine. Then the program will return and run the main thread untill the interrupt flag is set again.

Use Mplab sim and step through your code. Use the watch window, you will see exactly whats going on.

Not open for further replies.

Part and Inventory Search

Welcome to