16 digital outputs from an 08 picaxe using two HC595 chips

moxhamj

New Member
This circuit uses two serial to parallel chips (74HC595) to expand the number of outputs. It takes 3 outputs and converts to 16 outputs, and is also expandable to any arbitrary number and will work on any picaxe. This circuit is useful for driving two seven-segment displays or any task where lots of outputs are needed. The QH output (pin 9) can be cascaded to pin 14 of the next chip. All the bits are fed out one at a time and then the data is latched to the output pins.

' dual HC595 routine - 16 digital outputs
' uses any picaxe - pins 0,1,2
' pin0= latch clock HC595 leg 12 - low to high transition latches the data
' pin1 = serial data out = HC595 leg 14
' pin2= shift clock = HC595 leg 11 - low to high tranition stores the serial data out
' HC595 goes from left one to right one. Left one is MSB = the left 8 bits of the w0 word
' HC595 leg 11s in parallel
' HC595 leg 12s in parallel
' HC595 leg 9 of left one is the SQH bit output and goes into leg 14 of the right HC595
' HC595 reset legs 10 are both high
' HC595 output enable legs 13 are both low


main:w0=%1000000010000001
gosub senddatatolatch
pause 1000
w0=%0000000000000000
gosub senddatatolatch
pause 1000
goto main



senddatatolatch:' sends data in w0, uses register b2 and b3
' note QA is MSB and QH is LSB (binary number reads down) feed in LSB first
low 2' low leg 11
low 0' low leg 12
low 1' low leg 14
for b2=1 to 16
'*****b3=w0 and %00000001' check rightmost bit status
'*****if b3=0 then See post #5 - 2 lines replaced with one line
if bit0=0 then
low 1' serial data
else
high 1' serial data
endif
high 2' low to high transition stores the serial data
low 2
w0=w0/2' shift bits to right one place
next
high 0' latch the data and send it out - HC595 leg 12
low 0
return
 

Attachments

Last edited:

kranenborg

Senior Member
Hello
A very flexible option indeed, as it happens often that the 08M itself is capable of the task, just that you need more I/O lines.

As an alternative option to expand I/O in number and possibilities, you could also use one or more MCP23S17 16-bit I/O Expanders to get up to 128 lines of programmable input/output, optionally with interrupts, all with a single 08M. Note that this is the SPI version which takes the least code to write on a 08M, but you could use the MCP23017 as well, which is the i2c variant. Although 3-wire SPI is implemented, you can probably make 2-wire SPI by connecting the SPI input to the data line directly, while the SPI output is connected to the dataline via a 1K resistor.

An interesting aspect of the MCP23S17 is that - although these chips support SPI and thus require Chip Select hardware (which makes the I2C bus much easier to use from a hardware viewpoint) - they support address configuration using external pins and address selection in the same way as i2c does. This means that there can be several of these chips on the same SPI bus using the same CS line. See the datasheet for details.

For smaller number of interruptable I/O you could also use the MCP23S08/MCP23008 8-bit I/O expanders.

/Jurjen
 
Last edited:

ArnieW

Senior Member
This circuit uses two serial to parallel chips (74HC595) to expand the number of outputs.
Thanks Dr Acula - I've just put this circuit into VSM and am enjoying a flashing light show ;)

I am finding it very educational - there is nothing like seeing what is happening in a circuit compared to reading about it.

Anyway, in case you haven't picked it up yet, on the circuit you have drawn pin 8 of the picaxe connected to 5V instead of 0V.

If anyone wants the VSM version of this posted, let me know.

cheers and thanks, Arnie
 

lbenson

Senior Member
And--a nit--could you not replace

b3=w0 and %00000001' check rightmost bit status
if b3=0 then

with

if bit0 = 0 then
 

hippy

Technical Support
Staff member
An optimised version of Dr Acula's code for the 08 ( saves 25% code, 31 bytes down to 22 ), untested ...

Code:
SendDataToLatch:
  Low 2
  Low 0
  Low 1
  For b2 = 0 To 15
    pin1 = bit0
    PulsOut 2,1
    w0 = w0 / 2
  Next
  PulsOut 0,1
  Return
 

moxhamj

New Member
Optimised code for an 18X using output pins 5 6 and 7.
Using Hippy's code above, except that pin5 is not a valid 18X command (?)
The pulsout saves some bytes. Also in this code there is no need to set pin5 low at the beginning (pin1 for the 08M version above) as the for/next loop will set it.
The code below has been tested on an 18X/dual HC595 and driving an LCD display from the 595s.

HC595Output:'pass w0 - outputs the data to the HC595 chips
' the first bit of w0 goes to leg 15 of the HC595 on the right (IC1), second bit to leg 1 'etc, 9th bit to the left HC595 (IC5)
low 6' low leg 11 HC595
low 7' low leg 12 HC595
for b2=1 to 16
if bit0=0 then' check rightmost bit status
low 5' low HC595 leg 14
else
high 5' high HC595 leg 14
endif
pulsout 6,1
w0=w0/2' shift bits to right one place
next
pulsout 7,1
return
 

hippy

Technical Support
Staff member
Dr Acula : Try 'outpin5 = bit0'

Also "For 1 To 16" versus "For 0 To 15" - Both do the same but the later saves half a byte. I should have highlighted that change better.
 

moxhamj

New Member
Hippy does it again :) Not only is this code less than half the length, it also runs faster too!

HC595Output:'pass w0 - outputs the data to the HC595 chips
' the first bit of w0 goes to leg 15 of the HC595 on the right (IC1), second bit to leg 1 etc, 9th bit to the left HC595 (IC5)
low 6' low leg 11 on HC595
low 7' low leg 12 on 595
for b2=0 to 15
outpin5=bit0' pin 5 on picaxe = HC595 leg 14
pulsout 6,1
w0=w0/2' shift bits to right one place
next
pulsout 7,1
return
 

moxhamj

New Member
Driving a single HC595

There have been some requests for driving just one HC595 so the code below is a slight modification. Pass the values in b0. You can set individual outputs on the HC595 high with code like bit3=1.
The code cycles through 8 times instead of 16.

SingleHC595Output:'pass b0 - outputs the data to the HC595 chip
' the first bit of b0 goes to leg 15 of the HC595 second bit to leg 1 etc
low 6' low leg 11 on HC595
low 7' low leg 12 on 595
for b2=0 to 7
outpin5=bit0' pin 5 on picaxe = HC595 leg 14
pulsout 6,1
b0=b0/2' shift bits to right one place
next
pulsout 7,1
return
 
Top