Confirming Pages
298 CHAPTER 7 Microcontroller Programming and Interfacing
to pin RB7 remains on during this loop. The loop is called an infinite loop, because
it cycles as long as no interrupt occurs. Note that an active statement (such as High
led ) must exist between the label and the Goto of the loop for the interrupt to func-
tion, because PicBasic Pro checks for interrupts only after a statement is completed.
The final section of the program contains the interrupt service routine. Disable
must precede the label, and Enable must follow the Resume to prevent checking for
interrupts until control is returned to the main program. The interrupt service routine
executes when control of the program is directed to the beginning of this routine,
labeled by myint, when an interrupt occurs on pin RB0. At the identifier label myint,
the statement Low led clears pin RB7, turning off the LED. The Pause statement
causes a 500 millisecond delay, during which time the LED remains off. The next
line sets the INTCON.1 bit to 0 to clear the interrupt flag. The interrupt flag was set
internally to 1 when the interrupt signal was received on pin RB0, and this bit must
be reset to 0 before exiting the interrupt routine, so that subsequent interrupts can be
serviced. At the end of the myint routine, control returns to the main program loop
where the interrupt occurred. Lab Exercise 9 explores the concepts and example
presented in this section.
7.7 INTERFACING COMMON
PIC PERIPHERALS
This section introduces interfacing a PIC to two common peripheral devices. The
first is a 12-button keypad that can be used to input numeric data. The second is a
liquid crystal display that can be used to output messages and numeric information
to the user. More information on these and other useful peripheral devices can be
found online (see Internet Link 7.7).
7.7.1 Numeric Keypad
Figure 7.9 illustrates a common three-row, four-column 12-button keypad. Figure
7.10 shows a photographs of two common keypads. One has 12 keys with a ribbon
cable interface, and the other has 16 keys with solder holes to which individual wires
can be attached. Each key in a keypad is attached to a normally open pushbutton
switch. When a key is pressed, the switch closes. Figure 7.11 illustrates the electrical
schematic of the keypad with a recommended interface to the PIC16F84. A standard
keypad has a seven-pin header for connection to a ribbon cable socket. There is one
pin for each row and one pin for each column as numbered in Figure 7.11 .
The four rows (row 1, row 2, row 3, row 4) are connected to pins RB7 through
RB4, which are configured as inputs. Internal pull-up resistance is available as a
software option on these pins, so external pull-up resistors are not required (see
details in Section 7.8.1 ). The three columns (col 1, col 2, col 3) are connected to
pins RB0 through RB2, which are configured as outputs. The following PicBasic
Pro code contains initializations and a framework for a polling loop that can be used
to process input from the keypad. The column outputs are cleared low one at a time,
and each row input is polled to determine if the key switch in that column is closed.
Lab Exercise
Lab 9
Program-
ming a PIC
microcontroller—
part I
Internet Lin
7.7PIC I/O
interface devices
and useful
accessories
alc80237_ch07_258-345.indd 298alc80237_ch07_258-345.indd 298 12/01/11 12:52 PM12/01/11 12:52 PM