mynet43
01-08-2009, 09:35 AM
I'm trying to help someone on the forum drive this chip from a prop.
The problem is that they have the chip and I don't have one to test with.
They're a relative newby to the prop so I wrote the following driver.
They're using it to drive a set of 8 LED's.
Apparently it almost works. It latches the values for about a second and then
drops the lights and bit 0 flickers, as best as I can understand the description from long distance.
Here's the driver code I wrote. Would someone please take a look at it and let me know if you
spot something? If you have a chip to debug it (or a working driver), that would be even better:)
Thanks for your help.
Jim
'' ************************************************** ****************************
'' * "HC595.spin" 74HC595 SPIN Object *
'' * coded by Jim -- 12/31/08 -- Happy New Year! *
'' * Version 1.0 *
'' ************************************************** ****************************
''
'' Calling sequence
'' OBJ
'' SReg : "HC595"
''
'' code segment
'' SReg.InitSer595 ' to initialize driver
'' more code
'' DataByte := toSend ' set up data to send to chip
'' SReg.WriteSer595(DataByte) ' send data to chip
'' more code
''
CON
_PinHigh = 1
_PinLow = 0
CLK = 0 ' pin number of CLK pin on chip (AKA SCK)
SER = 1 ' pin number of SER pin on chip
CLR = 2 ' pin number of SCLR pin on chip
RCK = 3 ' pin number of RCK pin on chip
GPN = 4 ' pin number of G pin on chip
VAR
long bitout, databyte
PUB InitSer595 ' initialize 74HC595 for startup
' set the control lines as outputs
dira[CLK] ~~
dira[SER] ~~
dira[CLR] ~~
dira[RCK] ~~
dira[GPN] ~~
outa[CLK] := _PinLow ' init main clock line to low
outa[RCK] := _PinLow ' init latch clock line to low
outa[GPN] := _PinHigh ' set outputs to 3-state
outa[CLR] := _PinLow ' set to clear register on chip
outa[GPN] := _PinLow ' turn on the chip and clear pins
outa[CLR] := _PinHigh ' return CLR pin to high after clear
PUB WriteSer595(Data) ' Data is long with 8 bits of data in bits 7 - 0
' Write 8 bits of data. Data byte is output MSB first.
databyte := Data ' transfer to working storage
repeat 8 ' shift out 8 bits
bitout := databyte & %1000_0000 ' take hi bit from byte -> bitout bit 7
bitout >>= 7 ' shift to bit zero for output
databyte <<= 1 ' shift data left 1 bit for next loop
outa[SER] := bitout ' shift out high bit to serial pin (0 or 1)
outa[CLK] := _PinHigh ' toggle the clock pin to shift bit out
outa[CLK] := _PinLow ' high then low to cycle the clock
outa[RCK] := _PinHigh ' toggle the RCK pin to latch the output
outa[RCK] := _PinLow ' high then low to cycle the clock
The problem is that they have the chip and I don't have one to test with.
They're a relative newby to the prop so I wrote the following driver.
They're using it to drive a set of 8 LED's.
Apparently it almost works. It latches the values for about a second and then
drops the lights and bit 0 flickers, as best as I can understand the description from long distance.
Here's the driver code I wrote. Would someone please take a look at it and let me know if you
spot something? If you have a chip to debug it (or a working driver), that would be even better:)
Thanks for your help.
Jim
'' ************************************************** ****************************
'' * "HC595.spin" 74HC595 SPIN Object *
'' * coded by Jim -- 12/31/08 -- Happy New Year! *
'' * Version 1.0 *
'' ************************************************** ****************************
''
'' Calling sequence
'' OBJ
'' SReg : "HC595"
''
'' code segment
'' SReg.InitSer595 ' to initialize driver
'' more code
'' DataByte := toSend ' set up data to send to chip
'' SReg.WriteSer595(DataByte) ' send data to chip
'' more code
''
CON
_PinHigh = 1
_PinLow = 0
CLK = 0 ' pin number of CLK pin on chip (AKA SCK)
SER = 1 ' pin number of SER pin on chip
CLR = 2 ' pin number of SCLR pin on chip
RCK = 3 ' pin number of RCK pin on chip
GPN = 4 ' pin number of G pin on chip
VAR
long bitout, databyte
PUB InitSer595 ' initialize 74HC595 for startup
' set the control lines as outputs
dira[CLK] ~~
dira[SER] ~~
dira[CLR] ~~
dira[RCK] ~~
dira[GPN] ~~
outa[CLK] := _PinLow ' init main clock line to low
outa[RCK] := _PinLow ' init latch clock line to low
outa[GPN] := _PinHigh ' set outputs to 3-state
outa[CLR] := _PinLow ' set to clear register on chip
outa[GPN] := _PinLow ' turn on the chip and clear pins
outa[CLR] := _PinHigh ' return CLR pin to high after clear
PUB WriteSer595(Data) ' Data is long with 8 bits of data in bits 7 - 0
' Write 8 bits of data. Data byte is output MSB first.
databyte := Data ' transfer to working storage
repeat 8 ' shift out 8 bits
bitout := databyte & %1000_0000 ' take hi bit from byte -> bitout bit 7
bitout >>= 7 ' shift to bit zero for output
databyte <<= 1 ' shift data left 1 bit for next loop
outa[SER] := bitout ' shift out high bit to serial pin (0 or 1)
outa[CLK] := _PinHigh ' toggle the clock pin to shift bit out
outa[CLK] := _PinLow ' high then low to cycle the clock
outa[RCK] := _PinHigh ' toggle the RCK pin to latch the output
outa[RCK] := _PinLow ' high then low to cycle the clock