-----Original Message-----
From: Roberto Fonseca Iannini [SMTP:roberto1@LINKEXPRESS.COM.BR]
Sent: Friday, November 12, 1999 12:06 AM
To: PICLIST@MITVMA.MIT.EDU
Subject: reading indivivual bits in a byte var
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
You can use a union to map bit variables onto a byte variable.
union bitvars
{
unsigned char byte;
struct
{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
} bits;
}
union bitvars control;
control.byte = 0xAA; /* use the variable as an unsigned char */
if(control.bits.bit0 == 0) /* or access the bits individually */
{
}
etc...etc...
Regards
Mike Rigby-Jones
.