I work on the Wii Nunchuk and I would connect this one with the NXT.
But i have some problem, the nunchuk does not responding at the I2C request.
-I use 33k pull-up (connected on 3.3v)
-The nunchuk is powered by 3.3v
-The of the NXT is connected on the Nunchuk
-The nunchuk I2C speed is 100khz (we can up to 400khz)
-The clock and the data wires is correctly connected
Nunchuk sensor :
- Code: Select all
import lejos.nxt.*;
/**
* I2C mastering for the Nunchuk
*
* @version v0.1
* @project Nunchuk RS232
* @author Si-Mohamed Lamraoui
*/
public class I2CNunchuk extends I2CSensor
{
//leJOS expects the unshifted 7 bit address and the read/write direction is specified separately (it is the last parameter in the Start method above).
// div by 2 -> convert to 7 bits by shifting the address right by 1.
public final static int NUNCHUCK_I2C_WRITE_ADDR = 0x52;//0xA4 / 2; //address (0x52) + write bit (0) = 0xA4
public final static byte NUNCHUCK_I2C_READ_ADDR = 0x52;// 0xA5 / 2; //address (0x52) + read bit (1) = 0xA5
private I2CPort port ;
private byte[] data = new byte[6];
private byte[] init = new byte[2];
/**
* Constructor
* @param sensorPort NXT sensor port
*/
public I2CNunchuk(I2CPort sensorPort)
{
super(sensorPort);
this.port = sensorPort;
this.setAddress(NUNCHUCK_I2C_READ_ADDR);
this.port.setType(SensorConstants.TYPE_LOWSPEED);
this. port.setMode(SensorConstants.MODE_RAW);
}
/**
* Initializes the Nunchuk to communicate in I2C
*/
public void initialize()
{
this.init[0] = 0x40;
this.init[1] = 0x00;
this.sendData(0x00, init,2);
/*try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{}*/
}
/**
* Method for retrieving the data from the Nunchuk
* @return value of 8-bit parallel bus port
*/
public byte[] getNunchukData()
{
this.getData(0x00, this.data, 6);
return data;
}
public void AskNunchukData()
{
this.sendData(0x00, (byte) 0x00);
}
/**
* Method for decrypt the data from the Nunchuk
* @return value decrypted
*/
public byte decryptData(byte value){
return (byte) ((value ^ 0x17) + 0x17);
}
}
Main :
- Code: Select all
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
/**
* I2C Testing
*
* @version v0.1
* @project Nunchuk RS232
* @author Si-Mohamed Lamraoui
*/
public class TestLCD
{
public static void main(String [] options)
{
I2CNunchuk nunchuk = new I2CNunchuk(SensorPort.S4);
Graphics g = new Graphics();
byte[] data_n = new byte[6];
int result;
nunchuk.initialize();
g.drawString(" NUNCHUK - NXT v0.1",1,1);
g.clear();
while(!Button.ESCAPE.isPressed())
{
g.clear();
g.drawString(" NUNCHUK values : ",1,1);
data_n = nunchuk.getNunchukData();
LCD.drawInt(data_n[0],1,1,3);
LCD.drawInt(data_n[1],1,1,4);
LCD.drawInt(data_n[2],1,1,5);
LCD.drawInt(data_n[3],1,1,6);
LCD.drawInt(data_n[4],1,1,7);
g.refresh();
nunchuk.AskNunchukData();
try
{
Thread.sleep(100);
}
catch (InterruptedException e) {}
}
}
}
I find a man who have connected the Nunchuk in C language:
http://www.mindstormsforum.de/viewtopic.php?t=3828
Thanks
underfarad
