int clock = 2;
int ATT = 3;
int RX = 4;
int TX = 5;
int ACK = 6;
char TMP;
void setup () {
Serial.begin(19200);
pinMode(clock,OUTPUT);
pinMode(ATT,OUTPUT);
pinMode(RX,INPUT);
pinMode(TX,OUTPUT);
pinMode(ACK,INPUT);
}
unsigned char psx_data(unsigned char out_byte); /* This is an example of a forward declaration or "prototype" */
void lcdDisplay(unsigned char); /* Also note your parameters don't have to match names... you only need the types and commas, FYI the moar yuo know */
void loop() {
unsigned char data_1;
unsigned char data_2;
digitalWrite(ATT, LOW); // lower ATT
psx_data(0x01); // send a start command to the psx controller
unsigned char type = psx_data(0x42); // send a data request to psx controller + get controller type
psx_data(0x00); // get 'here comes data' code
data_1 = psx_data(0x00); // get controller data byte 1
data_2 = psx_data(0x00); // get controller data byte 2
digitalWrite(ATT, HIGH); // raise ATT
lcdDisplay(type);
}
unsigned char psx_data(unsigned char out_byte)
{
unsigned char in_byte; // data received from PSX controller
unsigned char mask_byte;
unsigned char bit_counter;
mask_byte = 1;
in_byte = 0;
// for each bit in the transmitted byte
for (bit_counter = 0; bit_counter < 8; bit_counter++)
{
// CLK low
digitalWrite(clock,LOW);
// set up CMD line (data out to controller)
if (out_byte & mask_byte) digitalWrite(TX,HIGH);
else digitalWrite(TX,LOW);
// shift the mask bit for the next comparison
mask_byte <<= 1;
delay(1);
// CLK high
digitalWrite(TX,HIGH);
// load the current controller data bit on the bus into our return byte
if (digitalRead(RX) == 1) in_byte |= 0x80;
// and right-shift to line up the next bit (last bit has final value, no need to shift)
if (bit_counter < 7) in_byte >>= 1;
delay(1);
}
return in_byte;
}
//talks to my matrix orbital LCD display
void lcdDisplay (unsigned char x) {
Serial.print(254,BYTE);
Serial.print("X");
Serial.print("Sample: ");
Serial.print(x, DEC);
}
http://www.arduino.cc/playground/Main/AnalogPSXLibrary
[ 本帖最后由 px601 于 2010-11-27 10:55 编辑 ] |