__sbit usage for 8051

Help
zramaro
2011-08-07
2013-03-12
  • zramaro
    zramaro
    2011-08-07

    Hello,

    __sbit is heavily used in the mcs51 include files of sdcc.
    My understanding is that __sbit selects a specific bit from a register. For example,  __sbit at (0x83) P0_3 means P0_3 is bit 3 of register 0x80.

    With the following C source file:

    #include <8051.h>
    main()
    {
      P0_3 = 1;  // set bit 3 of P0 to 1
      P0_5 = 0;  // set bit 5 of P0 to 0
      return 0;
    }
    

    the generated asm file contains:

    ...
    _P0_3=0x0083
    _P0_5=0x0085
    ...
    setb _P0_3
    clr  _P0_5
    ...
    

    I don't think that the generated asm instructions do the expected operation. I may probably miss something obvious.

    Thanks.
    Zafi.

     
  • David Cary
    David Cary
    2011-11-04

    Zafi,
    I suspect you are familiar with some other CPU instruction set, such as a PIC CPU, one where "setbit" takes 2 parameters: a byte address, and a bit indicator.

    Many 8051 peripherals have a "bit addressable memory", where "setbit" only takes 1 parameter: what looks like a byte address, but the hardware connects that address to only one bit.
    Confusing, but it seems to work.

    See, for example,
    Maxim APPLICATION NOTE 3477: "Using the Free SDCC C Compiler to Develop Firmware for the DS89C430/450 Family of Microcontrollers"
    http://www.maxim-ic.com/app-notes/index.mvp/id/3477