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 all, I searched the forum before starting this thread, but I couldn't find a satifying answer to my question. here's my question: I'm using keil uVision2 and the EZ-USB FX dev kit. This kit has 6 IO ports on it. I want to assign a name to each pin of the IO ports. This is what I tried so far: ... #define D0 OUTB.0 main() { D0 = 1; } ... I get 2 errors: error C141: syntax error near '.0' error C213: left side of asn-op not an lvalue As far as I remember I used this code in a previous project, and it worked. But now it doesn't :( anyone who knows what goes wrong? kind regards, Chris
#define D0 OUTB.0 main() { D0 = 1; } I get 2 errors: error C141: syntax error near '.0' error C213: left side of asn-op not an lvalue As far as I remember I used this code in a previous project, and it worked. But now it doesn't :( I don't think your code could ever have worked in C, maybe you were thinking of assembler. In C "." is the member operator and OUTB is not a structure. If OUTB is bit addressable then you can define an sbit address for bit zero. The next most obvious method of setting or clearing an individual bit is to use masks as follows:
#define D0 OUTB.0 main() { D0 = 1; }
#define D0_BIT_MASK 0x01 main() { BOUT = BOUT | DO_BIT_MASK; }
main() { data struct { unsigned char d0:1; unsigned char padding:7; } bout _at_ 0xXX; bout.d0 = 1; }
It is pity that C51 does not handle bit-fields efficiently and did not make this the normal way of defining an SFR that has bit-fields (as opposed to the totally non-C sbit extensions. Back in the heyday of the 8051 (which was the mid 80's according to Intel) the Intel ASM-51 Assembler and PL/M-51 Compiler were the standard development tools. When Keil started making C compilers for the 8051 a decisiiiion had to be made as to how SFRs and bits and that kind of stuff were to be handled. We decided to use the same nomenclature as PL/M-51 (which is similar to pascal). That's where sfr and sbit came from. At the time, these were the "standard" way of doing things. Jon
It is pity that C51 does not handle bit-fields efficiently Few compilers handle C bitfields efficiently. Given my bad experiences with that, plus the fact that bitfields are under-specified* and thus not very portable, I've learned to avoid their use entirely and just use bitwise operators instead. The Keil sbit mechanism is one of those low-level 8051 architecture adaptations. Since only the occasional SFR and a few bytes of memory are bit-addressable, it really wouldn't do as a mechanism for general bitfields. (* Common snags: The order of bitfields in a word is not standardized, and varies from compiler to compiler. Despite the fact that bitfields are specifically "int", whether or not a value with the high bit set is negative is implementation-dependent, so it's risky to use some tests on a bitfield or pass them as parameters. Standard C insists that bitfields are always an int wide, even if you declare only one bit, though most decent embedded compiler lets you declare them in ints of various sizes as an extension.)