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

A bit in a byte of an array

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



Joined: 24 Sep 2006
Posts: 262

View user's profile Send private message AIM Address

A bit in a byte of an array
PostPosted: Thu Apr 25, 2013 1:03 pm     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Thu Apr 25, 2013 1:30 pm     Reply with quote

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

View user's profile Send private message AIM Address

a bit in a byte of an array
PostPosted: Sat Apr 27, 2013 5:30 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 27, 2013 8:40 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 27, 2013 8:46 pm     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Sun Apr 28, 2013 2:05 am     Reply with quote

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
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