View previous topic :: View next topic |
Author |
Message |
rovtech
Joined: 24 Sep 2006 Posts: 262
|
A bit in a byte of an array |
Posted: Thu Apr 25, 2013 1:03 pm |
|
|
I am trying to access a bit in a byte of a char array.
The following works:
Code: |
// incoming packets
char packet [5]; // packets to receive
#define ackn (packet [0])
#define status (packet [1]) // compass
#define bat_volts (packet [2])
#define bat_amps (packet [3])
#define spare (packet [4])
status = 0x3C; // initialize to NO COMPASS
// compass signals
char hdg_att;
hdg_att = status;
#bit sp1 = hdg_att.0
#bit sp2 = hdg_att.1
#bit south = hdg_att.2
#bit east = hdg_att.3
#bit north = hdg_att.4
#bit west = hdg_att.5
#bit sp3 = hdg_att.6
#bit sp4 = hdg_att.7
|
I would like to use the following but the compiler gives an error.
Is there a better way to do this?
Code: |
// incoming packets
char packet [5]; // packets to receive
#define ackn (packet [0]) // acknowledge
#define status (packet [1]) // SROV Status, compass for now
#define bat_volts (packet [2]) // SROV Raw Battery voltage
#define bat_amps (packet [3]) // SROV Raw Battery current
#define spare (packet [4]) // Spare
status = 0x3C; // initialize to NO COMPASS
// compass signals
#bit sp1 = status.0
#bit sp2 = status.1
#bit south = status.2
#bit east = status.3
#bit north = status.4
#bit west = status.5
#bit sp3 = status.6
#bit sp4 = status.7
|
The use is:
Code: |
if (south)
// do the following
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19568
|
|
Posted: Thu Apr 25, 2013 1:30 pm |
|
|
Just use bit_test.
Code: |
#define SP1 0
#define SP2 1
#define SOUTH 2
#define EAST 3
#define NORTH 4
#define WEST 5
#define SP3 6
#define SP4 7
if (bit_test(status,SOUTH))
|
#bit can _only_ map to a fixed location.
Best Wishes |
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
a bit in a byte of an array |
Posted: Sat Apr 27, 2013 5:30 pm |
|
|
Thanks Ttelmah, I was hoping for something that would read easier.
if (NORTH) is much easier than if (bit_test(status, NORTH))
My method that works (first example) is nice except for having to equate the array byte to another variable. Why is the array variable "status" different from a "simple" variable "hdg_att", apart from being an element in an array.
The manual says that #bit id = x.y where id is a valid C identifier, x is a constant or a C variable, and y is a constant 0-7. However the error when I try the second example is that x must be a "simple variable".
I will try your suggestion and see if it works. bit_test may also require a simple variable. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 27, 2013 8:40 pm |
|
|
You can always use #define statements to make the code look like
whatever you want. Example:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char packet[5];
#define status (packet[1])
#define North (bit_test(status, 4))
//==========================================
void main()
{
if(North)
printf("N");
else
printf("?");
while(1);
} |
|
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Apr 27, 2013 8:46 pm |
|
|
Why not bit fields?: Code: | struct {
int8 ackn;
int8 sp1:1;
int8 sp2:1;
int8 south:1;
int8 east:1;
int8 north:1;
int8 west:1;
int8 sp3:1;
int8 sp4:1;
int8 bat_volts;
int8 bat_amps;
int8 spare;
} packet;
// ...
if (packet.north) // ... |
_________________ Andrew |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19568
|
|
Posted: Sun Apr 28, 2013 2:05 am |
|
|
Yes. The key point is that #bit, has to be locatable at compile time.
bit fields should work, but I'd probably just use a #define. It has the feeling of being 'safer', knowing the compiler....
Best Wishes |
|
|
|