Continue to Site

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.

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

C programming: bit vs. U8

Status
Not open for further replies.

CroneA

New Member
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!
 
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

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
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...
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top