|
|
View previous topic :: View next topic |
Author |
Message |
TheDude Guest
|
array of booleans |
Posted: Fri Oct 24, 2008 12:39 pm |
|
|
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: 1934 Location: Norman, OK
|
|
Posted: Fri Oct 24, 2008 1:19 pm |
|
|
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
|
|
Posted: Fri Oct 24, 2008 1:35 pm |
|
|
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
|
|
Posted: Fri Oct 24, 2008 2:35 pm |
|
|
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
|
|
Posted: Fri Oct 24, 2008 2:55 pm |
|
|
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
|
|
Posted: Fri Oct 24, 2008 3:23 pm |
|
|
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 |
|
|
|
|
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
|