Accessing MSB and LSB of a System Word Variable

Ive got into the habit of defining symbols for the most significant and least significant byte of Word variables.

e.g.

symbol Pitch = W4
symbol PitchMsb = B9
symbol PitchLsb = B8

where LSB is 2* the Word Variable number of the word and MSB is 2*Word Variable number + 1. These MSB and LSB variables have become my go to way for loading up a word variable from separate byte variables.

Having now grown desperate for variables, i've just started using the Systems Word Variables S_W0 to S_W7. My question is can i separately load the MSB and LSB of system word variables ? I dont know of equivalent byte variables for a system word MSB or LSB....
 
Hi,

Sorry, no the S_W bytes can't be accessed individually and I've actually encountered Bug-like behaviour when using system functions that imply access to the individual bytes (can't recall the exact details at the moment).

So no, with M2 devices you should use S_W1 to S_W6 only for pure Word variable functions (I wouldn't risk the OS stamping on S_W0 and S_W7).

But don't forget that you do have at least 100 (08M2) up to almost 500 "Indirectly addressable" bytes via PEEK/POKE, and more usefully @BPTR, if you really are running out of variables.

Cheers, Alan.
 
I try to keep a standard word variable free for short-term use. Some variables are only needed temporarily within a subroutine: you may be able to utilise one (word) or two (bytes) of these to operate as a temporary store. Eg. Copy S_Wx to a spare word register, access the bytes separately and then copy the updated word back to S_Wx.

Careful structuring of your code into subroutines can help release variables that are only used to store data temporarily, obviating the need for more variable storage.
 
Hi,

Sorry, no the S_W bytes can't be accessed individually and I've actually encountered Bug-like behaviour when using system functions that imply access to the individual bytes (can't recall the exact details at the moment).

So no, with M2 devices you should use S_W1 to S_W6 only for pure Word variable functions (I wouldn't risk the OS stamping on S_W0 and S_W7).

But don't forget that you do have at least 100 (08M2) up to almost 500 "Indirectly addressable" bytes via PEEK/POKE, and more usefully @BPTR, if you really are running out of variables.

Cheers, Alan.
Hi Alan

Thanks for that. Hugely appreciated.
Hi,

Sorry, no the S_W bytes can't be accessed individually and I've actually encountered Bug-like behaviour when using system functions that imply access to the individual bytes (can't recall the exact details at the moment).

So no, with M2 devices you should use S_W1 to S_W6 only for pure Word variable functions (I wouldn't risk the OS stamping on S_W0 and S_W7).

But don't forget that you do have at least 100 (08M2) up to almost 500 "Indirectly addressable" bytes via PEEK/POKE, and more usefully @BPTR, if you really are running out of variables.

Cheers, Alan.
Hi Alan

Thanks for your reply. Hugely appreciated. And thanks for the caution on S_W1 and S_W7 too. It crossed my mind the OS would not respect my choice to use them too ....

I have already invested in using @BPTR & indirectly addressable bytes for storing the content of incoming data packets. That works pretty nicely. Although I dislike indirect referencing of varibles as it seems to hugely increase the possibility of coding errors and makes the code much less readable. I never much enjoyed my time as a C programmer for that reason. Probably a limitation of my brain :-)

Where i'm struggling is i'm writing some twos complement arithmatic routines for my Low Power Model Rocket Telemetry project. I need to find the offset of rate gyros, correct that offset, integrate to convert angular rate to attitude angles, tune out any remaining drift etc. These are borderline comprehensible when written using nicely named Word variables. And become a nasty tangle when i start moving the interim calculations into indirectly addressed bytes and back again. But, as long as i havent missed an alternative, i will soldier on :)
 
I try to keep a standard word variable free for short-term use. Some variables are only needed temporarily within a subroutine: you may be able to utilise one (word) or two (bytes) of these to operate as a temporary store. Eg. Copy S_Wx to a spare word register, access the bytes separately and then copy the updated word back to S_Wx.

Careful structuring of your code into subroutines can help release variables that are only used to store data temporarily, obviating the need for more variable storage.
Thanks Pete. I've conceptually partitioned the code and overloaded different variable symbols in different parts of the code onto the same underlying Byte and Word level variables. And introduced some really nasty unexpected coupling between different bits of code when ive got it wrong :). I think years of structured high level languages has made me lazy. Its one of the reasons why Ive gone back to playing with microcontrollers ....gets you closer to the beautiful hardware and it keeps you thinking !

Thanks for your help, much appreciated
 
Hi,

It must be accepted that mathematical calculations are not one of PICaxe's strengths, but I have written a number of 16-bit signed subroutines. Others have attempted writing Floating Point calculation routines, but I don't recall any being very useful in practice. I think I'm most pleased with my Sunrise and Sunset Calculator but the most ambitious was a 32-bit Barometric/Humidity Sensor which involved an equation with 33 levels of parenthesis. PICaxe has none. :(

Of course, you can allocate numerous different variable names to the same base variable (byte/word), but must make very sure that you don't use them at the same time. PICaxe's lack of "Local Variables" or general "Bank Switching" does mean that you probably need to devise strategies for yourself; Personally, I usually reserve b1 through to about b7/w3 as Temporary/Local variables and for passing parameters into subroutines, and b0 for Global flags or real "emergency" use. Similarly you might need to pass a "pointer" to a bank of data, in to a subroutine. The PICaxe Editor's Simulator can be very useful for some debugging, but can be very slow for serious number crunching. I've posted a number of general purpose routines in the Code Snippets section of the forum (and you may soon recognise about half a dozen other regular authors). But the forum's Search facility can be rather obstructive and you might do better with a Google search including PICaxe as a keyword.

Cheers, Alan.
 
Back
Top