Page 78 - Making PIC Microcontroller Instruments and Controllers
P. 78
5A CO 'NOLLII{O THE OUTPU' AI{O BEADING THE IIIPUT
purposes, but keep in mind the intemal pull-up capability and the inline load limiting
resistors on the lower 4 bits/lines (80 to B3). These can, of cou$e, easily be made to
remain outside our circuitry, so none of this is a problem for us.
In other words, the keyboard is connected to PORTB such that the columns ol the
keyboard matrix are connected to the high nibble of a po and the rows ate connected
to the low nibble. The whing schemalic is shown in Figure 5.7.
To rcad a keyboard like this, the low nibble of PORTB is set to be outputs and the
high nibble is set to be inputs.
on the PIC 16F8??A, PORTB has a special property that allows its lines to be pulled
high (very weakly) with internal resislors by setting OPTION-REG.7 = 0. This prop-
erty of the PORT can alTect all the high bits (B5 to B7) but only those bits that are actu-
ally programmed to be inputs with TRISB will be affected.
Next, the four (the low bits, 83 to B0) are made low one line at a time, while the high
bits are polled to see if any of them has been pulled low If any of the switches is down,
one of the lines will be pulled low. Because we know which low bit was selected when
tlrc high bit became low, we can determine which key has be€n pressed. For our pur-
poses, al this stage we are interested only in swl, the upper left switch, so we can sim-
js
plify the circuify to whal is shown in Figure 5.8 for one roq and then v/hat shown
in Figure 5.9 forjust one key.
In these diagrams, we can see that if we make PORTB.olow and PORTB.4 has been
pulled high, PORIB.4 will become low only if SWl is held down. No polling is nec
essary at this stage. Once the conditions are set up, all we have to do is cfeate a loop
that tums the PORTD.o LED ON if the switch SWI is down and OFF for all other con-
ditions. The code for this is listed in Program 5.13.
ffiwf{6kt!{ Beading a swltch (Program reads swl and turns LED on PoRTD.0
ON while it is down)
CI.E]AR
DEFINE OgC 4
tRIsB = %11110000
PoRfB - %11111110 Set only B0 nade ]ow.
lhe pu11 ups on PORTB
OPEION-REC. 7 = 0 bi! 7 of lhe oPTIoN REG sets the puLl ups
IRI€D = %11111110 set only PORTD.o to an output.
PORID.0=0 initialize lhis LED to oFF
IIIAINI,oOP t
IF POR!!B.a=1 TEEN check for 6ast colrlm being 1ow
if i! is 1ow turn D0 oFF
ELSE
PORTD.0=!, if no! turn it oN
ENDIF
GOIO I'AINLOOP
AND