• 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.

Accessing bits of variable, XC8

Status
Not open for further replies.

camacho

New Member
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:

Pommie

Well-Known Member
Most Helpful Member
You can use a union.
Code:
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.
 

camacho

New Member
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
 

Pommie

Well-Known Member
Most Helpful Member
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.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
He is correct Mike...

Bits are define like this...
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;
 

TucsonDon

Member
based on the info above I have done the following

Code:
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:

TucsonDon

Member
I made the change and I am still getting "error: (314) ";" expected" for all the bit definitions when I try to build

Code:
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;
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Try this
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..
 

TucsonDon

Member
Now I am getting an error for the "status" declaration.
I have tried
Code:
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:
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:
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:
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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 :-
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:-
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...
 

TucsonDon

Member
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.
 

TucsonDon

Member
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?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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!!
 

TucsonDon

Member
I used your example above and removed the "typedef" so I am unclear as to status already defined.
STATUS is already defined... Change it to...

MYSTATUS mystatus;

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

volatile MYSTATUS mystatus @ 0x30; // or wherever!!
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I used your example above and removed the "typedef" so I am unclear as to status already defined
As in, There is already a STATUS register... Its defined within the header for that chip ( well every chip really )..
 

TucsonDon

Member
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?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top