Page 1 of 1
setting a specific bit
Posted: Tue Mar 18, 2003 8:22 pm
by slacker
how would i go about setting an individual bit...on the keyboard controller i want to set a certain bit to turn on a LED light without messing with the other bits.
Re:setting a specific bit
Posted: Wed Mar 19, 2003 12:49 am
by Mr_Spam
ive also been thinking of some LEDS handeling algorythems... is there a way to read the status of which LEDS are on or off? then you could toggle that bit to change without chaning the state of the other leads.
Re:setting a specific bit
Posted: Wed Mar 19, 2003 1:34 am
by distantvoices
You keep an global unsigned char-variable in your keyboard driver module: uchar_t led_state
it has 8 bits. we need the lower three:
000: everything off.
001: scroll on = 1
010: caps lock on = 2
100: num lock on = 4
say you want to set num lock:
led_state+=4.
to set caps lock: led_state+=2.
Now you have the following bit-mask:110
say you want to switch off num lock:
led_state-=4.
Now the bitmask looks like this: 010.
got it?
Re:setting a specific bit
Posted: Wed Mar 19, 2003 3:26 am
by Pype.Clicker
i wouldn't use '+' and '-' to manipulate a bitfield, if i were you: trying to set bit 1 with x+=2 will modify bit 2 if bit 1 was already set ...
It's cleaner to use bitwise operators like '&' and '|'
x|=2 : sets bit 1, other bits unchanged
x&=(~2) : clears bit 1, other bits unchanged.
Re:setting a specific bit
Posted: Wed Mar 19, 2003 3:34 am
by distantvoices
*gg*
It's a good method with bitwise operators, you are right.
this ~-operator, it INVERSES the bits, isn't it? Thus, ~2 would look like 101 in binary, and a field, say 110&101 will result in 100 - > bit 010 is erased. Did I understand this right?
Re:setting a specific bit
Posted: Wed Mar 19, 2003 4:25 am
by Pype.Clicker
yes, its roughly this, except that ~2 will not be 101,but rather 11111101 (0xFD) if the expression is a char-sized, 0xFFFD if the expression is short-sized and 0xfffffffd if it's an int.
To know how such operators work, you must learn their truth tables. For instance,
1&1 == 1, 1&0==0, 0&1==0, 0&0==0.
--> a 0 in x will clear the corresponding bit in a&x,
a 1 in x keeps the bit from a.
1|1 == 1, 1|0==1, 0|1==1, 0|0==0.
--> a 1 in x will set the corresponding bit in a|x
1^1 == 0, 1^0==1, 0^1==1, 0^0==1.
--> a 1 in x will toggle the corresponding bit in a^x
Re:setting a specific bit
Posted: Wed Mar 19, 2003 5:21 am
by distantvoices
truth tables:
AND (a&b) or (a&&b):
a and b result
1 1 true
1 0 false
0 1 false
0 0 false
OR(a|b) or (a||b):
a or b result
1 1 true
1 0 true
0 1 true
0 0 false
exclusive OR:
a XOR b result
1 1 false
1 0 true
0 1 true
0 0 false
Implication: also expressed as !a|b (NOT a OR b)
a implies b result
1 1 true
1 0 false
0 1 true
0 0 true
I didn't like the surreal experiments the teachers/professors did with these things, but I like using it in real nice nifty programs. These constructs can shorten else lenghty if structures.
Re:setting a specific bit
Posted: Wed Mar 19, 2003 11:30 am
by Mr_Spam
ah the good old truth tables.... did thoes last year in math class. I finally have a use for them!
Re:setting a specific bit
Posted: Thu Mar 20, 2003 12:18 am
by Perica
..
Re:setting a specific bit
Posted: Thu Mar 20, 2003 12:36 pm
by Peter_Vigren
Wait for the keyboard to accept commands. Send the command 0xED (put next byte into the output port) to port 0x60. After the keyboard once again is ready to accept a command, send the LED byte.
The 3 lesser bits (0:2) are the LED bits and the above must be zero.
Re:setting a specific bit
Posted: Thu Mar 20, 2003 4:09 pm
by slacker
Pype.Clicker wrote:
i wouldn't use '+' and '-' to manipulate a bitfield, if i were you: trying to set bit 1 with x+=2 will modify bit 2 if bit 1 was already set ...
It's cleaner to use bitwise operators like '&' and '|'
x|=2 : sets bit 1, other bits unchanged
x&=(~2) : clears bit 1, other bits unchanged.
what does the ~ do? i never learned about it....
and what does the &= and |& do?
Re:setting a specific bit
Posted: Thu Mar 20, 2003 8:00 pm
by Curufir
Well my C is a little hazy, but as far as I remember a tilde is the equivalent of a NOT gate.
So to run this through with only 8 bits you have:
Code: Select all
x&=(~2) : clears bit 1, other bits unchanged
You end up doing:
Which rather effectively clears the first bit.
Presumably the C compiler is smart enough to use the same number of bits in the 2 as in X, so all other bits will be left unchanged by the AND. This saves you the hassle of specifying a precise number to AND with at compile time (Magic numbers and relying on type size is
bad practice).
Forgot this in the original post. +=, -= , |=, &= etc are all abbreviations for common mathematical/logical operations.
Eg x+=1 actually means x=x+1
The rest follow exactly the same form.
Standard C stuff, but as I mentioned I'm a little rusty on it, so if I got something wrong please correct the mistake.
Re:setting a specific bit
Posted: Fri Mar 21, 2003 3:37 am
by Whatever5k
Curufir wrote:
You end up doing:
Actually that's
Remember: 2 is 10 in binary and ~ just inverses the bits (1 gets to 0 and other way round).
x &= y <equal> x = x & y
x | = y <equal> x = x | y
same with other operators (+,-,/, etc.)
Re:setting a specific bit
Posted: Fri Mar 21, 2003 9:56 am
by Curufir
Good catch Abless, serves me right for posting in the middle of the night
.