This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Assign name to pin

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

Parents
  • #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_BIT_MASK 0x01
    main()
    {
        BOUT = BOUT | DO_BIT_MASK;
    }
    
    Or, you can define a bit-field structure something like the following (untested).
    main()
    {
        data struct
        {
            unsigned char d0:1;
            unsigned char padding:7;
        } bout _at_ 0xXX;
    
        bout.d0 = 1;
    }
    
    Both the mask and bit-field methods will do a read-update-write. If BOUT is an SFR, you need to be sure that this is OK.

    In general, masks are frequently used in C code. This is partly because they are portable whereas C does not specify the order of fields in a bit-field structure.

    However, bit-field structures are a much more natural and elegant way of doing things. 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.

    I understand that Keil have some long-term plans to address this issue. There is an old thread on this subject here: http://www.keil.com/forum/docs/thread1291.asp

Reply
  • #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_BIT_MASK 0x01
    main()
    {
        BOUT = BOUT | DO_BIT_MASK;
    }
    
    Or, you can define a bit-field structure something like the following (untested).
    main()
    {
        data struct
        {
            unsigned char d0:1;
            unsigned char padding:7;
        } bout _at_ 0xXX;
    
        bout.d0 = 1;
    }
    
    Both the mask and bit-field methods will do a read-update-write. If BOUT is an SFR, you need to be sure that this is OK.

    In general, masks are frequently used in C code. This is partly because they are portable whereas C does not specify the order of fields in a bit-field structure.

    However, bit-field structures are a much more natural and elegant way of doing things. 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.

    I understand that Keil have some long-term plans to address this issue. There is an old thread on this subject here: http://www.keil.com/forum/docs/thread1291.asp

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