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

array of booleans

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







array of booleans
PostPosted: Fri Oct 24, 2008 12:39 pm     Reply with quote

Ok, here's a strange one. I'm trying to input a byte, bit by bit, using an array to store them all individually.
If I do it like this it doesn't work:
Code:

int1 sensor_uitlezing[8] = {false, false, false, false, false, false, false, false};
for(uitgangsteller = 0; uitgangsteller < 8; uitgangsteller++)
      sensor_uitlezing[uitgangsteller] = input(PIN_A4);


If I do it like this, it works, but it's not the most elegant solution:
Code:

int1 sensor_uitlezing[8] = {false, false, false, false, false, false, false, false};
for(uitgangsteller = 0; uitgangsteller < 8; uitgangsteller++)
switch(uitgangsteller)
   {
      case 0:  sensor_uitlezing[0] = !input(PIN_A4);
               break;
      case 1:  sensor_uitlezing[1] = !input(PIN_A4);
               break;
      case 2:  sensor_uitlezing[2] = !input(PIN_A4);
               break;
      case 3:  sensor_uitlezing[3] = !input(PIN_A4);
               break;
      case 4:  sensor_uitlezing[4] = !input(PIN_A4);
               break;
      case 5:  sensor_uitlezing[5] = !input(PIN_A4);
               break;
      case 6:  sensor_uitlezing[6] = !input(PIN_A4);
               break;
      case 7:  sensor_uitlezing[7] = !input(PIN_A4);
               break;
   }


Maybe i'm just rusty on my c skills, but what am I doing wrong here? Oh yes, if I forgot to mention something, I'll be happy to supply the info...

thanks in advance,
TheDude
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Oct 24, 2008 1:19 pm     Reply with quote

When you say the first one does not work..how?

It looks like to me that both ways should do the same basic thing. However the first one does not invert the A4 input where the second one does...
TheDude
Guest







PostPosted: Fri Oct 24, 2008 1:35 pm     Reply with quote

Ok, the inverting thing is my fault, I forgot it in the first piece of code. The first one doesn't work, the array stays false, all of it
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 24, 2008 2:35 pm     Reply with quote

I think there is a bug in the generated ASM code, when a bit array is
loaded from the input() function. A work-around is to load an
intermediate variable with input(). Then write that variable to the bit
array. Example:
Code:

int8 temp;   // Intermediate variable

// Put these two lines inside your for() loop:
temp = input(PIN_A4);
sensor[i] = temp;

I have shortened your variable names to make it easier.
Ttelmah
Guest







PostPosted: Fri Oct 24, 2008 2:55 pm     Reply with quote

What compiler version?.
Booleans as a whole, have some 'oddities'. Funnily part at least of these is to do with supposedly increasing ANSI compatibility...
I see PCM has posted a fix. One other is possibly to be more explicit in data typing and testing. So possibly:
Code:

      sensor_uitlezing[uitgangsteller] = (int1)(input(PIN_A4) == 0);

(for the inverted version).

Best Wishes
TheDude
Guest







PostPosted: Fri Oct 24, 2008 3:23 pm     Reply with quote

Hey,
just dropped in to say i tried the code you suggested, and guess what. It works perfectly. Thanks a LOT.
Have a drink and put it on my tab...

cheers,
TheDude
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