Wii nunchuck和arduino通信
本帖最后由 ttyp 于 2012-11-3 16:09 编辑手头有几个山寨的鸡腿,由于线的质量问题(比原装的容易断),很不灵敏,因此淘汰下来,近日看到几篇arduino控制它的方法,心里痒痒,查到淘宝上有控制板子卖,看到几家都说是坑爹板,仔细看看,原来板子上没有任何电子器件,,最低还8块,连上邮费要20,果然坑爹啊。还是省省吧,拆开鸡腿,果然看到板子上有熟悉的VCC,SDA,SCK,GND等标记,其实鸡腿就是一个I2C的传感器啊!!
这个是连线对应的阵脚
红:vcc
黄:sck
绿:sda
白:gnd
黑:dnt
这个是接口图
我的鸡腿反正也没什么用,已经买了几个二手的原装货,果断剪断连线,焊上面包线连接。
上图是演示图,下面的代码里,红线VCC连3脚,白线地连2脚
一开始使用了一些标准库,这里下载的,不能使用,读不出数据。后来查到原来山寨的和原装的不一样,请看这篇文章
下载了山寨版的代码,改了下1.01的兼容性,以下是代码:
/*
* NunchuckPrint
*
* 2007 Tod E. Kurt, http://todbot.com/blog/
*
* The Wii Nunchuck reading code is taken from Windmeadow Labs
* http://www.windmeadow.com/node/42
*/
#include <Wire.h>
uint8_t ctrlr_type;
void setup()
{
Serial.begin(9600);
nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
nunchuck_init(); // send the initilization handshake
Serial.print ("Finished setup\n");
}
void loop()
{
nunchuck_get_data();
nunchuck_print_data();
delay(100);
}
//
// Nunchuck functions
//
static uint8_t nunchuck_buf; // array to store nunchuck data,
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |=_BV(pwrpin);
delay(100);// wait for things to stabilize
}
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{
byte cnt;
Wire.begin();
// init controller
delay(1);
Wire.beginTransmission(0x52); // device address
Wire.write(0xF0); // 1st initialisation register
Wire.write(0x55); // 1st initialisation value
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.write(0xFB); // 2nd initialisation register
Wire.write(0x00); // 2nd initialisation value
Wire.endTransmission();
delay(1);
// read the extension type from the register block
Wire.beginTransmission(0x52);
Wire.write(0xFA); // extension type register
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.requestFrom(0x52, 6); // request data from controller
for (cnt = 0; cnt < 6; cnt++) {
if (Wire.available()) {
ctrlr_type = Wire.read(); // Should be 0x0000 A420 0101 for Classic Controller, 0x0000 A420 0000 for nunchuck
}
}
Wire.endTransmission();
delay(1);
// send the crypto key (zeros), in 3 blocks of 6, 6 & 4.
Wire.beginTransmission(0x52);
Wire.write(0xF0); // crypto key command register
Wire.write(0xAA); // sends crypto enable notice
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.write(0x40); // crypto key data address
for (cnt = 0; cnt < 6; cnt++) {
Wire.write(0x00); // sends 1st key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.write(0x40); // sends memory address
for (cnt = 6; cnt < 12; cnt++) {
Wire.write(0x00); // sends 2nd key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.write(0x40); // sends memory address
for (cnt = 12; cnt < 16; cnt++) {
Wire.write(0x00); // sends 3rd key block (zeros)
}
Wire.endTransmission();
delay(1);
// end device init
}
// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00);// sends one byte
Wire.endTransmission(); // stop transmitting
}
// Receive data back from the nunchuck,
int nunchuck_get_data()
{
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf = nunchuk_decode_byte(Wire.read());
cnt++;
}
nunchuck_send_request();// send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{
static int i=0;
int joy_x_axis = nunchuck_buf;
int joy_y_axis = nunchuck_buf;
int accel_x_axis = nunchuck_buf; // * 2 * 2;
int accel_y_axis = nunchuck_buf; // * 2 * 2;
int accel_z_axis = nunchuck_buf; // * 2 * 2;
int z_button = 0;
int c_button = 0;
// byte nunchuck_buf contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf
if ((nunchuck_buf >> 0) & 1)
z_button = 1;
if ((nunchuck_buf >> 1) & 1)
c_button = 1;
if ((nunchuck_buf >> 2) & 1)
accel_x_axis += 2;
if ((nunchuck_buf >> 3) & 1)
accel_x_axis += 1;
if ((nunchuck_buf >> 4) & 1)
accel_y_axis += 2;
if ((nunchuck_buf >> 5) & 1)
accel_y_axis += 1;
if ((nunchuck_buf >> 6) & 1)
accel_z_axis += 2;
if ((nunchuck_buf >> 7) & 1)
accel_z_axis += 1;
Serial.print(i,DEC);
Serial.print("\t");
Serial.print("joy:");
Serial.print(joy_x_axis,DEC);
Serial.print(",");
Serial.print(joy_y_axis, DEC);
Serial.print("\t");
Serial.print("acc:");
Serial.print(accel_x_axis, DEC);
Serial.print(",");
Serial.print(accel_y_axis, DEC);
Serial.print(",");
Serial.print(accel_z_axis, DEC);
Serial.print("\t");
Serial.print("but:");
Serial.print(z_button, DEC);
Serial.print(",");
Serial.print(c_button, DEC);
Serial.print("\r\n");// newline
i++;
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
果然读出了数据!!鸡腿二手的20多一个,真实廉价的传感器啊,包括游戏摇杆,2个按钮和3轴加速模块,性价比很高!!
下面是原装的代码(未测试,适用于arduino1.0.1):
/*
* NunchuckPrint
*
* 2007 Tod E. Kurt, http://todbot.com/blog/
*
* The Wii Nunchuck reading code is taken from Windmeadow Labs
* http://www.windmeadow.com/node/42
*/
#include <Wire.h>
void setup()
{
Serial.begin(9600);
nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
nunchuck_init(); // send the initilization handshake
Serial.print ("Finished setup\n");
}
void loop()
{
nunchuck_get_data();
nunchuck_print_data();
delay(100);
}
//
// Nunchuck functions
//
static uint8_t nunchuck_buf; // array to store nunchuck data,
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |=_BV(pwrpin);
delay(100);// wait for things to stabilize
}
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x40);// sends memory address
Wire.write(0x00);// sends sent a zero.
Wire.endTransmission(); // stop transmitting
}
// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00);// sends one byte
Wire.endTransmission(); // stop transmitting
}
// Receive data back from the nunchuck,
int nunchuck_get_data()
{
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf = nunchuk_decode_byte(Wire.read());
cnt++;
}
nunchuck_send_request();// send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{
static int i=0;
int joy_x_axis = nunchuck_buf;
int joy_y_axis = nunchuck_buf;
int accel_x_axis = nunchuck_buf; // * 2 * 2;
int accel_y_axis = nunchuck_buf; // * 2 * 2;
int accel_z_axis = nunchuck_buf; // * 2 * 2;
int z_button = 0;
int c_button = 0;
// byte nunchuck_buf contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf
if ((nunchuck_buf >> 0) & 1)
z_button = 1;
if ((nunchuck_buf >> 1) & 1)
c_button = 1;
if ((nunchuck_buf >> 2) & 1)
accel_x_axis += 2;
if ((nunchuck_buf >> 3) & 1)
accel_x_axis += 1;
if ((nunchuck_buf >> 4) & 1)
accel_y_axis += 2;
if ((nunchuck_buf >> 5) & 1)
accel_y_axis += 1;
if ((nunchuck_buf >> 6) & 1)
accel_z_axis += 2;
if ((nunchuck_buf >> 7) & 1)
accel_z_axis += 1;
Serial.print(i,DEC);
Serial.print("\t");
Serial.print("joy:");
Serial.print(joy_x_axis,DEC);
Serial.print(",");
Serial.print(joy_y_axis, DEC);
Serial.print("\t");
Serial.print("acc:");
Serial.print(accel_x_axis, DEC);
Serial.print(",");
Serial.print(accel_y_axis, DEC);
Serial.print(",");
Serial.print(accel_z_axis, DEC);
Serial.print("\t");
Serial.print("but:");
Serial.print(z_button, DEC);
Serial.print(",");
Serial.print(c_button, DEC);
Serial.print("\r\n");// newline
i++;
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
} 想做游戏的买个鸡腿不错!!比红外遥控方便多了:lol
For the Arduino to communicate with the nunchuck, it must send a handshake. So first send 2 bytes "0x40,0x00". Then send one byte "0x00" each time you request data from the nunchuck. The data from the nunchuck will come back in 6 byte chunks.
Byte Description Values of sample Nunchuk
1 X-axis value of the analog stick Min(Full Left):0x1E / Medium(Center):0x7E / Max(Full Right):0xE1
2 Y-axis value of the analog stick Min(Full Down):0x1D / Medium(Center):0x7B / Max(Full Right):0xDF
3 X-axis acceleration value Min(at 1G):0x48 / Medium(at 1G):0x7D / Max(at 1G):0xB0
4 Y-axis acceleration value Min(at 1G):0x46 / Medium(at 1G):0x7A / Max(at 1G):0xAF
5 Z-axis acceleration value Min(at 1G):0x4A / Medium(at 1G):0x7E / Max(at 1G):0xB1
6 Button state (Bits 0/1) / acceleration LSB Bit 0: "Z"-Button (0 = pressed, 1 = released) / Bit 1: "C" button (0 = pressed, 1 = released) / Bits 2-3: X acceleration LSB / Bits 4-5: Y acceleration LSB / Bits 6-7: Z acceleration LSB
这个是鸡腿的通信协议 好鸡腿;P;P;P 怎么都是左手控制器,连右手控制器的方法有吗 花了22买了一个山寨的,还好能用
页:
[1]