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 use extern sbit declare?

I declare a variable unsigned char bdata Kde in a.c.

[in file a.c]

unsigned char bdata Kde;

Than I want to use the variable inb.c.
[in file b.c]
#include <stdio.h>
.....
extern unsigned char bdata Kde;
sbit testbit=Kde^1;
void main(void)
{......}

:error C141:.......a.c:syntax error near 'sbit'

why?

Parents
  • Sfr and sbit's are special. They refer to hardware registers which are by definition global. Thus, Keil (at least if my memory serves me correctly) allows one to define the same named object in a header file and pretend that it is auto-externed/defined as needed. This is the only way files like reg552.h etc. could work. E.g.

    /* Foo.h */
    sfr r_port0 = 0x80;
    Now you can include foo.h in any C file you wish and use the name r_port0 to access the Register for Port 0. No extern is needed or allowed. As Graham Cole pointed out bdata vars. can be externed but they don't refer to hardware register bits. He is wrong about the sbit extern I think.

    However, your error is not related to extern anyhow. You have a simple syntax error. When you define an sbit you must provide the 8051's hardware address for the sbit var. You attempt to define testbit at the same time you initialize it, you cannot. Sbit's refer to hardware is that really what testbit is supposed to be?

Reply
  • Sfr and sbit's are special. They refer to hardware registers which are by definition global. Thus, Keil (at least if my memory serves me correctly) allows one to define the same named object in a header file and pretend that it is auto-externed/defined as needed. This is the only way files like reg552.h etc. could work. E.g.

    /* Foo.h */
    sfr r_port0 = 0x80;
    Now you can include foo.h in any C file you wish and use the name r_port0 to access the Register for Port 0. No extern is needed or allowed. As Graham Cole pointed out bdata vars. can be externed but they don't refer to hardware register bits. He is wrong about the sbit extern I think.

    However, your error is not related to extern anyhow. You have a simple syntax error. When you define an sbit you must provide the 8051's hardware address for the sbit var. You attempt to define testbit at the same time you initialize it, you cannot. Sbit's refer to hardware is that really what testbit is supposed to be?

Children