PIC1684A interrupt map.
Fig. 1 PIC1684A interrupt map.

Programming PIC16F84A-PIC16F628A TMR0 Interrupts

by Lewis Loflin

We utilize TMR0 interrupts to increment a counter, then when reaching a select number the program toggles the state of a LED on PB0. this is very easy to program and we will walk through the steps.

To fully understand PIC16FXX interrupts see Programming PIC16F84A-PIC16F628A Interrupts by Example. Note the partial code below


	ORG  0x004  ; interrupt vector
	;ISR code 

	DECFSZ CNT 
	GOTO $+7
	MOVLW B'00000001'
	XORWF PORTB, F
	MOVLW D'10' ; 50mS * value
	MOVWF CNT
	MOVLW D'60' ; preload value
	MOVWF TMR0
	bcf INTCON, T0IF ; clr TMR0 interrupt flag	
	retfie  ; return from interrupt

SETUP	; setup registers		
	; setp TMR0 operation
	; internal clock, pos edge, prescale 256
	movlw b'10000111' 
	movwf OPTION_REG
	BCF	STATUS, RP0 ; BANK 0	
	; 256uS * 195 =~ 50mS
	; 255 - 195 = 60
	MOVLW D'60'
	MOVWF TMR0 ; preload timer value
	; setup TMR0 INT
	bsf INTCON, GIE ; enable global interrupt
	bsf INTCON, T0IE ; enable TMR0 interrupt
	bcf INTCON, T0IF ; clr TMR0 interrupt flag to turn on, 
	; must be cleared after interrupt
	MOVLW D'10' ; 50mS * 20 = 1 Sec.
	MOVWF CNT

goto LOOP

The code is self-explanatory and is a combination of earlier coding. When TMR0 overflows the interrupt decrement CNT until it reaches zero. Otherwise most of the code is skipped (GOTO $+7), except the TMR0 interrupt flag is always cleared to 0. See the following:

PIC16F628A connected to 4 LEDs and 4 switches.

Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.