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

How to access bit addressable?

Here is one to help a mechanical engineering student trying to program a microcontroller.

P5 is a bit addressable register. In the header reg517.h is only the definiton of P5 as a sfr. How do I access a bit address of such a port in C? Do I have to define sbit P5_1=F8H?

By the way, Is there an easy way to write the assembly instruction CPL in C51 langauge?

Thanks a lot

  • sbit can use either the bit address, as you show, or the bit number; eg,

    sbit mybit0  = ibase ^ 0;  /* bit 0  of ibase */
    sbit mybit15 = ibase ^ 15; /* bit 15 of ibase */
    sbit Ary07 = bary[0] ^ 7;  /* bit 7 of bary[0] */
    sbit Ary37 = bary[3] ^ 7;  /* bit 7 of bary[3] */
    See the "Bit-addressable Objects" section in the manual

    By the way, Is there an easy way to write the assembly instruction CPL in C51 langauge?

    It's not usually appropriate to just insert odd assembler instructions into 'C' source code.
    To have a section of assembler in a 'C' source file, look up the SRC and ASM/ENDASM directives

  • There are a couple of slightly different ways of defining a bit variable with a fixed bit address. For example:

    sbit led_state = 0xF8;    // 0 = LED off 1 = LED on
    
    or
    sbit led_state = P5^1;    // 0 = LED off 1 = LED on
    
    The latter requires that P5 has already been defined as an SFR at a specified address.

    To complement the bits of a variable (or an SFR) use the ones-complement operator. For example:

    P5 = ~P5;