Write\Read

JAM1959

Member
I am not sure how to use read and write code in my main ADC code.I need to save data to 08m2 internal memory then read it at a later stage. I am using readadc 10.
 

JAM1959

Member
Write ADR, Word wX

Read ADR, Word wX

ADR is an even number 0 to 254 ($FE), or a variable holding such a number (*)

wX is a word variable, w0, w1, w2 etc

http://www.picaxe.com/BASIC-Commands/Variables/write
http://www.picaxe.com/BASIC-Commands/Variables/read

* Technically it can be any number but it can avoid coding problems and errors when using word values to use only even numbers.
Thanks. Now do I put the write code before ADC code and read code after ADC code
 

hippy

Technical Support
Staff member
Thanks. Now do I put the write code before ADC code and read code after ADC code
You have to put the WRITE after READADC or the data being written won't be what the ADC has read.

The READ will need to be wherever your project requires it to be.
 

JAM1959

Member
You have to put the WRITE after READADC or the data being written won't be what the ADC has read.

The READ will need to be wherever your project requires it to be.
This is my code

Main

Readadc10 1,w0
for w0 = 0 to 3
Serin 1,n2400,w1
next w0

for w,0 = 0 to 3
Read w0,w1
Serout 0,n2400,(#w1,w0,13)
next w0
goto main
 

lbenson

Senior Member
The value of w0 which you receive with the READADC statement is immediately lost in the FOR statement, which starts by initializing w0 to 0.

You read a value for w1 with SERIN, but don't do anything with that value--the loop exits with w1 having the last read value, but nothing is done with it.

For your SEROUT, do you want to output the binary value of w0 or the ASCII numeric characters, "0", "1", "2", "3". If you want the numerals, you need to use #w0.

You might do best to start playing with the very helpful PICAXE simulator in either PE5 or PE6--you can step through your code one statement at a time and see exactly what is happening with the code flow and the variables.
 

JAM1959

Member
This is my amended code
main

readadc10 1,w0
for w0 = 0 to 3
write w0,w1
serin 1,n2400,w1
next w0

for w0 = 0 to 3
read w0,w1
serout 0,n2400,(#w1,w0,13)
next w0
goto main
 

The bear

Senior Member
Post #6
You have not corrected; 'for w0 = 0 to 3', you have instructed w0 to equal zero!

Regards, bear..
 

The bear

Senior Member
Code:
main:

For b4 = 0 To 3
Readadc10 1,w0

Write w0,w1

Serin 1,N2400,w1
Sertxd (Cr, Lf,#w1,"  " )
Sertxd (Cr, Lf,#w0,"  " )
Next b4

If b4 > 3 Then
Endif

For b4 = 0 To 3
Read w0,w1

Serout 0,N2400,(w1,w0) 
Sertxd (Cr, Lf,#w1,"  " )
Sertxd (Cr, Lf,#w0,"  " )
Next b4
Goto main
Saw the example, I've never used Write & Read, just experimenting with your code,

Regards, bear..

@ hippy, If I've messed things up, apologies.
 

lbenson

Senior Member
Bear--since readadc10 can return a value of up to 1023, "write w0,w1" is not likely to be what is wanted.

It would be best if the op described the intended behavior.
 

JAM1959

Member
Thanks for code.I am trying to get data from load cell and then saving to internal memory.Will ur code work for that.
 

hippy

Technical Support
Staff member
If you want to take four 10-bit ADC readings, store them in EEPROM, wait for a serial signal, then send those four readings out then you probably want something like -

Code:
Main:

  for w0 = 0 to 3
    w1 = w0 * 2
    Readadc10 1,w2
    Write w1, Word w2
  next w0

  Serin 1,n2400,w1

  for w0 = 0 to 3
    w1 = w0 * 2
    Read w1, Word w2
    Serout 0,n2400,(#w0, " ", #w2, CR, LF)
  next w0

  goto main
 

JAM1959

Member
Thanks for code.I read while Googling that continued data stream into memory is not good for picaxe.I tried code did not work
 

lbenson

Senior Member
... I tried code did not work
If you are saying that hippy's code didn't work, you need to tell us how it didn't meet your requirements.

That code will--very quickly (in milliseconds)--take and store 4 ADC readings, so if that's not what you want, then what +do+ you want?

... continued data stream into memory is not good for picaxe
If I recall correctly, the picaxe eeprom is good for 100,000 writes (and maybe many more, but with the risk of having errors). Writing to eeprom many times per second will quickly cause the memory to fail.
 
Last edited:

The bear

Senior Member
Works for me. I don't understand it 100% yet, I've saved the example, it will come in useful one day.

Regards, bear..
 

JAM1959

Member
I have built a machine to test car shocks working with air. Compress shock take load reading from load cell and time in seconds using stop watch connected to micro switched.Then extend shock and take readings. All I wanted to do was save load reading in memory and access later.I am using 08m2.Load Cel is connected to an INA 125.The time is only maybe 20 sec on each stroke compressed or extended.
 

lbenson

Senior Member
Maybe you want a pushbutton you can press in order to tell the program when you want the reading to be made. As noted, without pauses or anything else to slow the program down, the 4 ADC readings will be completed in milliseconds.

Perhaps like this (with an appropriately wired pushbutton switch on pinC.3--see manual 3).
Code:
Main:

  for w0 = 0 to 3 
    do while pinC.3 = 0 : loop ' wait until button has been pressed
    w1 = w0 * 2
    Readadc10 1,w2
    Write w1, Word w2
    do while pinC.3 = 1 : loop ' wait until button has been released
  next w0

  Serin 1,n2400,w1

  for w0 = 0 to 3
    w1 = w0 * 2
    Read w1, Word w2
    Serout 0,n2400,(#w0, " ", #w2, CR, LF)
  next w0

  goto main
When you say, "Has not worked" it will help if you can explain in detail what results you got and how they differ from what you expected.
 

JAM1959

Member
Hello.I loaded ur program into Picaxe.Connect Picaxe to terminal open programming editor.Open terminal F8.On the output buffer put any number in click send.On the input buffer it does not come out as numbers.Have set bauld rate at 2400.So is working now just outputting gibberish.
 

lbenson

Senior Member
Exactly what is output?

Please post a clear photo of your circuit.

Instead of using SEROUT 0, try using SERTXD, and put SERTXD ("Starting",cr,lf) before main so you are sure you have the terminal settings right.
 

The bear

Senior Member
More info required, you are posting in 'bits', 'bytes' would be better, could you stretch it to 'word'?

More info = More response.

Regards, bear..
 

JAM1959

Member
Sorry I do not have proper internet sending from cell phone.I have had this problem before not outputting numbers something to do with bauld rate. Above program works in simulation. In serial terminal not.
 

Attachments

lbenson

Senior Member
Did you change SEROUT 0 to SERTXD and add a startup message so you can be sure that you have your terminal connection right?
 

JAM1959

Member
Yes I did.What happens is I get two lines of numbers. Then next time a line of junk no numbers.Why is that?
 

The bear

Senior Member
Come on JAM1959, at 59 you are in your prime.
Let us see where you are up to.
These things come to test us.
While you are enjoying the sunshine, some of us are below 0°C.

Regards, bear..
 

JAM1959

Member
As I said it works in simulation but once connected pushes out garbage. I am sure it is bauld related.I am using last code submitted on this forum.
 

lbenson

Senior Member
Last code submitted on the forum doesn't include the change to sertxd.

Need your confirmation that you are seeing the "Starting" message and then what the output is. Because there is "many a slip", it would be best if you post the actual code that you are programming onto the PICAXE in its entirety as you now have it.
 

lbenson

Senior Member
One other thought. When you run the real picaxe, are you still connected to the programming cable and viewing the output in the picaxe terminal?

If not, and if you are using something like USB serial to another device, then you will need to invert the output with respect to the way it is for the picaxe (actually, the picaxe signals are called "inverted" and the other is non-inverted).

To do that, use "Serout 0,t4800,(#w0, " ", #w2, CR, LF)"--note "t4800" instead of "n4800". SERTXD uses the "n" version--effectively n4800.

Is there a reason why you wanted to use 2400 baud instead of 4800?
 

JAM1959

Member
sertxd("starting",cr,lf)

Main:

for w0 = 0 to 6
do while pin.C ,= 1:loop
W1 = w0 * 2
readadc10 1,w2
write w1,word w2
do while pinC.3 = 1:loop

next b0

serin 1,n4800,w1
for w0 =0 to 6
w1 = w0 * 2
sertxd (#w0,'" "",#w2,cr,lf,)

next w0

goto main

It still pushes out junk but a end the word STARTING comes up
 

hippy

Technical Support
Staff member
readadc10 1,w2

serin 1,n4800,w1

That likely won't work. One shouldn't be using the same pin for ADC and SERIN.

do while pin.C ,= 1:loop

That won't even compile so what is posted is not exactly what you are putting into your PICAXE.
 

lbenson

Senior Member
Your code can't have been a copy of what was programmed because it wouldn't pass syntax, and omitted the READ line. I've fixed it in a way that I hope is appropriate, and programmed it on an 08M2 with a pot to vary the ADC reads. It works for me.
Code:
' 08readLoadCell
#picaxe 08m2
#terminal 4800
#no_data

pause 2000
sertxd("starting 08readLoadCell",cr,lf)

Main:

for w0 = 0 to 6
  do while pinC.3 = 0:loop
  w1 = w0 * 2
  readadc10 1,w2
  write w1,word w2
  pause 1000
  sertxd (#w0," ",#w2,cr,lf)
  do while pinC.3 = 1:loop
next w0

'serin 2,n4800,w1
sertxd (cr,lf)

for w0 = 0 to 6
  w1 = w0 * 2
  read w1,word w2
  pause 1000
  sertxd (#w0," ",#w2,cr,lf)
next w0

goto main
Here's the output:
Code:
starting 08readLoadCell
0 150
1 44
2 312
3 313
4 131
5 28
6 15

0 150
1 44
2 312
3 313
4 131
5 28
6 15
08readLoadCell2b.png08readLoadCell.jpg
 
Top