Program conversion: 08M2 to 20X2

giuseppe

Member
Hello all,

So I posted a program a while ago that showed the buttons statuses in a NES controller. I am now trying to switch this code over to a 20X2 (was working on an 08M2 before). There are two problems I am having. First is that when I try switching from the shiftin sub routine to the shiftin command, the program stop working. Second problem is that when I attempt to use other I/O for clk and data, it again stops working. Here is the last working code (already converted from 08M2):

Code:
#rem

================================================================================
  Updates
================================================================================

#endrem
'===============================================================================
'  Constants
'===============================================================================
symbol bits 	= 	8		'number of bits
symbol MSBvalue 	= 	128		'MSBvalue (=128 for 8 bits,512 for 10 bits, 
						 '2048 for 12 bits)
						
'===============================================================================
'  Variables
'===============================================================================
symbol NESdata	=	b0		'holds NES button data
symbol counter	= 	b1	   	'variable used during loop
symbol mask 	=	w1		'bit masking variable
symbol var_in 	= 	w2		'data variable used during shiftin

'===============================================================================
'  Pin Declarations
'===============================================================================
'Inputs:
symbol serdata	=	pinc.1		'serial data input

'Outputs:
symbol sclk		=	c.2		'clock output
symbol latch	=	c.4		'latch output

'===============================================================================
'  Initialization
'===============================================================================
#picaxe 20x2	'tells compiler this code is used with a PICAXE 08M2 chip.

low sclk	'clock must be low before we can use the shiftin subroutine
low latch	;latch must be low before we can use the pulsout command

'===============================================================================
'  Main
'===============================================================================
Main:
pulsout latch,1			'latch the data into the NES controller's 4021
'shiftin sclk,serdata,0,(var_in)
gosub Shiftin_LSB_Pre		'shift in the button data from the NES controller
let NESdata = var_in		'transfer shifted in bits to the variable NESdata

if bit0 = 0 then sertxd("A pressed     ",cr,lf) endif
if bit1 = 0 then sertxd("B pressed     ",cr,lf) endif
if bit2 = 0 then sertxd("Select pressed",cr,lf) endif
if bit3 = 0 then sertxd("Start pressed ",cr,lf) endif
if bit4 = 0 then sertxd("Up pressed    ",cr,lf) endif
if bit5 = 0 then sertxd("Down pressed  ",cr,lf) endif
if bit6 = 0 then sertxd("Left pressed  ",cr,lf) endif
if bit7 = 0 then sertxd("Right pressed ",cr,lf) endif

pause 50

goto Main			'go back and check the NES controller buttons again

'===============================================================================
'  Sub-Routines
'===============================================================================
' ***** Shiftin LSB first, Data Pre-Clock *****
' Shift in the data LSB first into variable var_in 
' Read data before sending clock pulse 
' Using clock output pin sclk
' Using data input pin serdata 
'#rem
Shiftin_LSB_Pre:
	let var_in = 0
	for counter = 1 to bits			' number of bits
		var_in = var_in / 2 		' shift right as LSB first	
		if serdata = 0 then skipLSBPre
		var_in = var_in + MSBValue	' set MSB bit if serdata = 1
SkipLSBPre:
		pulsout sclk,1 			' pulse clock to get next data bit
	next counter
	return
'	#endrem
Now when I comment out anything to do with the shiftin routine and try the shiftin command, it stops working:

Code:
#rem

================================================================================
  Updates
================================================================================

#endrem
'===============================================================================
'  Constants
'===============================================================================
symbol bits 	= 	8		'number of bits
symbol MSBvalue 	= 	128		'MSBvalue (=128 for 8 bits,512 for 10 bits, 
						 '2048 for 12 bits)
						
'===============================================================================
'  Variables
'===============================================================================
symbol NESdata	=	b0		'holds NES button data
symbol counter	= 	b1	   	'variable used during loop
symbol mask 	=	w1		'bit masking variable
symbol var_in 	= 	w2		'data variable used during shiftin

'===============================================================================
'  Pin Declarations
'===============================================================================
'Inputs:
symbol serdata	=	c.1		'serial data input

'Outputs:
symbol sclk		=	c.2		'clock output
symbol latch	=	c.4		'latch output

'===============================================================================
'  Initialization
'===============================================================================
#picaxe 20x2	'tells compiler this code is used with a PICAXE 08M2 chip.

low sclk	'clock must be low before we can use the shiftin subroutine
low latch	;latch must be low before we can use the pulsout command

'===============================================================================
'  Main
'===============================================================================
Main:
pulsout latch,1			'latch the data into the NES controller's 4021
shiftin sclk,serdata,1,(var_in)
'gosub Shiftin_LSB_Pre		'shift in the button data from the NES controller
let NESdata = var_in		'transfer shifted in bits to the variable NESdata

if bit0 = 0 then sertxd("A pressed     ",cr,lf) endif
if bit1 = 0 then sertxd("B pressed     ",cr,lf) endif
if bit2 = 0 then sertxd("Select pressed",cr,lf) endif
if bit3 = 0 then sertxd("Start pressed ",cr,lf) endif
if bit4 = 0 then sertxd("Up pressed    ",cr,lf) endif
if bit5 = 0 then sertxd("Down pressed  ",cr,lf) endif
if bit6 = 0 then sertxd("Left pressed  ",cr,lf) endif
if bit7 = 0 then sertxd("Right pressed ",cr,lf) endif

pause 50

goto Main			'go back and check the NES controller buttons again

'===============================================================================
'  Sub-Routines
'===============================================================================
' ***** Shiftin LSB first, Data Pre-Clock *****
' Shift in the data LSB first into variable var_in 
' Read data before sending clock pulse 
' Using clock output pin sclk
' Using data input pin serdata 
#rem
Shiftin_LSB_Pre:
	let var_in = 0
	for counter = 1 to bits			' number of bits
		var_in = var_in / 2 		' shift right as LSB first	
		if serdata = 0 then skipLSBPre
		var_in = var_in + MSBValue	' set MSB bit if serdata = 1
SkipLSBPre:
		pulsout sclk,1 			' pulse clock to get next data bit
	next counter
	return
	#endrem
Any ideas?
 

giuseppe

Member
Well what about when I switch to different I/O, say, for the clock (c.2 to c.7 from the code above)? Why would it stop working for that change?
 

Technical

Technical Support
Staff member
Double check your serdata symbol, it must be changed (pinx.x to x.x) when swapping from one to the other.
 

giuseppe

Member
Yeah I think I will go back to the routine but as for the changing of the pins, I am having no problems now. I was a little tired so maybe I got lost in the breadboard during wiring. I must have simply wired it wrong.
 
Top