We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I found my bug, but I want to understand why the compiler did that! I use a fonction to clear bits: CLRBIT(BYTE, MASK); The following C code: CLRBIT(aBMCURegisters[REGADDRESS], I2C_SELECT_BIT | DDC_BYPASSN_BIT); Compile this ASM code: ANL aBMCURegisters+01H,#07FH Which is wrong because, the definition of bits are: #define DDC_BYPASSN_BIT bmBIT2 #define I2C_SELECT_BIT bmBIT7 It's like the compiler only check the first bit of the second argument! To compile a right code, I need to put the second argument between (), like this: CLRBIT(aBMCURegisters[REGADDRESS], (I2C_SELECT_BIT | DDC_BYPASSN_BIT)); Compile this ASM code: ANL aBMCURegisters+01H,#07BH Please note that the C code use only one line... (no return between args). Do you know why? Thanks, Denis.
You don't give us the definition of CLRBIT, but most likely you have a precedence problem in the macro definition. The fact that an extra level of parens fixes the problem makes me think this is likely. As a general rule, macros should always parenthesize their arguments in case an expression is passed in. For example, rather than #define min(a,b) (a < b) ? a : b write #define min(a,b) (((a) < (b)) ? (a) : (b))
You are right! Haaaa! Why I didn't see this... My definition was: #define CLRBIT(THE_BYTE, BITMSK) THE_BYTE &= ~BITMSK I changed for: #define CLRBIT(THE_BYTE, BITMSK) THE_BYTE &= ~(BITMSK) Thanks, Denis.
That's still not careful enough. The two most important rules for writing function-like macros are: *) put a pair of parentheses around every use of any of the arguments. *) put another pair of parentheses around the whole macro body This a basic FAQ-level C knowledge. You should be beyond that if you're going to write embedded C.
I agree with you, except that I wrote some code in the past with some micro devices and I never used macros... It's not a necessary knowledge when the compiler support inline function. I beleive that C51 should also support it! Do you?
when the compiler support inline function. I beleive that C51 should also support it! it does. That it does it in a crummy way (no debug of C source etc) is another story. Erik
"I beleive that C51 should also support it!" It's an ANSI compiler. Where does ANSI say anything about inline functions?