View previous topic :: View next topic |
Author |
Message |
future
Joined: 14 May 2004 Posts: 330
|
byte, #define, #bit |
Posted: Fri May 14, 2004 4:57 pm |
|
|
I'm trying to to this:
byte rtvar[32];
#define testvar rtvar[20]
#bit name0 = testvar.0
#bit name1 = testvar.1
#bit name2 = testvar.2
#bit name3 = testvar.3
I'm getting an error in #bit expression telling me "expecting a ." in the dot in testvar.0
Is possible what I'm trying to do? |
|
|
Guest
|
|
Posted: Fri May 14, 2004 10:13 pm |
|
|
This doesn't LOOK legal to me, because I THINK that macros like #bit require a known address, while the 20th member of an array that could be moved around by the optimizer doesn't appear to be fixed. (I haven't seen what's in the #bit macro.)
I suspect there are other, simpler ways to do what you want to do.
I've had trouble with the CCS compiler generating bad code when I wander off the straight & narrow. Unless you're good with assembly (which I am NOT) then may I suggest to try to write C which is simple, vanilla stuff?
Robert |
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 15, 2004 4:51 am |
|
|
Since I'm new to C, and coming from picbasic which is possible to do that, I'm totally lost.
It would be nice if someone show me a way to have an array (used to send 32 values via serial port), having aliases to them (to make programming easier) and having aliases to some bits in the array (again to make programming easier). |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sat May 15, 2004 5:26 am |
|
|
One way of doing it is to place the array in the memory manually:
Code: |
byte rtvar[32];
#locate rtvar = 0x20
byte testvar;
#locate testvar = 0x34 //Now testvar=rtvar[20]
#bit name0 = testvar.0
#bit name1 = testvar.1
#bit name2 = testvar.2
#bit name3 = testvar.3
|
|
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 15, 2004 6:40 am |
|
|
Any other way?
I ask again because in the complete code there are 160+ variables like this and it will be a lot of work. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sat May 15, 2004 7:42 am |
|
|
Ok, how about:
Code: |
union Var
{
byte Byte;
struct Bits
{
int1 bit0;
int1 bit1;
int1 bit2;
int1 bit3;
int1 bit4;
int1 bit5;
int1 bit6;
int1 bit7;
} bits;
};
...
union Var rtvar[32];
rtvar[20].Byte=xx; //To access as a byte
rtvar[20].bits.bit0=x; //To access bit 0
|
|
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 15, 2004 7:49 am |
|
|
I like the last way, but how about having aliases to every byte in rtvar[32] and two of the aliases having bit access? |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sat May 15, 2004 7:58 am |
|
|
You mean something like this?
Code: |
struct Bits
{
int1 bit0;
int1 bit1;
int1 bit2;
int1 bit3;
int1 bit4;
int1 bit5;
int1 bit6;
int1 bit7;
};
...
byte rtvar[32];
struct Bits *testvar;
testvar=(Bits *)(rtvar+20);
testvar.bit0=x;
|
|
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 15, 2004 8:31 am |
|
|
It seems to be what I am looking for. C is more flexible than me .
Can you tell me what does '(Bits *)' do?
Thank you. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sat May 15, 2004 9:03 am |
|
|
Well testvar is a pointer to the Bits structure, but (rtvar+20) is an address (a pointer to) a byte. For the pointer assignment to work properly you have to convert the byte pointer to a pointer to Bits, and that is what the (Bits *) casting does.
By the way, I made a small mistake in my previous post. The last line of the code should be:
|
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 15, 2004 9:27 am |
|
|
Thinking a tittle more and looking at my first post...
byte rtvar[32];
#define testvar rtvar[20]
#bit name0 = testvar.0
#bit name1 = testvar.1
#bit name2 = testvar.2
#bit name3 = testvar.3
#define testvar1 rtvar[21]
#bit name4 = testvar1.0
#bit name5 = testvar1.1
#bit name6 = testvar1.2
#bit name7 = testvar1.3
struct Bits
{
int1 bit0;
int1 bit1;
int1 bit2;
int1 bit3;
int1 bit4;
int1 bit5;
int1 bit6;
int1 bit7;
};
...
byte rtvar[32];
struct Bits *testvar;
struct Bits *testvar1;
testvar=(Bits *)(rtvar+20);
testvar1=(Bits *)(rtvar+21);
testvar->bit0=x;
testvar1->bit0=x; |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Mon May 17, 2004 8:06 am |
|
|
This is how I work on this. Using intermediate variables keeps the compiler from being confused.
Code: |
int8 RegisterMap[256];
#locate RegisterMap = 0x100 //Through 0x1FF
int8 Register0;
#locate Register0 = RegisterMap
#bit x0 = Register0.0
#bit x1 = Register0.1
#bit x2 = Register0.2
#bit x3 = Register0.3
#bit x4 = Register0.4
#bit x5 = Register0.5
#bit x6 = Register0.6
#bit x7 = Register0.7
int8 Register1;
#locate Register1 = RegisterMap + 0x01
#bit x8 = Register1.0
#bit x9 = Register1.1
#bit x10 = Register1.2
#bit x11 = Register1.3
#bit x12 = Register1.4
#bit x13 = Register1.5
#bit x14 = Register1.6
#bit x15 = Register1.7
|
|
|
|
|