Professional Documents
Culture Documents
Programming
Interrupt I/O
Polling I/O
Start device and poll for completion
Program Execution
.
.
Start Device
Input status
Poll and Wait
Until Device Ready
Interrupt I/O
Start device and continue program execution
Program Execution
.
.
Start Device
.
.
.
.
ISR
Procedure
For device
Service
Routine
.
RETURN
16F87X Interrupt
The PIC16F87X family has up to 14
sources of interrupt.
Timer0 interrupt
External interrupt on RB0/INT (programmable edge trigger)
PortB change interrupt (RB7:RB4)
Parallel Slave Port interrupt
Analog to digital interrupt
UART Receive interrupt
UART Transmit Empty interrupt
Synchronous Serial Port interrupt
Compare/Capture/PWM-1 interrupt
Timer2 interrupt
Timer1 interrupt
EEPROM interrupt
Bus Collision(I2C) interrupt
Compare/Capture/PWM-2 interrupt
CpE 112 : Klinkhachorn
SLEEP instruction.
If enabled, the Watchdog Timer will be cleared but
keeps running
the PD/ bit (STATUS<3>) is cleared
the TO/ (STATUS<4>) bit is set
up the CPU
External reset input on MCLR/ pin
Watchdog Timer Wake-up (if WDT was
enabled)
Interrupt from INT pin, RB port change, or
some peripheral interrupts (e.g. PSP read/write,
TMR1, CCP, SSP, USART RX, TX, A/D, and
EEPROM write operation
CpE 112 : Klinkhachorn
be set
If global interrupt is disabled
The CPU continues execution at the instruction after
the SLEEP instruction
Code Template
Explanatory remarks
Assembler directives
Equates to give names to numbers
Variable definitions
Reset and interrupt vector
A table
Mainline code and its subroutines
Initialization code
Interrupt service routine
Systematic handling of W and STATUS
Polling routine
Specific interrupt handling subroutine
org H000
goto mainline
;Reset vector
;start main program
org H004
;Interrupt vector
goto IntService ;jump to ISR
;if the ISR is located in the same Program
; Memory bank of the main program otherwise
; save W, Status, PCLATH
; then select the proper bank (PCLATH<4:3>)
;before jump to ISR!
W_temp
STATUS,W
; store W in W_Temp
;move status to W
;without affecting z bit!
movwf Status_Temp
;save STATUS
;Polling interrupt flag and goto specific interrupt handler routines
;do what it need to be done
.
;on exit
;restore STATUS and W then return
swapf
Status_Temp,W
;restore STATUS (swap nibbles)
movwf STATUS
;without affecting z bit!
swapf
W_temp,F
;swap W_temp
swapf
W_temp, W
;swap again to restore W
;without affecting z bit
retfie
;Return to . and reenable the interrupts
CpE 112 : Klinkhachorn
;Reset vector
;start main program
org H004
;Interrupt vector
movwf
swapf
movwf
swapf
movwf
bsf
goto
;store W in W_Temp
;move status to W without affecting z bit!
;save STATUS
;move PCLATH to W without affecting z bit!
;save PCLATH
;Switch to page 1
W_temp
STATUS,W
Status_Temp
PCLATH, W
PCLATH_Temp
PCLATH,3
IntService
;jump to ISR
m ovwf STATUS
swapf W_Tem p,F
swapf W_Tem p, W
Retif
INTF(INTCON<1>) is set
can be disabled by clearing enable bit INTE (INTCON<4>)
Flag bit INTF must be cleared in software in the interrupt service
One cycle
ON/OFF
determines CCW/CW
CpE 112 : Klinkhachorn
External Inputs
PIC16F877
TIMERs/COUNTERs
PIC16F877
Timer0
Timer1
Timer2
Timer0 Module
8-bit timer/counter
Readable and writable
8-bit software programmable prescaler
Internal or external clock select
Interrupt on overflow from FFh to 00h
Edge select for external clock
Timer1 Module
16-bit timer/counter (TMR1H,TMR1L)
Readable and writable (both)
Internal or external clock select
Interrupt on overflow from FFFFh to 0000h
Reset from CCP module trigger
Programmable Prescaler (1,2,4, and 8)
Sync and Asyn Counter mode
CpE 112 : Klinkhachorn
Timer1Block diagram
Timer1 Oscillator
Low power Oscillator rated upto 200kHz
Primary intended for a 32kHz
Timer1 - TMR1H:TMR1L
The register pair (TMR1H:TMR1L)
Timer2 Module
8-bit timer (TMR2)
8-bit period register (PR2)
Readable and writable (both)
Interrupt on TMR2 match of PR2
Programmable Prescaler (1,4, and 16)
Programmable postscaler (1 to 16)
Can be use as the PWM time-base for PWM mode of the
CCP module
SSP module optional use of TMR2 output to generate clock
shift
CpE 112 : Klinkhachorn
Timer2 - TMR2
The prescaler and postscaler counters are