PEEK/READ with Word Variables

hippy

Technical Support
Staff member
PICAXE-18X Firmware 8.1, Editor 4.1.4

It's probably going to be argued as not a bug, but PEEK and READ behaviour is not what I'd expected, not clearly explained in the manual, nor consistant with other PICAXE statements ...

- w0 = $1234
- POKE $50,$AB
- PEEK $50,w0
- SERTXD(#w0,CR,LF)

- w0 = $1234
- WRITE $50,$AB
- READ $50,w0
- SERTXD(#w0,CR,LF)

In both cases the value in w0 after the PEEK or READ is 4779 ($12AB); only the LSB is updated.

I expected that the MSB would be cleared as it is when performing the following 'take a byte value and store it in a word variable' statements ...

LET w0 = b2
INFRAIN w0
INFRAIN2 w0
READADC PIN,w0
READI2C LOC,w0
READTEMP PIN,w0
SERIN PIN,BAUD,w0

Do any of the above actually not clear the MSB when reading a byte value ?

Obviously when using PEEK or READ to retrieve a word value, two such statements need to be done to obtain the MSB and LSB, but it's the handling logic of a byte value being stored in a word value is where the discrepency / confusion occurs.

Personally I'd like to see the MSB cleared when using word variables, or the command limited to only accepting byte variables as the operand.

While the manual alludes to only byte variables being used with PEEK, the READ desciption infers that any variable can be used, and doesn't mention this side effect ( ditto for READMEM ).
 

Technical

Technical Support
Staff member
Yes, we would argue this is not a bug but a user mistake, if you try and save a word variable in a byte space the data is often going to corrupt!

Perhaps we should highlight these are byte only commands a bit more in the manual, but the manual does already describe this behaviour - from the 'peek' command description in part 2 of ther manual...

<i>The most commonly used function is to store temporary byte data in the
microcontrollers spare &#8216;storage variable&#8217; memory. This allows the general purpose
variables (b0 to b13) to be re-used in calculations. Remember that to save a word
variable two separate poke/peek commands will be required - one for each of the
two bytes that form the word. </i>

The read description also carries a similar warning
<i>The read command is byte wide, so to read a word variable two separate byte read
commands will be required, one for each of the two bytes that makes the word
(e.g. for w0, read both b0 and b1). </i>

We understand your point about clearing the upper byte in a word as readadc etc. do in fact do, but this is by design a byte command set and there are no plans to change this behaviour at present.


Edited by - Technical on 8/12/2004 12:39:40 AM
 
Top