Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Selecting bits from an analog input (http://www.chiefdelphi.com/forums/showthread.php?t=61550)

Spencer E. 13-01-2008 08:55

Selecting bits from an analog input
 
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

Re: Selecting bits from an analog input
 
Quote:

Originally Posted by Spencer E. (Post 676756)
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:

Code:

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

Re: Selecting bits from an analog input
 
Thanks so much!


All times are GMT -5. The time now is 23:16.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi