Chapter6 Cont

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Working with Time:

Interrupts, Counters and Timers

Chapter 6, Cont…
Working with interrupts

1. Start the ISR at the interrupt vector, location 0004.


2. Enable the interrupt that is to be used, by setting the enable bit in the
INTCON register.
3. Set the Global Enable bit, GIE.
4. Clear the interrupt flag within the ISR.
5. End the ISR with a retfie instruction.
6. Ensure that the interrupt source, for example Port B or Timer 0, is
actually set up to generate interrupts!
#include p16f84A.inc
org 00
goto start
org 04 ;Interrupt Service Routine starts here
goto Int_Routine
org 0010 ;Initialise
start bsf status,rp0 ;select bank 1
movlw 01
movwf trisb ;portb bits 1-7 output bit 0 input
movlw 00
movwf trisa ;porta bits all output
bcf status,rp0 ;select bank 0
bsf intcon,inte ;enable external interrupt
bsf intcon,gie ;enable global int
bsf option_reg, intedg
wait movlw 0a ;set up initial port output values
movwf porta
movlw 15
movwf porta
goto wait
org 0080
Int_Routine ;Interrupt Service Routine continues here
movlw 00
movwf porta
bcf intcon,intf
retfie
end
Moving to multiple interrupts

In PIC16F84A we have only one interrupt vector (04H).


Therefore, if more than one interrupt the programmer must
write the ISR so that at its beginning its ISR by testing the
four interrupted flags and determines from this which one
has been called.
interrupt btfsc intcon,0 ;test RBIF
goto portb_int
btfsc intcon,1 ;test external interrupt flag
goto ext_int
btfsc intcon,2 ;test timer overflow flag
goto timer_int
btfsc eecon1,4 ;test EEPROM write complete flag
goto eeprom_int
portb_int
place portb change ISR here
bcf intcon,0 ;and clear the interrupt flag
retfie
ext_int
place external interrupt ISR here
bcf intcon,1 ;and clear the interrupt flag
retfie
timer_int
place timer overflow ISR goes here
bcf intcon,2 ;and clear the interrupt flag
retfie
eeprom_int
place EEPROM write complete ISR here
bcf eecon1,4 ;and clear the interrupt flag
retfie
Context Saving
What if the interrupt is happed when execution the program?
What will happed to the work and the status registers? Ex. If
the interrupt is happened in the first example at subroutine
Wait at movlw 0a.

The CPU will store the PC and jump to ISR to execute it.
When it retfie, the PC will be index to next instruction that is
movwf porta, BUT Unfortunately with A NEW value for the
W register and Status Register Since we did not save the
content of them before executing the ISR.

Therefore, the content of the W register and Status register


should be stored at the beginning of any ISR and re-loaded
again at the end of the ISR before retfie.
Int_Routine
; These Three Instruction to Store the W and Status Registers
movwf W_temp ;Copy W to TEMP register,
swapf status,0 ;Swap status to be saved into W
movwf status_temp ;Save status to STATUS_TEMP register
bcf status,5 ;ensure we are in Bank 0
movf rhi,0 ;output higher 4 bits to DAC
movwf porta
movf rlo,0 ;output lower 4 bits to DAC
movwf portb
; These Instructions to reload the values of W and Status Registers again.
swapf status_temp,0 ;Swap nibbles in STATUS_TEMP register
;and place result into W
movwf status ;Move W into STATUS register ;set bank to original
;state
swapf W_temp,1 ;Swap nibbles in W_TEMP and place result in
;W_TEMP
swapf W_temp,0 ;Swap nibbles in W_TEMP and place result into W
bcf intcon,intf
retfie
The Microchip recommend to use the previous PUSH and POP for
the W and Status register to insure not affecting the value of the
status register.
The SWAPF instruction will take the lower 4 bits and swap them with
the Upper 4 bits. Example: f= 0010 1100 after swapping it will be
1100 0010

You might also like