This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to read a Bit from a Byte ?

Respectable colleagues,

how to read a value of a BIT from value of a BYTE variable in Keil C ?

For example:
X=(Byte1.Bit7*4)+(Byte1.Bit6*2)

Thanks in advance.

With best regards,

Dragan Kujovic

Parents
  • > unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

    > bit_n = ( byte & mask_table[ n ] ) != 0x00;

    Thanks, that's it.

    > Keil C51 has its own special extensions to take advantage of the C51 chip's bit addressing instructions.

    I don't know why Keil didn't make possible accessing individual pins on C16x, but I have made Reg167.h, Reg168.h, Reg169.h and RegST10F269.h files of my own with all necessary declarations. So, now is possible to access individual pins from C code with simple commands like:

    P2_0 = 1; // Port 2 Pin 0 HIGH

    or

    P2_0 = 0; // Port 2 Pin 0 LOW

    I will send those files to them, and if they find this useful, they can include those files in the software package, or put on the Web site.

    With best regards,

    Dragan Kujovic

Reply
  • > unsigned char mask_table[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

    > bit_n = ( byte & mask_table[ n ] ) != 0x00;

    Thanks, that's it.

    > Keil C51 has its own special extensions to take advantage of the C51 chip's bit addressing instructions.

    I don't know why Keil didn't make possible accessing individual pins on C16x, but I have made Reg167.h, Reg168.h, Reg169.h and RegST10F269.h files of my own with all necessary declarations. So, now is possible to access individual pins from C code with simple commands like:

    P2_0 = 1; // Port 2 Pin 0 HIGH

    or

    P2_0 = 0; // Port 2 Pin 0 LOW

    I will send those files to them, and if they find this useful, they can include those files in the software package, or put on the Web site.

    With best regards,

    Dragan Kujovic

Children
  • // main.h

    #define OUTPUT 1
    #define INPUT 0
    #define HIGH 1
    #define LOW 0

    sfrbit P_TEST _atbit(P2,10);
    sfrbit DP_TEST _atbit(DP2,10);

    //main.c

    DP_TEST = OUTPUT;
    P_TEST = HIGH;
    _nop();
    P_TEST = LOW;

    DP_TEST = INPUT;
    if (P_TEST == HIGH)
    {
    }


    Does this workout for you ?

    Best Regards,
    Bert

  • Don't forget those &ltpre&gt and &lt/pre&gt tags when posting code:

    // main.h

    #define OUTPUT 1
    #define INPUT  0
    
    #define HIGH 1
    #define LOW  0
    
    sfrbit P_TEST  _atbit(P2,10);
    sfrbit DP_TEST _atbit(DP2,10);
    
    //main.c
    void main( void )
    {
       DP_TEST = OUTPUT;
       P_TEST  = HIGH;
       _nop();
       P_TEST  = LOW;
    
       DP_TEST = INPUT;
       if (P_TEST == HIGH) 
       {
          /* do stuff */
       }
    }