Register question

Pic8581

New Member
Hi

I would like to add up and average the content of a bunch of registers and
wonder how this can be done most efficient. (PICAXE 20M2)



I started with


Code:
let w0 = b34+b35+b36+b37+b38+b39+b40+b41
w0=w0/8

only to realize that for some strange reason I am getting a syntax error if I use a register
that is larger than b27 although the simulation does allow me to look at b30, b31.... strange..

For instance:

Code:
let w0 = b28
do I really have to do something like

Code:
peek 34,b1
w0=w0+b1
peek 35,b1
w0=w0+b1
...
??

Thanks
 

tony_g

Senior Member
not completely sure but i think the "M2" range of picaxe only has directly accessible (or whatever its called) variables up to b27, the "X2" picaxe has more.

im sure their are ways around this but thats not something im familiar with as i never use all of them in any one program.


tony
 

inglewoodpete

Senior Member
Hi Pic, You must be new to the PICAXE. The M2 (and many older) PICAXEs are limited to 28 byte registers. This is because of the limited resources available in the lower-spec PIC chips. As you have discovered, there is more RAM available to you via Peek and Poke.

All is explained in the BASIC Command Manual (Manual 2), page 11 in the section titled "Variables - General".

When saving and recalling data using Poke and Peek, you can "stack" the operations to make coding simpler:

Code:
Poke 34, b0, b1, b2, b3, b4  'Fetch contents of location 34 into b0; loc 35 into b1 etc
Another way to save and recall data in RAM is to use bptr, which is a system variable that can be used to point to RAM:

Code:
bptr = 34
b0 = @bptr          'Fetch the value in RAM location 34
'
bptr = 40
b0 = @bptrinc       'Fetch the value in RAM location 40, then increment the pointer
b1 = @bptrinc       'Fetch the value in RAM location 41, then increment the pointer
b2 = @bptr          'Fetch the value in RAM location 42
 

westaust55

Moderator
When using the @bptr indirect addressing pointer you can then easily sum a number a values

Code:
Bptr = start_register
b1 = 0 ; clear summing register - used word variable if result may be > 255
FOR b0 = 1 TO Number_Entries
    b1 = b1 + @bptrinc ; add in the next value and increment pointer
NEXT
b1 = b1 / Number_Entries ; calculate average
 

westaust55

Moderator
But note that if your sum may exceed 255, you need to sum into a word register.
Very true and reinforced in the second line of my code snippet above (complete with typo :eek: ):
b1 = 0 ; clear summing register - used word variable if result may be > 255
 
Top