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