Hi, I want to know the address of a bit variable. For example:
bit gBit; func() { printf("%d", &gBit); }
This is what I am trying to do
foo() { bit Finish = FALSE; //pass the bit address to interrupt service routine(ISR) //the ISR routine will set this bit to TRUE when some event occurs Reg2ISR(&Finish); while( !Finish ) //waiting for the ISR routine to set this bit to true ; }
As long as I can get this address, I can assign value to the bit of that address. No, you can't. More to the point, the 8051 can't do that. As a previous reply already pointed out to you: the '51 doesn't have the concept of a pointer to a bit variable. Or, to put in terms of assembly code: there is no indirect addressing mode for single bits, only direct addressing. A bit variable simply can't be used the way you want it to. Use a byte instead.
Of course it can be done in 8051 otherwise how the compiler initialize all the global bit variables before the main function is called? When your program is compiled and linked, Keil C will build a data structure called C_INITSEG. This data structure stores all the global variables that need to be initialized before the main function is called. So it also records all the addresses of bit variables that need to initialized before the main function is called. By using this address of the bit variable, it can give the bit variable initial value.
If you're so sure of your understanding, why don't you just go ahead an look into C_INITSEG to check what the compiler actually does? Or tell me the opcode of the machine instruction accessing a bit indirectly?
If I read this correctly, the OP is of the school where it is taught "If you use a global address, you will die" Access to a bit in main and an interrupt can be accomplished simply by making the bit declaration global. Erik
So it also records all the addresses of bit variables that need to initialized before the main function is called. By using this address of the bit variable, it can give the bit variable initial value. The initialization code converts the bit address into a byte.bit address and then uses and and or to mask and set the bit accordingly. There is no instruction on the 8051 that supports indirect access to bits. It has to be done manually. Jon
The initialization code converts the bit address into a byte.bit address and then uses and and or to mask and set the bit accordingly Jon, you scared me there. I had a look and GBvfdBusy = TRUE; does, indeed, compile to SETB GBvfdBusy wiping sweat off forhead Erik
I'm talking about global bit variable initialization. We would never do something like this for local bit initialization (which use the SETB and CLR instructions). Jon
my example above IS a global bit.
Just got it two statements in one heap the bit address is converted into a byte.bit address before it is used The initialization code ... uses and and or to mask and set the bit accordingly