Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 11


another byte to TXREG for transmitting and perform any other necessary
actions.
The PIC18F4520 has two interrupt vectors. Multiple interrupts with the same
priority share an interrupt vector. If not using interrupt priority, all interrupts
use vector 08h. If using interrupt priority, the high-priority interrupts use vec-
tor 08h and the low-priority interrupt uses vector 18h. If more than one inter-
rupt is enabled and uses the same vector, the code in the ISR must read the
interrupt flags to determine the source of the interrupt and take appropriate
action.

 To use the receive interrupt, set the appropriate values in the INTCON and
PIE1 registers:
' Enable unmasked peripheral interrupts


INTCON = %11000000

' Enable the serial receive interrupt

PIE1 = %00100000
The ON INTERRUPT statement specifies what routine to run when an inter-
rupt occurs:
ON INTERRUPT GOTO interrupt_service_routine
The ISR begins with a label and ends with a RESUME statement. A DISABLE
statement before the label holds all interrupt processing except for the current
interrupt until the next ENABLE statement.
DISABLE
interrupt_service_routine:

if (PIR1bits.RCIF == 1)
' A serial receive interrupt has occurred.
' Place code to read and respond to received data here.
else
' Check other flags for enabled interrupts and take action as needed.
endif
RESUME
ENABLE
Free download pdf