read and write problems.

JPU

Senior Member
Hi

I am trying to store a binary value o or 1 into a permanent location which remains when the chip is switched off. However no matter what I do the code always defaults to displaying a value which represents a zero value being read from the eprom. So if I try to write 1 to location 1 eg write 1,1 and then i read the value read 1,b1 b1 is always returned as 0

I know its something simple that I am missing and I am probably over looking but I am totally stuck!

Can someone please look at this example code and tell me why no matter what selection is made (pin1 or pin2 made high) the words "stored value is 0" are always displayed. (I know the code doesn't allow for switch bounce etc, its only to demonstrate my problem)

Thanks for your help.

Code:
symbol swtch=b1


main:

read 1,b1



if swtch=1 then
	
	
	serout B.1,N2400,(254,72) ; move to start of first line
	serout B.1,N2400,("stored value is 1")
end if

if swtch=0 then 
	serout B.1,N2400,(254,72) ; move to start of first line
	serout B.1,N2400,("stored value is 0")
	
end if

do while pin3<>1
	
if pin1=1 then 
	write 1,1
end if

if pin2=1 then
	write 1,0
endif


loop



goto main
 

lbenson

Senior Member
Works for me with 14M2 and assuming that pin1, pin2, and pin3 are the "C" port pins (which they are).
Code:
#picaxe 14M2
#terminal 4800
  symbol swtch=b1

main:

read 1,b1

if swtch=1 then
	serout B.1,N2400,(254,72) ; move to start of first line
	serout B.1,N2400,("stored value is 1")
end if

if swtch=0 then 
	serout B.1,N2400,(254,72) ; move to start of first line
	serout B.1,N2400,("stored value is 0")
	
end if

do while pin3<>1
	
if pin1=1 then 
	write 1,1
end if

if pin2=1 then
	write 1,0
endif

loop

goto main
 

lbenson

Senior Member
With an 08M2, you have a conflict: pin1 and pinB.1 are the same pin. Try changing one or the other to pinC.4.

"pin1" without the port designation is an old style for the 08M (and 08M2, and implying port C on the 14M2)--best to label them all pinC.#.
 

JPU

Senior Member
Works for me with 14M2 and assuming that pin1, pin2, and pin3 are the "C" port pins (which they are).
Oh, thats strange as I was convinced I was missing something from the read and write statement since I can not get this to work.
 

tmfkam

Senior Member
The contents of the Eeprom may be overwritten when the PicAxe is reprogrammed.

Use #NoData to prevent this.
 

hippy

Technical Support
Staff member
I would suggest starting with this ...

Code:
#Picaxe 08M2
#Terminal 4800
#No_Data

Read 1, b1
b1 = b1 + 1
Write 1, b1

Do
  SerTxd( "Value stored is now ", #b1, CR, LF )
  Pause 1000
Loop
Every time the PICAXE is turned off and then back on it will show an incremented number.

To just toggle between a 0 and 1, replace b1=b1+1 with -

b1 = b1 & 1 ^ 1
 

JPU

Senior Member
Hi all

Thanks for all your suggestions. It did help me find the problem as I was convinced my syntax was incorrect but by your example suggestions I was able to see it was correct and then i was able to see the wood for the trees. I am using all the available variables in my programme and I thought b26 was free but then realised I was using W13 in a subroutine.

Oops. :eek:

Thanks for your help.
 

AllyCat

Senior Member
Hi,

I am using all the available variables in my programme ...
Actually, probably not. With an M2 there are the six unused "system" words (s_w1 to s_w6) and also s_w0 (if not using multitasking), which can be used like any other words (except they cannot be addressed as separated bytes). That gives 50% more variable space when working with word variables.

Then there is bptr, which also gives access to another 100+ bytes of RAM (@bptr) even with an 08M2. Very useful for "buffering" data through serial I/O commands, using @bptrinc.

Cheers, Alan.
 

techElder

Well-known member
... and then there is this.

I bet you don't NEED to use all the variables available in your program. :D

Post the code for further information and optimization!
 

westaust55

Moderator
Hi all
I am using all the available variables in my programme

Thanks for your help.
While over the entire length of your program you may have used all of the pre defined variables, it is very likely that not every variable needs to have the value retained all the time.

For example the variable used for FOR...NEXT loops can be the same where the variables are not nested.

If a variable is only used in some parts of the program, it is likely that it can be used elsewhere in the program for another purpose.

I have written some quite long and complex programs in the past where a number of the variables are used for different purposes (up to three each) with different SYMBOL names (you can use the same variable in multiple SYMBOL statements).

To do so it is a good idea to make a table with the main loop and subroutines on say the horizontal axis (columns) and the variables on say the vertical axis (rows).
As well as that under the subroutines list all of the calling routines/parts and put say an "X" at the columns and row for each variable where used.
If you then identify which variables MUST keep the current value for re-use again, you can then identify which variables can be reused for a different purpose in other subroutines.

Hope that description makes sense.
 
Top