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

bdata across multiple files?

I have data that I would like to efficiently address as both a byte and as 8 bits.
The bdata type sounds like the way to go, but I can not make it work across multiple files.
This program fails to compile and link. Any ideas?

// main.h
extern char bdata Main_Bits;
sfr Main_Bit0 = Main_Bits ^ 0;

//main.c 
#include "main.h"
char bdata Main_Bits;

void FnA( void );  
void main( void )
{
  Main_Bits = 0;
  FnA();  
}

//FnA.c 
#include "main.h"
void FnA( void )
{
  Main_Bit0 = 1;
}

  • sfr Main_Bit0 = Main_Bits ^ 0;

    Try sbit instead of sfr.

  • As Alex has already said, you need sbit, not sfr

    and then:

    // main.h
    extern char bdata Main_Bits;
    extern bit        Main_Bit0;

    //main.c 
    #include "main.h"
    
    char bdata Main_Bits;
    sbit       Main_Bit0 = Main_Bits ^ 0;

    In the header file, you just declare that Main_Bit0 is a bit which is defined elsewhere;
    In main.c, you actually define the actual storage location of the bit.

    Note also that bits always have to be at "global" scope - you can't have local bit variables within functions.

    This is not well documented in the Keil manuals - it took me a while to get to grips with it!

    As mentioned elsewhere, it doesn't hurt to #include "main.h" in main.c, and it does give you the advantage that the compiler will complain if your declarations & definitions get out of step!








  • Sorry, I really did mean to type sbit.
    Anyways, using sbit, I get the following errors.

    Build target 'Target1'
    compiling Main.C...
    MAIN.H(3): error C142: 'Main_Bits': invalid base address
    compiling FnA.c...
    MAIN.H(3): error C142: 'Main_Bits': invalid base address
    .\FNA.C(5): error C202: 'Main_Bit0': undefined identifier
    Target not created
    
    The code files are:
    // main.h
    extern char bdata Main_Bits;
    sbit Main_Bit0 = Main_Bits ^ 0;
    
    //main.c 
    #include "main.h"
    char bdata Main_Bits;
    
    void FnA( void );  
    void main( void )
    {
      Main_Bits = 0;
      FnA();  
    }
    
    //FnA.c 
    #include "main.h"
    void FnA( void )
    {
      Main_Bit0 = 1;
    }
    


  • See my earlier post: you need just an "extern bit" (not sbit) declaration in the header file, and the sbit definition in the main.c file

  • Thank you very much. I would never have thought to use an "extern bit" for a "sbit declaration".

  • You're not alone!
    I had the same trouble, and everyone else at this Client as well!

    Like I said, this is poorly documented in the Keil manuals.

  • Refer to page 328 in the C51 users guide 01.97. The description shows how to deal with bits in bdata memory.

    HHK

  • Yes, that's what I meant by it being poorly documented (it didn't say it wasn't documented!)

    <RANT>
    Why is this information hidden away in an appendix right at the back of the manual instead of being clearly stated in the main body?

    This is one of Keil's extensions to ANSI 'C' - you won't find it in any "general" 'C' textbook, so if you can't get a clear explanation from the manual, where on earth is it going to come from!?

    IMO, The fact that they have felt the need to include this section just goes to prove that the body of the manual is inadequate!
    </RANT>

    Fortunately, we have this discussion forum!