Addressing bits in loop counters

Chips 28X1 & 18M2

I am trying to set/clear some bits. At the moment I am setting/clearing separately e.g.

Bit1 = 0
Bit2 = 0
and so on

What would be easier would be to be able to do something like;
Counter1 being a register B5 and above
For counter1 = 3 to 11
BitCounter1 = 0
next counter1

Similarly I need to interrogate the bits

For counter1 = 3 to 11
If BitCounter1 =1 then........
Next Counter1

Any suggestions?

Thanks
 
Dear HertzHog,
Thanks for being quick of the mark with a reply.

I am aware that the 32 bits double up as B0 to B3 in both the mentioned chips.

Maybe my question wasn't clear enough.
I need to set & interrogate, and set a number of bits (I used 3 to 11 in the above question) the bits may/may not be multiples of bytes, as you implied.

If I was examining Bytes I can use indirect addressing. Is indirect addressing available for bytes?

Cheers
 
last line should be:
If I was examining Bytes I can use indirect addressing. Is indirect addressing available for bits?

Sorry about the typo
 

nick12ab

Senior Member
There's no 'bitpointer' for bits like there's a byte pointer for bytes.

Does using a bit mask help?
Code:
b1 = b1 AND %01010101
In that example, bits 9,11,13 and 15 will be cleared and the other bits in that byte are retained.
 

pvdven777

New Member
On the X2 parts couldn't you just use setbit/clearbit ?
Example :
setbit b6, 0 ' setbit bytevar, countervar

Otherwise you can manipulate bits by maths.
I am using this following solution in a loop within a piece of code which I intend to post in a day or so :
Sendcode = Sendcode + 1 ' Set least sigificant bit of variable.

Reading back individual bits can be done using a mask :

for counter = 1 to bits ' Count up to number of bits to send
mask = var_out & 128 ' Mask MSB
if mask = 0 then low testvar else ' work out if bit is high or low
high testvar
endif
next counter ' Loop to check out next bit

I've got 128 hardcoded to get the most significant byte. Then on each loop (not shown in sample code) I shift the byte value to get the next bit. You could also use a variable instead to point to a certain bit on each loop. (This is actually a modified piece of sample code from the bitbang example in the picaxe manual)

Hope that helps !

Patrick
 

hippy

Ex-Staff (retired)
To determine a bit value using an index you can do this -

LookUp index, ( bit0, bit1, bit2, ... , bit30, bit31 ), bitValue

There's no simple equivalent for setting or manipulating bits, but you can use LOOKUP to convert index to a bit mask and apply that, for bits 0 to 15 -

Lookup index, ( $1, $2, $4, $8, $10, $20, ... , $4000, $8000 ), bitMask
w0 = w0 | bitMask

For 32 bits you could use ...

Lookup index, ( $1 ... $8000, $1 .. $8000 ), bitMask
If index <= 15 Then
w0 = w0 | bitMask
Else
w1 = w1 | bitMask
End If

SET : w0 = w0 | bitMask
CLR : w0 = bitMask ^ $FFFF & w0
TOG : w0 = w0 ^ bitMask
GET : bitValue = w0 & bitMask Max 1
 
Hi Hippy,
The mask method you suggest is the one I am using, it works well but is messy to compose code for.

Thanks for the verification.
Gonzo aka Summertown
 
Top