How to store Data

Andrew Cowan

Senior Member
As part of my robot project, I've found that I've run out of variables.

A main 28X2 in the robot collects data from a network of other PICs, and then sends the data across the radio link.

A 20X2 at the other end receives the data, and displays it on an LCD screen.

The problem is that I've run out of variables - there are about 80 bytes of data that need sending. My current setup (with about 50 bytes of data), uses one byte variable (b1, b2, b3 etc) for each bit of data.

I haven't used the scratchpad before - would I be wise to allocate a scratchpad location to each data byte, then get it out before sending?

EG:
In the 28X2 in the robot:
  • Collect all the data from the slave PICs, and put the data into scratchpad locations 1-70.
  • Transfer the first 40 bytes into b1 to b40.
  • Send these across the wireless link.
  • Transfer the next 40 bytes into b1 to b40.
  • Send these across the wireless link.
  • Repeat

And on the receiving end, do the opposite.

Does this sound like a good solution? I'm happy to send the data in several packets. Can the symbol command be used as it can be with byte variables? It is very neat (code wise) to have code such as if CO2>25 then....

Thanks,

Andrew
 
Last edited:

hippy

Technical Support
Staff member
Untested but with all the bytes stored in scratchpad you can churn them out all in one go ...

ptr = 0
SerOut TX, BAUD_RATE, ( "UUUUUUUUUUUUUUU" )
Pause 3
SerOut TX, BAUD_RATE, ( "XYZZY" )
For b0 = 1 To 80
SerOut TX, BAUD_RATE, ( "U", @ptrInc )
Next

The "U" characters are to give the data stream a more evenly balanced number of zero and one bits.

The receiver could copy data into its scratchpad in the same way. You may need to add some PAUSE or PAUSEUS in the transmitter FOR-NEXT to give the receiver enough time, possibly also between the "U" and data.

ptr = 0
SerIn RX, BAUD_RATE, ( "XYZZY", "U" ), @ptrInc
For b0 = 1 To 80
SerIn RX, BAUD_RATE, ("U"), @ptrInc
Next
 

BeanieBots

Moderator
No need to transfer to seperate variables, certainly not one for each value.
ptr is a pointer and @ptr is the value pointed to by the pointer.

So, ptr = 0, b0=@ptr gets the first value (which you can then send)
ptr = 1, b0=@ptr gets the second value etc. etc.

b0 = @ptrinc gets the value currently pointed to AND increments the pointer ready to get the next value.

@ptr (or @ptrinc/dec) can be used wherever you would use a 'regular' variable.
 

bpowell

Senior Member
Hello,

I am working on a project that collects various data from my Pump House and then sends it via RF to a receiver and LCD...I'm not necessarily running out of variables on the transmitter side, but in an effort to optimize the code, some folks here came up with some GREAT ideas...one of them I'm going to incorporate...basically, I have two (currently) variables assigned to basically on/off reporting...

Is Pump on? If so, set b0 to 1
Is Heater on? If so, set b1 to 1

So I'm using two bytes (16 bits!) to store and transmit 2 bits of data...

So, I'm going to consolidate that down to one byte by just setting the bits in that byte!

Is Pump on? If so, b0 = b0 + 1
is Heater on? If so, b0 = b0 + 2
Is Pump off? If so, b0 = b0 - 1
is Heater off? if so, b0 = b0 - 2

So, basically I'm using the two LSB of byte b0 to store these on / off stats...on the receiver, (I'm just using LED's to reflect the data) I can set pins and then let pins = b0...easy!

Maybe this will help?

If your robot is sending lots of yes / no, on / off data, you can really get 8 data-points in one byte.

Brendan
 

Andrew Cowan

Senior Member
Thanks for the replies.

Most of the data uses the full byte (there is even a word or two in there). I'll have a play with the scratchpad tomorrow, and report back.

Regards

Andrew
 

BeanieBots

Moderator
@bpowell,

You could potentially make that method much more readable with symbols.

Symbol Pump = bit0
Symbol Heater = bit1

If Pump = 1 then... to see if it's on
Pump = 1 to turn it on.

You could also use symbol for the states.

Symbol Switched_On = 1
Symbol Switched_Off = 0

If Pump = Switched_On then....
Pump = Switched_On to turn it on
 

bpowell

Senior Member
@bpowell,

You could potentially make that method much more readable with symbols.

Symbol Pump = bit0
Symbol Heater = bit1

If Pump = 1 then... to see if it's on
Pump = 1 to turn it on.

You could also use symbol for the states.

Symbol Switched_On = 1
Symbol Switched_Off = 0

If Pump = Switched_On then....
Pump = Switched_On to turn it on
BeanieBots:

I don't want to hijack this thread, but thank you! I've never worked with "bit0, bit1" before, and I forgot you could even address bits...

Thanks again!

Brendan
 

BeanieBots

Moderator
I'm sure AC will forgive you;)
Don't forget setbit / clearbit and togglebit can be used on any variable on the 20/28/40/X1/X2 variants.

Also, ptr can be used just like any other variable.
So, Hippy's example could be re-written as:-

ptr = 0
SerIn RX, BAUD_RATE, ( "XYZZY", "U" ), @ptr
For ptr = 1 To 80
SerIn RX, BAUD_RATE, ("U"), @ptr
Next ptr

and therefore use no b# variables at all.
 

Andrew Cowan

Senior Member
I don't want to hijack this thread
Don't worry about it.

All PICAXEs support the bits of b0 and b1 (bit0 to bit15), and X1 and X2 PICAXEs also support the bits of b2 and b3 (up to bit31).

A

Edit - clashed with Bb.

The scratchpad looks like a very neat solution! 1024 bytes - I'll have a hard job filling that up.
 
Top