Shift In bits with 08M

giuseppe

Member
Hello everyone

I am trying to retrieve data bits from a shift register. Actually, it is a shift register inside the Super Nintendo Controller. Here is the info regarding its function http://english.cxem.net/pinout/pinout21.php .

I am using the PICAXE 08M. When I read the manual, it stated that I need to use a different method since the 08M does not have a shiftin command. I am actually trying to understand how exactly the alternate method goes about doing this. The past semester (ended about a week and a half ago) I took a class titled "Intro to Digital Design" and learned about D flip flops and how they are used to make shift registers so the concept is not foreign to me.

Here is the manual's code for reference.

Code:
' ========================================================================
' ***** Shiftin MSB first, Data Post-Clock *****
' Shift in the data MSB first into variable var_in 
' Read data after sending clock pulse 
' Using clock output pin sclk
' Using data input pin serdata 

shiftin_MSB_Post:
	let var_in = 0
	for counter = 1 to bits			' number of bits
		var_in = var_in * 2 		' shift left as MSB first
		pulsout sclk,1 			' pulse clock to get next data bit
		if serdata = 0 then skipMSBPost
		var_in = var_in + 1 		' set LSB if serdata = 1
skipMSBPost:
	next counter
	return
' ========================================================================
I have been looking at this code and am still a little unsure on what it is doing. Here is my understanding:

the "for counter = 1 to bits" line is how many bits I would like to shiftin from the shift register
var_in = var_in * 2 --- it says it shifts left as MSB first, how does it exactly do that. when it starts initially the value will be zero before the first clock.

the pulsout sclk, 1 --- just pulses the clk pin to shift to the next bit to be read

if serdata = 0 then skipMSBPost --- why is it skipping it?
var_in = var_in + 1 ---- not sure

so say I shift in the binary number "1010" using the code above (MSB first and sample data after clock pulse), after cycling four bits, the value of var_in will be 10? if the number is "1111" the value of var_in will be 15? I think so because the decimal value matches the binary after going through the code but I just would like to know HOW and WHY it does this.
 

Chavaquiah

Senior Member
var_in = var_in * 2 --- it says it shifts left as MSB first, how does it exactly do that. when it starts initially the value will be zero before the first clock.
Yes, but later on you have var_in = var_in + 1.

Multiplying by 2 shifts bits to the left, making room to read the current bit.

if serdata = 0 then skipMSBPost --- why is it skipping it?
var_in = var_in + 1 ---- not sure
If the incoming bit is zero, there's nothing left to do. Multiplication by 2 already left a zero at position 0. On the other hand, if the incoming bit is 1 then it executes var_in = var_in + 1.

Here's the sequence to read %1010 (decimal 10):

%00000000 // var_in = 0

%00000000 // *2
%00000001 // First incoming bit is 1 so var_in = var_in + 1

%00000010 // *2
%00000010 // Second bit is zero, so skips to next cycle

%00000100 // *2
%00000101 // 3rd bit is 1

%00001010 // *2
%00001010 // 4th and last bit is zero
 

westaust55

Moderator
Try running your program in the Programming Editor simulator.
Click "Simulate" then "Step" on the PE toolbar.

Using the step mode to slow the action down and you can see what is happening. then just turn the input pin on and off to represent the incoming serial data stream.

That may help you understand the porogram flow/actions and the whys/wherefores.
 
Last edited:

giuseppe

Member
Ohhh okay. I see it now. Multiplying it by two is to actually go on to the next place. Since we are in a base 2 numbering system, doubling it takes us to the next place. If there is a "1" upcoming, we need to stick that in the next place over because the previous doubling "created" a spot for the next bit to reside in. The zero is there because it is an even binary number. If there is a "0", it is already there and we do not have to tell the PICAXE that it is there.

Thanks a bunch guys, the explanation and and simulation idea helped. The SNES controller is returning the information flawlessly.
 
Top