Proto type PIC 16F628 demo board..

PIC16F628 Timer Interrupt Programming Tutorial

by Lewis Loflin

The complete assembly file for this demo is at TRM0_IRQ.asm

The Microchip PIC series of micro-controllers have a number of programmable interrupt sources. In this tutorial we will use 8-bit timer 0 (TMR0) to generates an interrupt every ~16.4 mSec. which increments variable CNT (0x23) which at 61 counts toggle an LED ON-OFF on RA0.

PIC16F628A option and interrupt control registers

The 8-bit option register (81H BANK1) controls TMR0 while interrupt control register (0x0B BANK0) as follows in setup section by clearing and setting bits in the appropriate registers. See above for labels.

	; setp TMR0 interrupts
	banksel OPTION_REG ; Reg. 0x81 BANK1
	movlw b'10000111' 
	; internal clock, pos edge, prescale 256
	banksel INTCON ; Back 0x0B BANK1
	bsf INTCON, GIE ; enable global interrupt
	bsf INTCON, PEIE ; enable all unmasked interrupts
	bsf INTCON, T0IE ; enable TMR0 interrupt
	bcf INTCON, T0IF ; clr TMR0 interrupt flag 
	; to turn on, must be cleared after interrupt
	clrf	CNT ; RAM location 0x23

Using a 16 mHz crystal this is divided by 4 then divided by 256 will increment TMR0 every 64 uSec. and after 255 increments times out to 16.32 mSec. will generate an interrupt.

	ORG     0x000             
	; processor reset vector
	goto    setup              
	; go to beginning of program
	ORG     0x004             
	; interrupt vector location
; isr code can go here or be located 
; as a call subroutine elsewhere

	movwf   w_temp            
	; save off current W register contents
	movf	STATUS,w          
	; move status register into W register
	movwf	status_temp       
	; save off contents of STATUS register
	movlw d'61' 
	; approx 61 interrupts = 
	; 1 sec. at 16 mHz crystal
	subwf CNT, w
	btfss STATUS, Z ; i = 10
	goto $+4
	call toggle 
	; state = !state PORTA, 0
	clrf CNT
	goto $+2
  	incf CNT
	bcf INTCON, T0IF 
	; clr TMR0 interrupt flag	

	movf    status_temp,w     
	; retrieve copy of STATUS register
	movwf	STATUS           
	; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          
	; restore pre-isr W register contents
	; return from interrupt


Referring to above the interrupt vector is at location 0x04 while the reset vector is at 0x00. The code below "ORG 0x004

Every interrupt generated by TMR0 vectors to ORG 0x04 executing code below it - will tehn save W and STATUS registers. Initialized to 0 variable CNT is incremented on each pass, then by subtracting 61 and checking the Z flag in the STATUS register when equal will set the flag, clear.

If not equal CNT is incremented, TMR0 IRQ flag is cleared, W and STATUS registers are retrieved. Then a return from interrupt (retfie) returns to the main program. The process is repeated.

If during the pass CNT = 61 the Z flag is set, the state on the LED on RA0 is toggled, CNT is set to 0, TMR0 IRQ flag is cleared, W and STATUS registers are retrieved. Then a return from interrupt (retfie) returns to the main program.

See How I got into Electronics banner.

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