Write/Read problem

sid

Senior Member
I have one circuit sending data to another circuit via a serout/serin connection and both circuits work as expected.
The output circuit monitors a set of 14 toggle switchs and depending which switch is used sends the data to the other circuit eg: if switch1 is high then b0=1 etc then b0 is sent to the receive circut, which uses a 20x2.
I want this second circuit to take the incoming data and then depending on the value of b0 to go do something
eg: if b0=1 then goto section1, if b0=2 then goto section2 etc.
This all works fine, and every time the code does this I get it to write the value of the variable and then read it later as required. Aagin this all works well until I try and be clever and tell the program to write/read several things and then add them up.
I'm pretty sure the hardware is working ok and that I've got my b0 values correct, I'd just like someone to tell me if my code is functional or not.
Code:
Start:
serin c.2, n1200_8,("ABC"),b0
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4

vd1:
b4=0
write 1,b4
goto vd5

vd2:
b4=4
write 1,b4
goto vd5

vd3:
b5=0
write 2,b5
goto vd5

vd4:
b5=5
write 2,b5
goto vd5

vd5:
b6=b4+b5
write 3,b6
if b6=9 then goto section1
if b6=4 then goto section2
if b6=5 then goto powerup
if b6=0 then goto powerup

section1:
section2:
powerup:
I've omitted most of the program for clarity as this is the only part that is not working completely.
I've checked that I'm not doubling up on variables b0/w0 etc.
Is there a limit to the number of writes that can be achieved?, the manuel says 0-255 so I'm assuming that means 255 positions that can be written to
I've spent three nights on this now but I can't see why the code won't work, so I've finally resorted to asking the forum for help
 

ValueAdd

Senior Member
writing to external EEPROM chips does incur a delay while the write is executed. Although you are using internal EEPROM and no comment in the manual 2, suggest that you try adding a PAUSE 5 either after each write command
 
Last edited:

hippy

Ex-Staff (retired)
There's no PAUSE required after a WRITE to internal Data Eeprom.

Gazing into my crystal ball, if it works with each individual command sent but not with a longer sequence, I'd guess that data were being sent before the receiver is ready.
 

sid

Senior Member
What happens if b0 received is not 9,10,13 or 14...?
I omitted most of the program for clarity but since you ask I've posted it so you can see what happens to the data when b0 is a different value.
In short the incoming data points the program to a particular section of the code, where it ticks away quite happily, routinely checking for new data.
Each time the program points to a particular section of code I use a write command so when the circuit is switched off and then back on (sometimes days later), it "remembers which section of code that it should be using and the program works fine when it's a simple case of
Code:
 write 0, b2
, it's when I try and get it to remember more than one remembered value and then make a calculation from two or more values that it goes wrong, but I can't see for the life of me what I'm doing wrong code wise. And yes I've checked that the incoming data is what is expected.
Code:
;=================================================================================
;X MULTIPLE SIGNAL CONTROL UNIT 8 SIGNALS VIA 3 8BIT SHIFT REGISTERS USING A 20X2
;=================================================================================
symbol counter=w8


powerup:
read 0,b2					;READ MEMORY ON POWER UP
if b2=1  then goto signal1
if b2=2  then goto signal2
if b2=3  then goto signal3
if b2=4  then goto signal4
if b2=41 then goto signal4A
if b2=5  then goto signal5 

 
start:
serin c.2, n1200_8,("ABC"),b0
if b0=1 then goto signal1
if b0=2 then goto signal2
if b0=5 then goto signal4
if b0=7 then goto signal3
if b0=6 then goto signal4
if b0=8 then goto signal4A


SIGNAL1:					;SET SIGNAL TO GREEN
b2=1
write 0, b2					;WRITE SIGNAL TO MEMORY
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001100 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
           		
Monitor1:                        	;MONITOR SIGNAL1'S SENSOR
readadc 1,b3                		;READ ADC1
if b3 <120 then goto red1     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
gosub checkswitchs			;CHECK SERIN FOR NEW DATA
goto Monitor1                    	;KEEP LOOPING UNTIL INPUT CHANGES

red1:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red11:
counter=counter+1
if counter=20 then goto yellow1
readadc 1,b3				;MONITOR SENSOR FOR INPUT
gosub checkswitchs			;MONITOR SWITCHS FOR CHANGE
if b3 <120 then goto red1
goto red11

yellow1:
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001010 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
yellow11:
readadc 1,b3                		;READ ADC1
if b3 <120 then goto red1
counter=counter+1
if counter=15 then goto signal1
gosub checkswitchs
goto yellow11				

SIGNAL2:					;SET SIGNAL TO GREEN
b2=2
write 0, b2					;WRITE SIGNAL TO MEMORY
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01100001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10               		;LATCH REGISTERS

Monitor2:                        	;MONITOR SIGNAL2'S SENSOR
readadc 2,b3                		;READ ADC2
if b3 <120 then goto red2     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
gosub checkswitchs			;CHECK SERIN FOR NEW DATA
goto Monitor2                    	;KEEP LOOPING UNTIL INPUT CHANGES

red2:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red21:
counter=counter+1
if counter=20 then goto yellow2
readadc 2,b3				;MONITOR SENSOR FOR INPUT
gosub checkswitchs			;MONITOR SWITCHS FOR CHANGE
if b3 <120 then goto red2
goto red21

yellow2:
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01010001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
yellow21:
readadc 2,b3                		;READ ADC2
if b3 <120 then goto red2
counter=counter+1
if counter=15 then goto signal2
gosub checkswitchs
goto yellow21				


SIGNAL3:					;SET SIGNAL TO GREEN
b2=3
write 0, b2					;WRITE SIGNAL TO MEMORY
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010011 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %00001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
           		
Monitor3:                        	;MONITOR SIGNAL1'S SENSOR
readadc 4,b3                		;READ ADC4
if b3 <120 then goto red3     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
gosub checkswitchs			;CHECK SERIN FOR NEW DATA
goto Monitor3                    	;KEEP LOOPING UNTIL INPUT CHANGES

red3:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red31:
counter=counter+1
if counter=20 then goto yellow3
readadc 4,b3				;MONITOR SENSOR FOR INPUT
gosub checkswitchs			;MONITOR SWITCHS FOR CHANGE
if b3 <120 then goto red3
goto red31

yellow3:
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
yellow31:
readadc 4,b3                		;READ ADC4
if b3 <120 then goto red3
counter=counter+1
if counter=15 then goto signal3
gosub checkswitchs
goto yellow31				

SIGNAL4:
b2=4
write 0, b2					;WRITE SIGNAL TO MEMORY
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10011000 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
           		
Monitor4:                        	;MONITOR SIGNAL1'S SENSOR
readadc 5,b3                		;READ ADC5
if b3 <120 then goto red4     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
gosub checkswitchs			;CHECK SERIN FOR NEW DATA
goto Monitor4                    	;KEEP LOOPING UNTIL INPUT CHANGES

red4:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red41:
counter=counter+1
if counter=20 then goto signal4
readadc 5,b3				;MONITOR SENSOR FOR INPUT
gosub checkswitchs			;MONITOR SWITCHS FOR CHANGE
if b3 <120 then goto red4
goto red41

SIGNAL4A:
b2=41
write 0, b2					;WRITE SIGNAL TO MEMORY
goto powerup
	
registers:
for b0= 1 to 8
if b1 > 127 then high c.7 else low c.7
endif
pulsout c.5,10
b1 = b1 *2
next b0
return 

checkswitchs:			;CHECK SERIN FOR NEW DATA,AFTER 2SEC RETURN TO MS1
serin [1000,rtn], c.2, n1200_8,("ABC"),b0
if b0=1 then goto signal1
if b0=2 then goto signal2
if b0=5 then goto signal4
if b0=6 then goto signal4
if b0=7 then goto signal3
if b0=8 then goto signal4A

if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4

rtn:
return

vd1:
b4=0
write 1,b4
pause 5
goto vd5

vd2:
b4=4
write 1,b4
pause 5
goto vd5

vd3:
b5=0
write 2,b5
pause 5
goto vd5

vd4:
b5=5
write 2,b5
pause 5
goto vd5

vd5:
b6=b4+b5
write 3,b6
pause 5
if b6=9 then goto signal5
if b6=4 then goto allred
if b6=5 then goto powerup
if b6=0 then goto powerup

allred:
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
goto start


SIGNAL5:
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %11000010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
           		
Monitor5:                        	;MONITOR SIGNAL1'S SENSOR
readadc 5,b3                		;READ ADC1
if b3 <120 then goto red5     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
gosub checkswitchs			;CHECK SERIN FOR NEW DATA
goto Monitor5                    	;KEEP LOOPING UNTIL INPUT CHANGES

red5:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red51:
counter=counter+1
if counter=20 then goto signal5
readadc 5,b3				;MONITOR SENSOR FOR INPUT
gosub checkswitchs			;MONITOR SWITCHS FOR CHANGE
if b3 <120 then goto red5
goto red51
 

westaust55

Moderator
AT a quick glance there are several programming problems that can prevent the program working as hoped.

1. You serin some data which could be a value form 0 to 222 and branch (IF...THEN) if certain values are received but just fall through into other code is it is nopt one of the desired values.

2. at line 39 you have a gosub (gosub checkswitchs)
but in that subroutine you have more tests with GOTO command
(if b0=1 then goto signal1)
those further section do further GOTO's and some end up with GOTO such as
if b6=5 then goto powerup
so you are jumping back to the start without any RETURN comamnd which will lead to PICAXE stack overflow and erroneous operation.

Try to look through your program flow and make sure that all GOSUBS have a matching RETURN.

It woiuld also be much easier to follow your program if you applied some program indentation so only labels: started in the first column and nested commands are sequentially indented further by a few spaces.
 

sid

Senior Member
Thanks Westaust55,
I've tidyied the code up so hopefully it is easier to read and have removed the the gosub routine that wasn't returning and have placed it in each section of the program as required (although this has doubled the program size)
(I've had to omit part of the program due the number of charictars accepted by the forum)I've added a small section that sets b4,b5 & b6 on "first startup" so as long as all the switchs are set to thier zero positions the data b4=0 etc will be true.
However I still can't get the code to remember more than one variable, so trying to get it to make sense of b6=b4+b5 is not working.
any advice greatly appreaciated

Code:
;=================================================================================
;V1.1 MULTIPLE SIGNAL CNTL UNIT 8 SIGNALS VIA 3 8BIT SHIFT REGISTERS USING A 20X2
;=================================================================================
symbol counter=w8


powerup:
read 0,b2					;READ MEMORY ON POWER UP
if b2=1  then goto signal1
if b2=2  then goto signal2
if b2=3  then goto signal3
if b2=4  then goto signal4
if b2=41 then goto signal4A
if b2=5  then goto signal5 

initalize:					;SETS THE VARIABLES TO ZERO FIRST TIME USE
b4=0
write 1,b4
b5=0
write 2,b5
b6=0
write 3,b6
 
start:
serin c.2, n1200_8,("ABC"),b0		;FOR FIRST TIME USE 
if b0=1 then goto signal1
if b0=2 then goto signal2
if b0=5 then goto signal4
if b0=7 then goto signal3
if b0=6 then goto signal4
if b0=8 then goto signal4A


;==============================================================================
;SIGNAL 1
;==============================================================================
SIGNAL1:					;SET SIGNAL TO GREEN
b2=1
write 0, b2					;WRITE SIGNAL TO MEMORY
counter=0                    		;SET COUNTER TO ZERO
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001100 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
           		
Monitor1:                        	;MONITOR SIGNAL1'S SENSOR
readadc 1,b3                		;READ ADC1
if b3 <120 then goto red1     	;IF INPUT IS DARK THEN CHANGE THE SIGNAL TO RED
serin [1000,monitor1], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4
			
red1:
pause 800
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
red11:
counter=counter+1
if counter=20 then goto yellow1
readadc 1,b3				;MONITOR SENSOR FOR INPUT
if b3 <120 then goto red1
serin [1000,red11], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4

yellow1:
b1 = %00100100 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers	;DATA TO SHIFT REGISTER
b1 = %01001010 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10                	;LATCH REGISTERS
counter=0
yellow11:
readadc 1,b3                		;READ ADC1
if b3 <120 then goto red1
counter=counter+1
if counter=15 then goto signal1
serin [1000,yellow11], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4				

;==============================================================================
;SIGNAL 2
;==============================================================================
SIGNAL2:					
b2=2
write 0, b2					
counter=0                    		
b1 = %00100100 :gosub registers  
b1 = %10010010 :gosub registers    	
b1 = %01100001 :gosub registers	
pulsout c.4,10              

Monitor2:                        	
readadc 2,b3               
if b3 <120 then goto red2    
serin [1000,monitor2], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4			
                   
red2:
pause 800
b1 = %00100100 :gosub registers	
b1 = %10010010 :gosub registers
b1 = %01001001 :gosub registers	
pulsout c.4,10                
counter=0
red21:
counter=counter+1
if counter=20 then goto yellow2
readadc 2,b3							
if b3 <120 then goto red2
serin [1000,red21], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4

yellow2:
b1 = %00100100 :gosub registers	
b1 = %10010010 :gosub registers	
b1 = %01010001 :gosub registers	
pulsout c.4,10                
counter=0
yellow21:
readadc 2,b3                	
if b3 <120 then goto red2
counter=counter+1
if counter=15 then goto signal2
serin [1000,yellow21], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4				

;==============================================================================
;SIGNAL 3
;==============================================================================

SIGNAL3:				
b2=3
write 0, b2					
		

;=============================================================================
;SIGNAL 4
;=============================================================================
SIGNAL4:
b2=4
write 0, b2					


;============================================================================
;SIGNAL 4A
;============================================================================					
SIGNAL4A:
b2=41
write 0, b2					


;============================================================================
;SIGNAL 5
;============================================================================
SIGNAL5:
b2=5
write 0,b2
counter=0                    		
b1 = %00100100 :gosub registers    
b1 = %11000010 :gosub registers    	
b1 = %01001001 :gosub registers	
pulsout c.4,10    			
           		
Monitor5:                        	
readadc 6,b3               
if b3 <120 then goto red5     	
serin [1000,monitor5], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4                    

red5:
pause 800
b1 = %00100100 :gosub registers	
b1 = %10010010 :gosub registers	
b1 = %01001001 :gosub registers
pulsout c.4,10                	
counter=0
red51:
counter=counter+1
if counter=20 then goto signal5
readadc 5,b3						
if b3 <120 then goto red5
serin [1000,red51], c.2, n1200_8,("ABC"),b0
if b0=1  then goto signal1
if b0=2  then goto signal2
if b0=5  then goto signal4
if b0=6  then goto signal4
if b0=7  then goto signal3
if b0=8  then goto signal4A
if b0=9  then goto vd1
if b0=10 then goto vd2
if b0=13 then goto vd3
if b0=14 then goto vd4

;==============================================================================
;SET ALL SIGNALS TO RED
;==============================================================================
allred:
b1 = %00100100 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %10010010 :gosub registers    	;DATA TO SHIFT REGISTER
b1 = %01001001 :gosub registers	;DATA TO SHIFT REGISTER
pulsout c.4,10    			;LATCH REGISTERS
pause 10
goto start

;============================================================================
;REGISTERS	
;============================================================================
registers:
for b0= 1 to 8
if b1 > 127 then high c.7 else low c.7
endif
pulsout c.5,10
b1 = b1 *2
next b0
return 


;==============================================================================
;VARIBLE DATA USED TO CALCULATE WHICH WAY SWITCHS 9,10,13,14  ARE SET
;==============================================================================
vd1:
b4=0
write 1,b4
goto vd5

vd2:
b4=4
write 1,b4
goto vd5

vd3:
b5=0
write 2,b5
goto vd5

vd4:
b5=5
write 2,b5
goto vd5

vd5:
b6=b4+b5
write 3,b6
if b6=9 then goto signal5
if b6=4 then goto allred
if b6=5 then goto powerup
if b6=0 then goto powerup
 
Last edited:
Top