Searching \ for 'reading indivivual bits in a byte var' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=reading+indivivual
Search entire site for: 'reading indivivual bits in a byte var'.

Truncated match.
PICList Thread
'reading indivivual bits in a byte var'
1999\11\11@190526 by erto Fonseca Iannini

flavicon
face
I have a byte that came from serial port and I need to make decisions
based in individual bits of it and then take several attitudes like set
a led, etc...
Example: A byte called CONTROL is formed by: 0b11001101
The bit0 defines if an led must be set or turned off;
Bit1 and bit2 defines if an motor will run or not;
Bit4 defines clockwise or counterclockwise;
and so on...
The whole code is working, but I need some tips about how to read a
single bit inside a byte variable (in c language).
I was thinking in "is.flag"... but I didn't know how to implement it.
All ideas are welcome.

See you,

Roberto.

1999\11\11@192641 by Dave Johnson

flavicon
face
Roberto Fonseca Iannini wrote:

>The whole code is working, but I need some tips about how to read a
>single bit inside a byte variable (in c language).
I've never written C for micros like the PIC (I use asm for that) so
there may be special constructions for that sort of thing. But in
"vanilla" C for a desktop machine I'd just do a  bitwise AND with an
appropriate mask, so for instance to see if bit 0 is set:

if( byte & 0x01 )
    // bit 0 is set
else
    // bit 0 is not set

and so on, using 0x02 for bit 1, 0x04 for bit 2, etc. There are probably
spiffier ways to do it, but this an obvious one.

Dave Johnson

1999\11\12@025243 by Michael Rigby-Jones

flavicon
face
part 0 5629 bytes
<P><FONT SIZE=2 FACE="Arial">I have a byte that came from serial port and I need to make decisions</FONT>
<BR><FONT SIZE=2 FACE="Arial">based in individual bits of it and then take several attitudes like set</FONT>
<BR><FONT SIZE=2 FACE="Arial">a led, etc...</FONT>
<BR><FONT SIZE=2 FACE="Arial">Example: A byte called CONTROL is formed by: 0b11001101</FONT>
<BR><FONT SIZE=2 FACE="Arial">The bit0 defines if an led must be set or turned off;</FONT>
<BR><FONT SIZE=2 FACE="Arial">Bit1 and bit2 defines if an motor will run or not;</FONT>
<BR><FONT SIZE=2 FACE="Arial">Bit4 defines clockwise or counterclockwise;</FONT>
<BR><FONT SIZE=2 FACE="Arial">and so on...</FONT>
<BR><FONT SIZE=2 FACE="Arial">The whole code is working, but I need some tips about how to read a</FONT>
<BR><FONT SIZE=2 FACE="Arial">single bit inside a byte variable (in c language).</FONT>
<BR><FONT SIZE=2 FACE="Arial">I was thinking in &quot;is.flag&quot;... but I didn't know how to implement it.</FONT>
<BR><FONT SIZE=2 FACE="Arial">All ideas are welcome.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">See you,</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Roberto</FONT>
</P>
</UL>
<P><FONT COLOR="#0000FF" SIZE=2 FACE="Arial">You can use a union to map bit variables onto a byte variable.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">union bitvars </FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned char byte;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">struct </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit0:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit1:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit2:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit3:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit4:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit5:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit6:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">unsigned&nbsp;&nbsp;&nbsp;&nbsp; bit7:1;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">} bits;</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">union bitvars control;</FONT>
<BR><FONT SIZE=2 FACE="Arial">control.byte = 0xAA;&nbsp;&nbsp;&nbsp; /* use the variable as an unsigned char */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">if(control.bits.bit0 == 0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* or access the bits individually */</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
<BR><FONT SIZE=2 FACE="Arial">etc...etc...</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Regards</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Mike Rigby-Jones</FONT>
<UL>
<P><FONT SIZE=2 FACE="Arial">.</FONT>
</P>
</UL>
</BODY>
</HTML>
</x-html>

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...