Author Topic: Boolean values with P18  (Read 2451 times)

0 Members and 1 Guest are viewing this topic.

paulstreats

  • Guest
Boolean values with P18
« on: February 23, 2008, 07:36:43 PM »
Hey,

 Im trying to use boolean values with p18 but it doesnt seem to accept the usual Bit method.
when i use the following

unsigned int direction:1;

this is supposed to convert the int into a single bit, ie. value 0 or value 1

But it doesnt work, the code compiles fine but it allows for other values to be placed into the int such as 2,3,4,5 etc....

The idea is to just have a variable with a boolean value, therefore cutting down on memory requirements.

Any suggestions?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Boolean values with P18
« Reply #1 on: February 24, 2008, 09:51:30 AM »
Probably a dumb comment on my part, mostly because your compiler might not handle this but . . .

can't you do

bool direction =1;

or

boolean direction =1;

?

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: Boolean values with P18
« Reply #2 on: February 24, 2008, 11:13:34 AM »
make a "main boolean" variable, and define a name for each bit

paulstreats

  • Guest
Re: Boolean values with P18
« Reply #3 on: February 24, 2008, 01:07:51 PM »
It doesnt accept 'boolean' or 'bool' unfortunately. I have come across c compilers before that dont but you can get around it by making typedef structs and then padding with the extra bits, c18 accepts the syntax below, but it doesnt follow it, thereby wasting memory.

The idea is to make adding actuators/sensors as an object form, the objects contain a whole byte but with different names for bit structures
Code: [Select]
typedef struct {         //
       unsigned char direction:1; //specify a bit field of 1 - allowing values of either 0 or 1
       unsigned char speed:5; //specify a bit field of 5 - allowing 5 bit value 0 to 32
       unsigned char port:2; //specify a bit field of 2 allowing a 2 bit value of 0 to 3
       :0; //bit padding, no bits are needed here because a full byte has been used above
} motor;          //the structure is called "motor"


heres how i would implement it:

Code: [Select]

motor leftmotor; //create a new instance of the motor'type' called leftmotor

leftmotor.direction = 0; //set the leftmotors direction to 0
leftmotor.speed = 0; //set the leftmotors speed to 0
leftmotor.port = 1; //set the active port to motorport 1 (maybe consider linking it with a predefined port type set)


It seems a good way of grouping attributes together and keeping memory usage down. I was wondering if maybe it is working but overloading the bits so if i assign a 4 to the 'port' it overloads the bits and actually shows a 0?

 


data_list