1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Accessing bits of variable, XC8

Discussion in 'Microcontrollers' started by camacho, Jun 26, 2017.

  1. camacho

    camacho New Member

    Joined:
    Jun 26, 2017
    Messages:
    3
    Likes:
    0
    Hi, lets say i want to access a variable bit by bit, for example, i set individual bits of a variable, and then do bitwise operations with whole variable, lets say shift the whole variable, or assign value of another variable to it etc . I read about accessing it bit-by-bit with structures, but it says nothing about manipulating a whole variable. In AVR basic i simply wrote "variable.x" to access an x bit of a variable. How can i do that in C (XC8) ?Thanks
     
    Last edited: Jun 26, 2017
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    You can use a union.
    Code (text):

    unsigned char temp;

    struct tempbits{
        union{
            temp;
        };
        bit b0;
        bit b1;
        bit b2;
        bit b3;
        bit b4;
        bit b5;
        bit b6;
        bit b7;
    };
     
    Then use tempbits.b4=1; etc to access it.

    Mike.
     
    • Like Like x 1
  3. camacho

    camacho New Member

    Joined:
    Jun 26, 2017
    Messages:
    3
    Likes:
    0
    Thanks for pointing me to the right direction, i used folowing:

    struct bitstructure{
    unsigned int bit0:1;
    unsigned int bit1:1;
    unsigned int bit2:1;
    unsigned int bit3:1;
    unsigned int bit4:1;
    unsigned int bit5:1;
    unsigned int bit6:1;
    unsigned int bit7:1;
    };

    union data{
    unsigned int bytedata;
    struct bitstructure bits;
    }values;

    values.bits.bit1 = 1; //writing to bits
    values.bytedata = 255; //writing to whole variable
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia

    You do realize that an int is 16 bits. Use unsigned char for an 8 bit byte. Also, XC8 supports the bit date type.

    Mike.
     
  6. camacho

    camacho New Member

    Joined:
    Jun 26, 2017
    Messages:
    3
    Likes:
    0
    Well i do, just with bit i get an error. even with your code. "taking sizeof bit is illegal".
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    That's weird as the code above compiles fine for me!

    Mike.
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    He is correct Mike...

    Bits are define like this...
    Code (c):

    typedef union{
      struct{
        unsigned b0:1;
        unsigned b1:1;
        unsigned b2:1;
        unsigned b3:1;
        unsigned b4:1;
        unsigned b5:1;
        unsigned b6:1;
        unsigned b7:1;
       };
    }tempbits;

     
     
  9. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    based on the info above I have done the following

    Code (text):

    bool a;
    bool b;
    bool c;
    bool d;
    bool e;
    bool f;
    bool g;
    bool h;

    typedef union
    {
    struct
    {
    a b0;
    b b1;
    c b2;
    d b3;
    e b4;
    f b5;
    g b6;
    h b7;
    };
    }status;

     
    what I am trying to do is change/check individual bits to monitor various operational status i.e. if a=true do..... or if operation is done d=false.

    but when I compile I get errors on the bit definitions. a b0, b1.....

    I am also declaring this in a header file if that makes a diff.
     
    Last edited: Jul 23, 2017
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Look at my example... b0~ b7 are defined with the colon : The bool keyword is defined as a unsigned byte...
     
  11. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I made the change and I am still getting "error: (314) ";" expected" for all the bit definitions when I try to build

    Code (text):

    typedef union
    {
    struct
    {
    a b0:1;
    b b1:1;
    c b2:1;
    d b3:1;
    e b4:1;
    f b5:1;
    g b6:1;
    h b7:1;
    };
    }status;
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Try this
    Code (c):

    typedef unsigned char bool;

    typedef union
      {
      struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }status;
     
    Because a~ h are already declared variables you cannot use them in that way..
     
  13. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Now I am getting an error for the "status" declaration.
    I have tried
    Code (text):

    uint8_t status;
    typedef unsigned char bool;

    typedef union
      {
      struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }status;
     
    I have tried
    Code (text):

    typedef unsigned char bool;

    typedef union
      {
    uint8_t status;
       struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }status;
     
    And
    Code (text):

    typedef unsigned char bool;

    typedef union
      {
      struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }uint8_t status;
     
    and I get a error for status.a~g:
    error: (193) not a variable identifier "status"
    error: (196) struct/union required

    in my source code I have:
    Code (text):

    if (status.a != true)status.a = true;
     
    but when I build I get the above errors any place that I use the bitwise "status.a~h"
     
    Last edited: Jul 23, 2017
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,794
    Likes:
    134
    Location:
    morristown,tn
    Id like too see this been playing a bit with xc8
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    You are using the type and not the variable...

    Let me explain

    typedef makes a new type!! It doesn't make a variable..

    look here...

    unsigned char avariable; // this makes a memory location called avariable... avariable = 0~255!!
    typedef unsinged char avariabletype; //this makes a copy of the type.. avariabletype = unsigned char!!

    so you use it like this :-
    Code (c):

    typedef unsigned char bool;

    typedef union
      {
      struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }STATUS;

    STATUS status;

     
    STATUS is the new type and status is the variable... You could just get rid of the typedef and use it how you think it should go:-
    Code (c):

    typedef unsigned char bool;

    union
      {
      struct
         {
         bool a:1;
         bool b:1;
         bool c:1;
         bool d:1;
         bool e:1;
         bool f:1;
         bool g:1;
         bool h:1;
         };
      }status;
     
     
    Now it is a variable...
     
  16. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Ian Rogers thanks for the explanation. This is my first attempt at this type of operation. I think removing the "typedef" will serve my purpose.
     
  17. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I ran into another issue, when I run the simulator (MPLab) and manipulate the bits the if (if (status.a == true) call function) statement doesn’t pick up the change. I setup a watch on “status” which shows the memory address but when I look @ the file register that address shows an I2C que function.

    I guess my next question is can I assign the variable “status” to a specific address and if so how do I do that?
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Whoops..... You have used my example to the letter!!!!

    STATUS is already defined... Change it to...

    MYSTATUS mystatus;

    Anywho!!

    You can tell the compiler to use a bank variable by:-

    volatile MYSTATUS mystatus @ 0x30; // or wherever!!
     
  19. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I used your example above and removed the "typedef" so I am unclear as to status already defined.
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    As in, There is already a STATUS register... Its defined within the header for that chip ( well every chip really )..
     
  21. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I was using "status" as an example. I have defined it as timestatus in my code. If I am understanding you correctly I should use your first example and then declare it as volatile?
     

Share This Page