Shift registers and M2 chips, mask

Jakob2803

Senior Member
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:
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
It looks pretty compact. This is what I am using right now:

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
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: :(
Code:
for bit0 to bit7
if bitX = 0 then high serpin
else low serpin
end if
next bitX
pulsout clockpin,1
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.
 

srnet

Senior Member
Could someone please explain how the masking process works, and that ampersand in this line:

mask = var_out & 1 ‘ mask LSB
I just did a search of the manwel, and the first occurrence of '&' says its a bitwise AND so;

mask = var_out & 1

is the same as

mask = var_out AND 1

Does that help ?
 

Jakob2803

Senior Member
I just did a search of the manwel, and the first occurrence of '&' says its a bitwise AND so;

mask = var_out & 1

is the same as

mask = var_out AND 1

Does that help ?
But how do you determine which bit to check with the AND? Very confusing. :)
 

Technical

Technical Support
Staff member
It is always ANDing the lower bit, bit0.
What you have missed is this line:

var_out = var_out / 2 ‘ shift variable right for LSB

Which moves all the bits right one place every loop. So it is bit 0 always tested, but bit0 actually keeps changing to the next bit in turn...
 

inglewoodpete

Senior Member
If it is the binary AND that is causing the confusion then the following sequence may help:

Assume that the initial value of var_out is $E2 or 226 decimal (%11100010)
The mask is a constant $01 or 1 decimal (%00000001)

Code:
%11100010  'Initial value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #0'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%01110001  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #1'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00111000  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #2'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00011100  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #3'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00001110  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #4'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000111  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #5'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000011  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #6'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000001  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #7'
So the 8-bits of the byte are shifted out in sequence: 0, 1, 0, 0, 0, 1, 1, 1 (Least significant bit first.)
 

Jakob2803

Senior Member
It is always ANDing the lower bit, bit0.
What you have missed is this line:

var_out = var_out / 2 ‘ shift variable right for LSB

Which moves all the bits right one place every loop. So it is bit 0 always tested, but bit0 actually keeps changing to the next bit in turn...
If it is the binary AND that is causing the confusion then the following sequence may help:

Assume that the initial value of var_out is $E2 or 226 decimal (%11100010)
The mask is a constant $01 or 1 decimal (%00000001)

Code:
%11100010  'Initial value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #0'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%01110001  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #1'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00111000  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #2'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00011100  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #3'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00001110  'New value of var_out
%00000001  'Mask
%00000000  'Resultant value to shift out: 0 'bit #4'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000111  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #5'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000011  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #6'
After dividing var_out by 2, the bit pattern is 'shifted' 1-bit to the right:
%00000001  'New value of var_out
%00000001  'Mask
%00000001  'Resultant value to shift out: 1 'bit #7'
So the 8-bits of the byte are shifted out in sequence: 0, 1, 0, 0, 0, 1, 1, 1 (Least significant bit first.)
I see. :) Yes it makes a bit more sense now.
 
Top