1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

C programming: bit vs. U8

Discussion in '8051/8951' started by CroneA, Oct 28, 2013.

  1. CroneA

    CroneA New Member

    Joined:
    Oct 23, 2013
    Messages:
    19
    Likes:
    0
    Hello everyone! I have a programming question regarding the use of C for a 8051 microcontroller.

    I have been going over some Silicon Labs premade examples, it seems that for even a simple one bit variable they will declare it as a U8. Would there be a good reason why they aren't using the BIT data type?

    Thanks!
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Normally a structure would be created with bit patterns so you create a bit variable.

    Look at this example... It comes from XC compiler on the PIC but it is ANSI C so will work on any compiler

    Code (c):

    // Register: INTCON
    extern volatile unsigned char           INTCON              @ 0x00B;
    #ifndef _LIB_BUILD
    asm("INTCON equ 0Bh");
    #endif
    // bitfield definitions
    typedef union {
        struct {
            unsigned IOCIF                  :1;
            unsigned INTF                   :1;
            unsigned TMR0IF                 :1;
            unsigned IOCIE                  :1;
            unsigned INTE                   :1;
            unsigned TMR0IE                 :1;
            unsigned PEIE                   :1;
            unsigned GIE                    :1;
        };
        struct {
            unsigned                        :2;
            unsigned T0IF                   :1;
            unsigned                        :2;
            unsigned T0IE                   :1;
        };
    } INTCONbits_t;
     
    Now you can access any of the bits within the declare SFR INTCON.

    SDCC has its own way
    Code (c):

    __sbit __at 0xA0 pwm;
    __sbit __at 0xA1 pwmn;
    __sbit __at 0xA2 enable;
     
    Remember that there are only a few DATA locations that accept bit access...
     
    • Like Like x 1
  3. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Generally c does not have a native bit type. Many programmer will use a byte aka U8 type which wastes 7 bits but makes accessing the variable faster then using just a bit and having to access a structure to get to it.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. CroneA

    CroneA New Member

    Joined:
    Oct 23, 2013
    Messages:
    19
    Likes:
    0

    Thank you both for your responses! I will take note of this in the future, very useful!
     

Share This Page