I want to give a particular portbit a name, that I can use across multiple sourcefiles. To make portable code, I prefer to avoiding physical addresses. The only a approach I have been able to think of, is this:
foo.c: #include <C8051F120.h> // register definition file #include <foo.h> bdata unsigned char test; sbit bit1 = test^3; sbit bit2 = 0xB3; // P3 has address 0xB0, ie. it is bitaddressable sbit bit3 = P3^3; foo.h: extern bit bit1; extern bit bit2; extern bit bit2; bar.c #include <C8051F120.h> // register definition file #include <foo.h> void myfunc(void) { bit1 = 1; bit2 = 1; bit3 = 1; }
It works fine for bit1 and bit2, but bit3 results in a linker warning “Reference made to unresolved external”. As sfr addresses, ending with 0 or 8 are byte- and bit addressable, I wonder why Keil complains about this, when the sbit in test can be handled.
Any alternative solutions?
Thanks, Glenn
I just realized that bit2 definition was wrongly placed. It should have been:
foo.c: #include <C8051F120.h> // register definition file #include <foo.h> bdata unsigned char test; sbit bit1 = test^3; sbit bit3 = P3^3; foo.h: extern bit bit1; extern bit bit2; sbit bit2 = 0xB3; // P3 has address 0xB0, ie. it is bitaddressable bar.c #include <C8051F120.h> // register definition file #include <foo.h> void myfunc(void) { bit1 = 1; bit2 = 1; bit3 = 1; }
Isn't referring to specific port bits inherently non-portable?
foo.h: extern bit bit1; extern bit bit2; extern bit bit2;
There is no extern for bit3.
Jon
I just figured it out! However, I find the solution to be inconsistent, as sbit must be in the c-file, when I am referencing a bit in a user defined bit space variable, while it must be located in the header file, when I am naming a port-bit. Why can't I just define it with sbit in the c-file, and declare it with extern bit in the header file?
Thanks /Glenn
foo.c: #include <C8051F120.h> // register definition file #include <foo.h> bdata unsigned char test; sbit bit1 = test^3; foo.h: extern bit bit1; sbit bit2 = P3^4; bar.c #include <C8051F120.h> // register definition file #include <foo.h> void myfunc(void) { bit1 = 1; bit2 = 1; }
I think that's because Keil's implementation is "inconsistent" here:
See: http://www.keil.com/forum/19515/ - and follow the links!
See also: http://www.keil.com/support/docs/1175.htm