Keil Logo Arm Logo

Discussion Forum

Naming portbits globally

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Glenn M
Posted
12-Sep-2011 09:08 GMT
Toolset
C51
New! Naming portbits globally

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

Read-Only
Author
Glenn M
Posted
12-Sep-2011 09:30 GMT
Toolset
C51
New! RE: Naming portbits globally

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;
}
Read-Only
Author
Andrew Neil
Posted
12-Sep-2011 09:51 GMT
Toolset
C51
New! RE: To make portable code

Isn't referring to specific port bits inherently non-portable?

Read-Only
Author
Jon Ward
Posted
12-Sep-2011 16:02 GMT
Toolset
C51
New! RE: Naming portbits globally
foo.h:
extern bit bit1;
extern bit bit2;
extern bit bit2;

There is no extern for bit3.

Jon

Read-Only
Author
Glenn M
Posted
13-Sep-2011 07:48 GMT
Toolset
C51
New! RE: Naming portbits globally

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;
}
Read-Only
Author
Andy Neil
Posted
13-Sep-2011 09:28 GMT
Toolset
C51
New! RE: I find the solution to be inconsistent

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

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.