CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

byte, #define, #bit

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

byte, #define, #bit
PostPosted: Fri May 14, 2004 4:57 pm     Reply with quote

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








PostPosted: Fri May 14, 2004 10:13 pm     Reply with quote

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?

Smile Robert
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sat May 15, 2004 4:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 5:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 6:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 7:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 7:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 7:58 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 15, 2004 8:31 am     Reply with quote

It seems to be what I am looking for. C is more flexible than me Very Happy .

Can you tell me what does '(Bits *)' do?

Thank you.
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Sat May 15, 2004 9:03 am     Reply with quote

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:

Code:
testvar->bit0=x;
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Sat May 15, 2004 9:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 17, 2004 8:06 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group