Yesterday I recieved a couple of shift registers, the 74HC595N type. I've looked around for how to use them with the Picaxe and it seems that X2 parts have the command "shiftout" which does it all for you. Some alternative code is given here in Example 2.
I have not yet tested the code. Could someone please explain how the masking process works, and that ampersand in this line:
It looks pretty compact. This is what I am using right now:
I made a mistake in that a 0 makes the serial pin go high, instead of the other way around. I am looking around for something that takes all the bit0-7 checks and puts them into a single one. I think I read something about @ptr or somesuch, but I cannot remember more nor find anything about it.
This would be very compact, but ofcourse it does not work that way:
Is there a way to use a "for" for bit0 to bit7? When used with byte variables it increments the variable each time to check how many times it has looped. I would need one that does not modify the bits.
I have not yet tested the code. Could someone please explain how the masking process works, and that ampersand in this line:
Code:
‘***** Sample symbol definitions *****
symbol sclk = 5 ; clock (output pin)
symbol sdata = 7 ; data (output pin for shiftout)
symbol serdata = input7 ; data (input pin for shiftin, note input7)
symbol counter = b7 ; variable used during loop
symbol mask = w4 ; bit masking variable
symbol var_in = w5 ; data variable used durig shiftin
symbol var_out = w6 ; data variable used during shiftout
symbol bits = 8 ; number of bits
symbol MSBvalue = 128 ; MSBvalue =128 for 8 bits, 512 for 10 bits, 2048 for 12 bits)
‘ ***** Shiftout LSB first *****
shiftout_LSBFirst:
for counter = 1 to bits ‘ number of bits
[COLOR="#FF0000"][SIZE=4][B] mask = var_out & 1 ‘ mask LSB[/B][/SIZE][/COLOR]
low sdata ‘ data low
if mask = 0 then skipLSB
high sdata ‘ data high
skipLSB: pulsout sclk,1 ‘ pulse clock for 10us
var_out = var_out / 2 ‘ shift variable right for LSB
next counter
return
Code:
symbol serpin = C.0
symbol clockpin = C.1
symbol latchpin = C.2
let dirsC =%111111
setfreq m32
main:
let pinsC =%000000
if bit0 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit1 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit2 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit3 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit4 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit5 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit6 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
if bit7 = 0 then high serpin
else low serpin
end if
pulsout clockpin,1
pulsout latchpin,1
goto main
This would be very compact, but ofcourse it does not work that way:
Code:
for bit0 to bit7
if bitX = 0 then high serpin
else low serpin
end if
next bitX
pulsout clockpin,1