Reputation: 490
The idea is to read any bit from a port. Anyway accessing to one known bit is simple, like
P0_0 <-- gets bit 0 from port 0
But if i need to access bit y via function?
read_bit(__bit y){
return P0_y; // <-- just an idea but its not right becouse of syntax.
}
using SDCC to program and 8051 header.
Upvotes: 0
Views: 1966
Reputation: 735
just see this function
char chek_bit_p0(unsigned char chk_bit){
if((P0>>chk_bit) & 1)
return 1;
else
return 0;
}
or simply by a macro like below (preferred way)
#define chek_bit_p0(x) (((P0>>x)&1)&&1)
Upvotes: 0
Reputation: 62048
If it's a literal constant, you can use a macro trick:
#define READ_P0_BIT(BIT) (P0_ ## BIT)
unsigned x = READ_P0_BIT(1);
If it's not a literal constant, you can do this:
int readP0bit(int bitNo)
{
switch (bitNo)
{
case 0: return P0_0;
case 1: return P0_1;
// ...
case 7: return P0_7;
default: return 0;
}
}
Upvotes: 1
Reputation: 409166
You can make a local array-variable that contains the bits in the function, and use the "bit" as an index into this array.
Something like:
__bit read_bit(const int b)
{
__bit all_bits[8] = {
P0_0,
P0_1,
/* etc. */
P0_7
};
return (b < 8 ? all_bits[b] : 0);
}
Upvotes: 0