NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Project Help and Ideas » Interfacing a PS2 Controller with the Nerdkits
January 29, 2010 by Ethanal |
A couple years ago, I bought this for my NXT, but now, I am trying to interface it with the Nerdkits. I found some sample code for the controller written in a c-like language called NXC and I am now trying to translate it into regular C for the Atmega168. The board talks to the NXT over I2C (whatever that is). Can anyone help me translate this? The original this how far I've gotten on the header-like file: /*-------------------------------------- Controller button layout: L1 R1 L2 R2 d triang a c square circle b cross l_j_b r_j_b l_j_x r_j_x l_j_y r_j_y -------------------------------------- / / bits as follows: b1: a b c d x r_j_b l_j_b x b2: square cross circle triang R1 L1 R2 L2 */ /************/ //done struct psp { unsigned char b1; //raw byte read from PSP-Nx unsigned char b2; //raw byte read from PSP-Nx
}; /************/ void PSP_SendCommand(unsigned char port, unsigned char i2cAddr, unsigned char cmd) { unsigned char cmdBuf[]; ArrayBuild(cmdBuf, i2cAddr, 0x41, cmd); I2CWrite(port, 0, cmdBuf); int status = I2CCheckStatus(port); while (status > NO_ERR) status = I2CCheckStatus(port); } /************/ void PSP_PowerOFF(byte port, byte i2cAddr) { PSP_SendCommand(port, i2cAddr, 'D'); } /************/ void PSP_PowerON(byte port, byte i2cAddr) { PSP_SendCommand(port, i2cAddr, 'E'); } /************/ void PSP_ReadButtonState(byte port, byte i2cAddr, psp & currState) {
} /************/ string PSP_format_bin ( int i ) { string s;
int b = 0x80; s = ""; for ( j = 0; j < 8; j++) { if ( i&b ) { s += NumToStr(1); } else { s += NumToStr(0); } b = b>>1; } return (s); } This is the demo program (includes the header file above) include "PSP-Nx-lib.nxc"const byte SensorPort = IN_1; define ADDR 0x02/*-------------------------------------- Controller button layout:
a c square circle b cross
-------------------------------------- / / bits as follows: b1: a b c d x r_j_b l_j_b x b2: square cross circle triang R1 L1 R2 L2 */ task main() { string msg, msg0, msg1, msg2, msg3; psp currState; string x; int i; while (true ) {
} } Thanks! |
---|---|
January 29, 2010 by hevans (NerdKits Staff) |
Hi Ethenal, It sounds like you have found yourself a really neat project on your hands. My suggestion is to try to step back a little and understand the I2C protocol a little better (also known as the TWI - Two Wire Interface). From the little bit of reading I did into that controller it seems the NXT abstracts away an awful lot of the actual communication which makes it easy to use, but pretty much impossible to understand. The Atmega168 has an on board module to handle I2C. In the datasheet it is called the 2 wire serial interface. Take a look at that section of the datasheet, along with a few threads on the forums about the two wire interface. Try to just read the raw data coming out of the controller, without worrying about decoding it. Once you have a bitstream coming out of your controller, you can start trying to see what that NXT code does and decode what the controller is sending. Hope that gets you going in the right direction! Humberto |
January 29, 2010 by Ethanal |
Thanks! By "reading the raw data" do you mean using an oscilloscope, or connecting the controller to the Atmega168 and reading I2C data from there (via serial or lcd)? |
February 02, 2010 by hevans (NerdKits Staff) |
Hi Ethanal, I guess I meant, any way you can get find to read the raw bit stream. If you have an oscilloscope handy, then great! That should let you immediately see the signal coming out, then all you have to do is decode it. If not, then you have to use the MCU to try to catch the I2C and send it down the serial port to see. Humberto |
February 02, 2010 by Ethanal |
I don't have an oscilloscope, but I have a Bus Pirate coming in the mail soon. I guess I can use that or the MCU. Thanks! |
Please log in to post a reply.
Did you know that LEDs (light emitting diodes) only conduct current in one direction, like normal diodes? Learn more...
|