Log in

View Full Version : Selecting bits from an analog input


Spencer E.
13-01-2008, 08:55
Hey everyone, we are going to be using the USB chicklet and for buttons 5-8 it uses 4 bits from the AUX port on the joystick but I can't figure out how to select the bits from the actual output. Is there a simple way to do this or will I just have to do some math and figure out which output is being pressed by removing 128, 64, 32 and 16 from the output? Thanks in advance!

kaszeta
13-01-2008, 09:03
Hey everyone, we are going to be using the USB chicklet and for buttons 5-8 it uses 4 bits from the AUX port on the joystick but I can't figure out how to select the bits from the actual output.

There are several posts here on that if you search, but the easiest thing to do is use the bitwise AND operator. I also recommend writing a function that translates the AUX port to some global variables:


void Buttons(void)
{
p1_sw_5=p1_sw_6=p1_sw_7=p1_sw_8=1;
p1_sw_5=(p1_aux&1<<7)?0:1;
p1_sw_6=(p1_aux&1<<6)?0:1;
p1_sw_7=(p1_aux&1<<5)?0:1;
p1_sw_8=(p1_aux&1<<4)?0:1;
}


where p1_sw_* are global variables.

(I wrote this to demonstrate the ?: operator to some students, you can also invert the logic test instead).

Spencer E.
13-01-2008, 09:06
Thanks so much!