EXAMPLE 8
Using timer TMR1 and using interrupt
16-bit timer TMR1 is used in this example. By occupying its registers
TMR1L and TMR1H, an interrupt occurs and the number on port B is
incremented. This has already been seen in the previous examples. The
difference is in the program delay which is a bit longer this time
because the prescaler rate is 1:8.
Source Code
;*************************** Header *****************************************
;************* DEFINING VARIABLES *******************************************
cblock 0x20 ; Block of variables starts at address 20h
w_temp ; Variable at address 20h
pclath_temp ; Variable at address 21h
status_temp ; Variable at address 22h
endc
;************************ PROGRAM START ************************
org 0x0000 ; Address of the first program instruction
goto main ; Jump to label "main"
;************************ INTERRUPT ROUTINE **********************************
org 0x0004 ; Interrupt vector
movwf w_temp ; Save register W
movf STATUS ; Save register STATUS
movwf status_temp
movf PCLATH ; Save register PCLATH
movwf pclath_temp
banksel PORTB ; Selects bank containing PORTB
incf PORTB ; Register PORTB is incremented by 1
movf pclath_temp,w ; PCLATH is given its original content
movwf PCLATH
movf status_temp,w ; STATUS is given its original content
movwf STATUS
swapf w_temp,f ; W is given its original content
swapf w_temp,w
banksel PIR1 ; Selects bank containing PIR1
bcf PIR1,TMR1IF ; Clears interrupt flag TMR1IF
bsf INTCON,GIE ; Global interrupt enabled
retfie ; Return from interrupt routine
;************************ MAIN PROGRAM ***************************************
main ; Start of main program
banksel ANSEL ; Selects bank containing register ANSEL
clrf ANSEL ; Clears registers ANSEL and ANSELH
clrf ANSELH ; All pins are digital
banksel TRISB ; Selects bank containing register TRISB
clrf TRISB ; All port B pins are configured as outputs
banksel T1CON ; Selects bank containing register T1CON
bcf T1CON,TMR1CS ; TMR1 counts pulses generated by oscillator
bsf T1CON,T1CKPS0 ; Prescaler rate is 1:8
bsf T1CON,T1CKPS1
bsf T1CON,TMR1ON ; Turns on timer TMR1
banksel PIE1 ; Selects bank containing register PIE1
bsf PIE1,TMR1IE ; TMR1 interrupt overflow enabled
bsf INTCON,PEIE ; Peripheral modules interrupt enabled
; Timer TMR1 belongs to peripheral modules
bsf INTCON,GIE ; Global interrupt enabled
banksel PORTB ; Selects bank containing register PORTB
clrf PORTB ; Clears port B
loop
goto loop ; Remain here
end ; End of program