This is only a preview of the June 2003 issue of Silicon Chip. You can view 29 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Sunset Switch For Security & Garden Lighting":
Items relevant to "Test Your Reflexes With A Digital Reaction Timer":
Items relevant to "Adjustable DC-DC Converter For Cars":
Purchase a printed copy of this issue for $10.00. |
PICAXE APPLICATION SPECIAL
PICAXE-08
PICAXE
Port Expansion
Everyone is raving about the PICAXE-08. It’s
tiny, cheap and is so easy to program that even
beginners can do it. There is one major drawback,
though: it only has five pins available for input
and output ports. Not any more, it ain’t!
S
ure we could use the PICAXE-18 or PICAXE-28
when we need more inputs and outputs (both of
which have significantly more i/o pins) but that
would defeat the purpose of using a small, low-cost chip.
Fortunately there is a simple solution. By using 74xx165
by
David Lincoln
and the 74xx595 shift registers, we can expand the number
of input and output ports in multiples of eight.
Fig.1 shows how. In Fig.1a a 74xx165 is being used to
expand the number of inputs to eight. This requires three
of the PICAXE ports; one for serial data, one for clocking
The author’s Port Expansion unit,
lashed up on a Protoboard. This has
both inputs (from a telephone keypad)
and outputs (monitored by the LED
display. The chips top left of the photo
are a true RS232 interface.
22 Silicon Chip
www.siliconchip.com.au
Figs. 1a (left) and
1b (right) show the
input and output
port expansion
(respectively) for
a PICAXE-08.
Only one 8-port
expansion chip is
shown but these
can be further
cascaded as
required.
the shift register, and one for latching the data into the
shift register.
The 74xx165 also has a serial output port making it
possible to daisy chain multiple 165’s to achieve even
more inputs.
To read 8 input lines from a single 74xx165 the PICAXE-08 code is: Symbol
Symbol
Symbol
latchin = 4
datain = pin3
clk = 2
Main:
High latchin
Loop:
Gosub bytein
Goto loop
‘Read a byte into b1
‘Loop forever
Bytein:
‘Reads a byte of data into b1
Pulsout latchin, 1
‘Latch the input register
Let b1 = 0
‘Initialise data to zero
For b0 = 0 to 7
‘Count to 8
Let b1 = b1 * 2
‘Shift left
If datain = 0 then nobit ‘Test for a data bit
Let b1 = b1 + 1
Nobit:
Pulsout clk, 1
‘Clock the shift register
Next b0
return
In Fig.1b a 74xx595 is being used to expand the number of outputs to eight. Once again three
PICAXE ports are needed; one for serial
data, one for clocking the shift register,
and one for latching the data. Like the
74xx165, the 74xx595 also has a serial
output port, again making it possible to
daisy chain multiple 595’s to achieve even
more outputs.
To output 8 bits from a single 74xx595 the PICAXE-08
code is: Symbol
Symbol
Symbol
dataout = 0
clk
=2
latchout = 1
Main:
For b3 = 0 to 255
Let b2 = b3
Gosub byteout
Next b3
Goto main
Byteout:
For b0 = 0 to 7
Let b1 = b2 & 1
If b1 = 1 then outhi
Low dataout
Goto clockout
Outhi:
High dataout
Clockout:
Pulsout clk, 1
Let b2 = b2 / 2
Next b0
Pulsout latchout, 1
return
‘Output the numbers 0 thru 255
‘Loop forever
‘Output the byte in b2 — b2 is
destroyed in the process
‘Count to 8
‘Mask off low order bit
Test for output bit
‘Set output bit to zero
‘Set output bit to one
‘Clock the shift register
‘Shift right
‘Latch the output register
Expanding both the input and output ports at the same
Fig.2 combines both of the above circuits
into one, giving both input and output
port expansion for the ’08.
www.siliconchip.com.au
June 2003 23
time can be achieved by combining the circuits of Figs.1a
and 1b into the circuit of Fig.2. Only five PICAXE ports
are required because the clock line can be shared by the
input and output shift registers. That’s just as well, because the PICAXE-08 has more than four and less than
six ports . . .
Now that we have eight inputs and eight outputs to play
with we can run some experiments. By connecting push
button switches to the expanded input ports and LEDs
to the expanded output ports, we can show that our port
expansion unit is working. Fig.3 shows what’s needed.
Goto clockout
Outhi:
High dataout
Clockout:
Pulsout clk, 1
Let b2 = b2 / 2
Next b0
Pulsout latchout, 1
return
‘Set output bit to one
‘Clock the shift register
‘Shift right
‘Latch the output register
Almost anything that recognises or runs from a TTL-compatible signal can be connected to the expanded input and
output terminals. Fig.4 shows how to connect a 7-segment
LED display and Fig.5 shows how to connect a telephone
pushbutton keypad.
Here is the PICAXE code to display the numbers 0
through 9 on the 7 segment display: Symbol
Symbol
Symbol
Fig.3: this little test setup can be built to show that all
is working properly.
To copy 8 bits from input to output, the PICAXE-08
code is: Symbol
Symbol
Symbol
Symbol
Symbol
latchin = 4
datain = pin3
clk = 2
dataout = 0
latchout = 1
Main:
‘Read a byte into b1 and output it
from b2
High latchin
Loop:
Gosub bytein
‘Read a byte
Let b2 = b1
‘Copy input to output
Gosub byteout
‘Write a byte
Goto loop
‘Loop forever
Bytein:
‘Reads a byte of data into b1
Pulsout latchin, 1 ‘Latch the input register
Let b1 = 0
‘Initialise data to zero
For b0 = 0 to 7
‘Count to 8
Let b1 = b1 * 2
‘Shift left
If datain = 0 then nobit
‘Test for a data bit
Let b1 = b1 + 1
Nobit:
Pulsout clk, 1
‘Clock the shift register
Next b0
return
Byteout:
‘Output the byte in b2 — b2 is
destroyed in the process
For b0 = 0 to 7
‘Count to 8
Let b1 = b2 & 1
‘Mask off low order bit
If b1 = 1 then outhi ‘Test for output bit
Low dataout
‘Set output bit to zero
24 Silicon Chip
dataout = 0
clk
=2
latchout = 1
Main:
‘Output the numbers 0 thru 9 on
a 7 segment display
For b3 = 0 to 9
Lookup b3, ($BE, $82, $DC, $D6, $E2, $76, $7E, $92,
$FE, $F2), b2
Gosub byteout
Pause 500
Next b3
Goto main
‘Loop forever
Byteout:
For b0 = 0 to 7
Let b1 = b2 & 1
If b1 = 1 then outhi
Low dataout
Goto clockout
Outhi:
High dataout
Clockout:
Pulsout clk, 1
Let b2 = b2 / 2
Next b0
Pulsout latchout, 1
return
‘Output the byte in b2 — b2 is
destroyed in the process
‘Count to 8
‘Mask off low order bit
‘Test low order bit
‘Set output bit to zero
‘Set output bit to one
‘Clock the shift register
‘Shift right
‘Latch the output register
The following code will read the buttons pressed on a
telephone keypad and display the result on the LED display. To do this we need 10 output lines, more than can be
achieved with a single 74xx595, so a second ’595 is daisy
chained to the first to give 8 extra outputs (see Fig.5). A
similar technique could be used to daisy chain ’195 shift
registers to give more inputs.
‘Keypad input - 7 seg. display output
‘
‘
b0
bit counter
‘
b1
temp work data
‘
b2
display data
‘
w4
output data
www.siliconchip.com.au
Fig.4: here’s
how a 7segment LED
display is
connected to
the ’595. Any
of the “garden
variety” common cathode
LED displays
could be used.
‘
‘Digit
0 1 2 3 4 5 6 7 8 9 * #
‘Segment code $BE, $82, $DC, $D6, $E2, $76, $7E, $92,
$FE, $F2, $68, $EB
‘
‘b2 bit
76543210
‘Segment
b g f a e d c dp
‘
symbol
clk = 2
symbol
latchout = 1
symbol
dataout = 0
symbol
datain = pin3
symbol
latchin = 4
main:
high latchin
loop:
let w4 = b2 + $100
gosub inout
lookup b1, (b2, $82, $E2, 0, $92, 0, 0, 0, $68), b2
if b1 <> 0 then loop
let w4 = b2 + $200
gosub inout
lookup b1, (b2, $DC, $76, 0, $FE, 0, 0, 0, $BE), b2
if b1 <> 0 then loop
let w4 = b2 + $400
gosub inout
lookup b1, (b2, $D6, $7E, 0, $F2, 0, 0, 0, $EB), b2
goto loop
inout:
wordout:
‘Output the word in w4 - destroys w4
for b0 = 0 to 15
let b1 = w4 & 1
if b1 = 1 then outhi
low dataout
goto clockout
outhi:
high dataout
clockout:
pulsout clk, 1
let w4 = w4 / 2
next b0
pulsout latchout, 1
bytein:
‘Read a byte into b1
pulsout latchin, 1
let b1 = 0
for b0 = 0 to 7
let b1 = b1 * 2
if pin3 = 0 then nobit
let b1 = b1 + 1
nobit:
pulsout clk, 1
next b0
return
SC
Fig.5: input
from a
telephone
keypad and
output to a
7-segment LED
display.
www.siliconchip.com.au
June 2003 25
|