View previous topic :: View next topic |
Author |
Message |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
#bit into an array? |
Posted: Wed May 18, 2011 10:53 am |
|
|
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
|
|
Posted: Wed May 18, 2011 11:57 am |
|
|
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
|
|
Posted: Wed May 18, 2011 12:52 pm |
|
|
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: 19496
|
|
Posted: Thu May 19, 2011 2:40 am |
|
|
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.
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri May 20, 2011 7:53 pm |
|
|
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 |
|
|
|