reading a bit

jrem

New Member
sorry for posting this if it has been posted before, but I've been struggling with how to read a specific bit of a variable, i.e., x = b0,4 where x is my control, b0 is the byte, and 4 is the bit.

Any simple ideas?
 

SAborn

Senior Member
I can not make head nor tail of what it is you want to do.

X= control ...What control?

4 is the bit.... bit to what?

You might need to explain a bit better of what it is you want to do.
Maybe someone else can figure the puzzle out.
 

lbenson

Senior Member
If you run the following program in the simulator, you will see that the value of b7 (which is set to the value of bit4) toggles when you toggle pin4.

The bit values, bit0-bit7 have the contents of the bits 0-7 in b0.

Code:
#picaxe 08M2

dirsc = %01000
b0 = pinsc
main:
  do
    if b0 <> pinsc then
      b0 = pinsc
      b7 = bit4
    endif
  loop
If this was not the information you were looking for, you will need to restate your question.
 

jrem

New Member
yes, thank you, but I would like to read an internal variable byte, look at the bit and control an output.
 

westaust55

Moderator
Then you need some PICAXE code akin to (this is generalised since you do not say which PICAXE you are using):

for older (non X2 or M2) chips: PEEK <location>, <variable> ; use any byte variable
for the newer X2 and M2 chip: PEEFSRF <location>, <variable> ; use any byte variable

this puts the entire byte from the PIC SFR register into the specified byte variable

You need to read up on the PEEKSRF to ascertain the correct "location"

Then if it is byte variable b0 you can test with bit variable bit4

If you were to use a byte variable other than those with overlapping bit variables then
for example b5 = b5 AND %0001000 ; only bit 4 holds its value all others will be zero, then test with
IF b5 = 0 THEN
. . . bit4 was a 0
ELSE
. . . bit4 was a 1
ENDIF
 
Last edited:

lbenson

Senior Member
PICAXE Basic commands include setbit, togglebit, clearbit to manipulate any bit in one of the "b" or "w" variables--it would be nice if "getbit" were added (or have I missed that command?).
 

hippy

Technical Support
Staff member
@ lbenson : There's the "IF <var> BIT <num> SET/CLEAR THEN..." command which can be used but no GETBIT as such. I've made a note of that.
 

Technical

Technical Support
Staff member
sorry for posting this if it has been posted before, but I've been struggling with how to read a specific bit of a variable, i.e., x = b0,4 where x is my control, b0 is the byte, and 4 is the bit.

Any simple ideas?
You may have missed this hidden in WA55's answer, but this is much simpler then the other options discussed here:

if bit4 = 1 then...
if bit4 = 0 then...

or

my_var = bit4

to read

Try to use the lower 4 bytes and then each bit has an individual name already i.e. 'bit4' is 'b0,4'
 

jrem

New Member
ok, now that I like. I thought I was going to have to XOR the byte with a mask then do an if then on the result.

just fyi, I'm making a 74HC595 cycle and need three pins. the two are data and clock, third is a latch. Need to cycle the data and clock together. There isn't a routine on the picaxe to make that happen, is there?
 

westaust55

Moderator
Have a look at the SHIFTOUT command in the PICAXE manual 2.
If you have an X1 or X2 part, there is an inbuilt routine in the PICAXE firmware. For other PICAXE parts you need to use the routine given at the bottom of the SHIFTOUT and SHIFTIN sections.


If in future at the start, you advise:
1. The PICAXE chip you are using,
2. post any code you have developed thus far
3. clearly explain what you are trying the do or your question

then folks here are better able to help without guessing and needing to offer alternatives to suit different PICAXE chips (eg X1/X2 versus M2 versus others)
 

westaust55

Moderator
Also hidden, perhaps, in my answer: "b7 = bit4". Sorry if I confused by wrapping this in a way to toggle that value in the simulator.
I guess the problem is that at times those in the know think that they have provided a clear explanation but it is sufficiently obtuse that the newbie does not receive/understand the intended "core" message.
Conversely, some newcomers have difficulty providing sufficient information up fromt to enable others to clearly understand the problem and some others "drip feed" the information like/on a perceived "needs to know" basis.
 
Top