|
|
View previous topic :: View next topic |
Author |
Message |
ferrarilib
Joined: 11 Jun 2005 Posts: 38
|
ex_expio.c |
Posted: Tue Jun 21, 2005 3:54 am |
|
|
from the example EX_EXPIO.C
#include <74595.c>
#include <74165.c>
main() {
byte data;
do {
read_expanded_inputs (&data);
data |= 0xF8; //Force the unused input bits on
data -= (!(data&0x01)&!(data&0x02))<<2; //Turn on bit 2 it both inputs are
//toggled
write_expanded_outputs (&data);
} while (TRUE);
}
this is e code of example but i don't undastand
data -= (!(data&0x01)&!(data&0x02))<<2;
what do this ?
i have try emule it but dont cange state of bit 3 in output when all button is on .
there is sameone that can help me ?
thanks |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 21, 2005 4:18 am |
|
|
(!(data&0x01)&!(data&0x02))<<2;
Split this up. The '&' statements are logical bitwise '&' operators. So the bit (data & 0x02), will give a '2' as an output, if the second bit is on in 'data'. The (data & 0x01), will give a '1' as an output if the bottom bit is on in data.
Then the '!' statement, is a logical 'not' operator. Basically if the value is 'true', it makes the result 'false'.
It would appear that the buttons are expected to pull the signal 'down' to 0v, when on, and let the signal rise to 5v, when the buttons are off. So the two statements would each return 'true' for two buttons on bit 0, and bit 1. Then the '&' statement is another bitwise and. Now this is a little worrying, since the values are now 'logic' values, rather than bitwise values (&&, is the 'logical' and statement).
Then this value is shifted left two bits, and subtracted from the existing 'data' value.
I'd redraft the statement as:
data -= (((data & 1)==0) && ((data & 2)==0))?4:0;
Now this handles the logic rather differently. It tests for the bits being zero, with ((data & 1)==0), and the similar test for bit 1. If the bits are zero, it returns a logical 'true' in each case, and then uses the logical and operator to combine these. This statement is evaluated, to give a logical 'true' when both buttons are holding the signals low. Then instead of relying on rotating a logical 'true' value, it uses the '?:' construct to return a '4' if the statement is true, and a '0' if it is false.
This form should work on any C compiler, no matter what numeric value is returned for 'true', while the former is reliant on the internal logical conversions behaving in one particular way...
Best Wishes |
|
|
ferrarilib
Joined: 11 Jun 2005 Posts: 38
|
|
Posted: Tue Jun 21, 2005 6:03 am |
|
|
thanks Ttelmah ..
now i undastend all you told to me ..
the !(data&0x01) ! is because button down V=0 if button off is +5V
ths is for button 2 too !
But why <<2 ? not mast be 3 ? <<3 ?
<<2 set bit 2 not bit 3 ! is 3 led .
or because begin from 0 not from 1 ?
thanks i know that is stupid questions but i am beging to program pic in this days .. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 21, 2005 7:23 am |
|
|
If it is 'bit3', then in my alternative code the value to set the bit would need to be '8', not 4. The problem is a question of what 'true' is defined as. In 'C', technically anything non-zero is 'true'. In some compilers, by default, logic operations return '-1', which has all bits on Hence rotating this left two, would just move it up to 'clear' the input bits, and would turn on all the bits above these. However if the operations just return '1' for a true (which is how CCS currenly define it), then as you say, it'd need to be rotated three times to move a 'true' to bit three. However the logic as given is wrong, in using the bitwise 'and' operator...
Was this code written 'for' the CCS compiler, or has it been ported from another chip/compiler?. If so, this would (might...) explain the oddities...
Best Wishes |
|
|
Guest
|
|
Posted: Wed Jun 22, 2005 5:51 am |
|
|
now i undastand thanks ..
is like write :
if(!(data & 0x01) && !(data& 0x02))
data -= 4;
else
data -= 0;
the <<2 in the exsample do if all button is pushed
00000000 do(true) 00000001 to(<<) 00000100
thanks
i hope that is this |
|
|
|
|
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
|