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 CCS Technical Support

#bit into an array?

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



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

#bit into an array?
PostPosted: Wed May 18, 2011 10:53 am     Reply with quote

Never had to do it before but I would like to do the following:
BYTE address, i2creceivedata[5], i2csenddata[5];
#bit iRun = i2csenddata[4].0
#bit iError = i2csenddata[4].1

I can't see a reason why this isn't possible as the compiler must know where the array is stored. Is there a good robust workaround?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 18, 2011 11:57 am     Reply with quote

This works, but it seems a little messy.

What this does is:
- Declare the array
- Set a fixed address for it
- Identify an element and its address
- Declare a bit in that element

Maybe there's some easier way to do it, but I don't have time to work
on this any more.
Code:

#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

BYTE i2csenddata[5];
#locate i2csenddata = 0x400
#byte i2csenddata_4 = i2csenddata +4
#bit iRun = i2csenddata_4.0

//======================================
void main(void)
{

iRun = 1;

while(1);
}
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Wed May 18, 2011 12:52 pm     Reply with quote

Thanks, looks a very neat solution to me. I was using up precious code to do this with variables.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Thu May 19, 2011 2:40 am     Reply with quote

As a comment, you don't need to locate the array, the compiler already knows where it is, and can do the offset in the same line as the bit location.
So:
Code:

BYTE i2csenddata[5];
#bit iRun = i2csenddata+4.0


Will happily give you a variable 'iRun', accessing bit 0 of the last byte in i2csenddata.
It is a really useful ability. Smile

Best Wishes
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri May 20, 2011 7:53 pm     Reply with quote

I also tend to build structures so I don't worry about the big location when modifying it... only when creating the structure.

From there, I can spit out the entire struct or plunk around in it by names.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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