0

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.

RassK
  • 515
  • 6
  • 20

3 Answers3

1

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;
    }
}
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

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);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Most likely, `P0_something` is a hard-wired identifier of a special type and a hard-coded location, that the compiler knows how to access. Your code most likely won't compile, but if it does, it will read all bits of the hardware port, which generally is not a good thing as there may be side effects from reading hardware ports. You usually can't make pointers to such port bits either. – Alexey Frunze Oct 04 '12 at 08:27
  • @AlexeyFrunze Yeah you're right. That's why I voted on your answer. :) – Some programmer dude Oct 04 '12 at 08:50
0

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)
rajesh6115
  • 705
  • 9
  • 21