极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 30695|回复: 5

Wii nunchuck和arduino通信

[复制链接]
发表于 2012-11-3 16:05:05 | 显示全部楼层 |阅读模式
本帖最后由 ttyp 于 2012-11-3 16:09 编辑

手头有几个山寨的鸡腿,由于线的质量问题(比原装的容易断),很不灵敏,因此淘汰下来,近日看到几篇arduino控制它的方法,心里痒痒,查到淘宝上有控制板子卖,看到几家都说是坑爹板,仔细看看,原来板子上没有任何电子器件,,最低还8块,连上邮费要20,果然坑爹啊。还是省省吧,拆开鸡腿,果然看到板子上有熟悉的VCC,SDA,SCK,GND等标记,其实鸡腿就是一个I2C的传感器啊!!



这个是连线对应的阵脚

红:vcc
黄:sck
绿:sda
白:gnd
黑:dnt

这个是接口图



我的鸡腿反正也没什么用,已经买了几个二手的原装货,果断剪断连线,焊上面包线连接。



上图是演示图,下面的代码里,红线VCC连3脚,白线地连2脚



一开始使用了一些标准库,这里下载的,不能使用,读不出数据。后来查到原来山寨的和原装的不一样,请看这篇文章

下载了山寨版的代码,改了下1.01的兼容性,以下是代码:

  1. /*
  2. * NunchuckPrint
  3. *
  4. * 2007 Tod E. Kurt, [url]http://todbot.com/blog/[/url]
  5. *
  6. * The Wii Nunchuck reading code is taken from Windmeadow Labs
  7. *   [url]http://www.windmeadow.com/node/42[/url]
  8. */

  9. #include <Wire.h>
  10. uint8_t ctrlr_type[6];

  11. void setup()
  12. {
  13.   Serial.begin(9600);
  14.   nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  15.   nunchuck_init(); // send the initilization handshake
  16.   Serial.print ("Finished setup\n");
  17. }

  18. void loop()
  19. {
  20.   nunchuck_get_data();
  21.   nunchuck_print_data();
  22.   delay(100);
  23. }


  24. //
  25. // Nunchuck functions
  26. //

  27. static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

  28. // Uses port C (analog in) pins as power & ground for Nunchuck
  29. static void nunchuck_setpowerpins()
  30. {
  31. #define pwrpin PORTC3
  32. #define gndpin PORTC2
  33.     DDRC |= _BV(pwrpin) | _BV(gndpin);
  34.     PORTC &=~ _BV(gndpin);
  35.     PORTC |=  _BV(pwrpin);
  36.     delay(100);  // wait for things to stabilize        
  37. }

  38. // initialize the I2C system, join the I2C bus,
  39. // and tell the nunchuck we're talking to it
  40. void nunchuck_init()
  41. {
  42. byte cnt;

  43. Wire.begin();
  44.             
  45. // init controller
  46. delay(1);
  47. Wire.beginTransmission(0x52); // device address
  48. Wire.write(0xF0);          // 1st initialisation register
  49. Wire.write(0x55);          // 1st initialisation value
  50. Wire.endTransmission();
  51. delay(1);
  52. Wire.beginTransmission(0x52);
  53. Wire.write(0xFB);          // 2nd initialisation register
  54. Wire.write(0x00);          // 2nd initialisation value
  55. Wire.endTransmission();
  56. delay(1);
  57.             
  58. // read the extension type from the register block        
  59. Wire.beginTransmission(0x52);
  60. Wire.write(0xFA);          // extension type register
  61. Wire.endTransmission();
  62. Wire.beginTransmission(0x52);
  63. Wire.requestFrom(0x52, 6);          // request data from controller
  64. for (cnt = 0; cnt < 6; cnt++) {
  65.     if (Wire.available()) {
  66.         ctrlr_type[cnt] = Wire.read(); // Should be 0x0000 A420 0101 for Classic Controller, 0x0000 A420 0000 for nunchuck
  67.     }
  68. }
  69. Wire.endTransmission();
  70. delay(1);
  71.             
  72. // send the crypto key (zeros), in 3 blocks of 6, 6 & 4.
  73. Wire.beginTransmission(0x52);
  74. Wire.write(0xF0);          // crypto key command register
  75. Wire.write(0xAA);          // sends crypto enable notice
  76. Wire.endTransmission();
  77. delay(1);
  78. Wire.beginTransmission(0x52);
  79. Wire.write(0x40);          // crypto key data address
  80. for (cnt = 0; cnt < 6; cnt++) {
  81.     Wire.write(0x00);          // sends 1st key block (zeros)
  82. }
  83. Wire.endTransmission();
  84. Wire.beginTransmission(0x52);
  85. Wire.write(0x40);          // sends memory address
  86. for (cnt = 6; cnt < 12; cnt++) {
  87.     Wire.write(0x00);          // sends 2nd key block (zeros)
  88. }
  89. Wire.endTransmission();
  90. Wire.beginTransmission(0x52);
  91. Wire.write(0x40);          // sends memory address
  92. for (cnt = 12; cnt < 16; cnt++) {
  93.     Wire.write(0x00);          // sends 3rd key block (zeros)
  94. }
  95. Wire.endTransmission();
  96. delay(1);
  97. // end device init
  98. }

  99. // Send a request for data to the nunchuck
  100. // was "send_zero()"
  101. void nunchuck_send_request()
  102. {
  103.   Wire.beginTransmission(0x52); // transmit to device 0x52
  104.   Wire.write(0x00);  // sends one byte
  105.   Wire.endTransmission(); // stop transmitting
  106. }

  107. // Receive data back from the nunchuck,
  108. int nunchuck_get_data()
  109. {
  110.     int cnt=0;
  111.     Wire.requestFrom (0x52, 6); // request data from nunchuck
  112.     while (Wire.available ()) {
  113.       // receive byte as an integer
  114.       nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
  115.       cnt++;
  116.     }
  117.     nunchuck_send_request();  // send request for next data payload
  118.     // If we recieved the 6 bytes, then go print them
  119.     if (cnt >= 5) {
  120.      return 1;   // success
  121.     }
  122.     return 0; //failure
  123. }

  124. // Print the input data we have recieved
  125. // accel data is 10 bits long
  126. // so we read 8 bits, then we have to add
  127. // on the last 2 bits.  That is why I
  128. // multiply them by 2 * 2
  129. void nunchuck_print_data()
  130. {
  131.   static int i=0;
  132.   int joy_x_axis = nunchuck_buf[0];
  133.   int joy_y_axis = nunchuck_buf[1];
  134.   int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
  135.   int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
  136.   int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;

  137.   int z_button = 0;
  138.   int c_button = 0;

  139.   // byte nunchuck_buf[5] contains bits for z and c buttons
  140.   // it also contains the least significant bits for the accelerometer data
  141.   // so we have to check each bit of byte outbuf[5]
  142.   if ((nunchuck_buf[5] >> 0) & 1)
  143.     z_button = 1;
  144.   if ((nunchuck_buf[5] >> 1) & 1)
  145.     c_button = 1;

  146.   if ((nunchuck_buf[5] >> 2) & 1)
  147.     accel_x_axis += 2;
  148.   if ((nunchuck_buf[5] >> 3) & 1)
  149.     accel_x_axis += 1;

  150.   if ((nunchuck_buf[5] >> 4) & 1)
  151.     accel_y_axis += 2;
  152.   if ((nunchuck_buf[5] >> 5) & 1)
  153.     accel_y_axis += 1;

  154.   if ((nunchuck_buf[5] >> 6) & 1)
  155.     accel_z_axis += 2;
  156.   if ((nunchuck_buf[5] >> 7) & 1)
  157.     accel_z_axis += 1;

  158.   Serial.print(i,DEC);
  159.   Serial.print("\t");
  160.   
  161.   Serial.print("joy:");
  162.   Serial.print(joy_x_axis,DEC);
  163.   Serial.print(",");
  164.   Serial.print(joy_y_axis, DEC);
  165.   Serial.print("  \t");

  166.   Serial.print("acc:");
  167.   Serial.print(accel_x_axis, DEC);
  168.   Serial.print(",");
  169.   Serial.print(accel_y_axis, DEC);
  170.   Serial.print(",");
  171.   Serial.print(accel_z_axis, DEC);
  172.   Serial.print("\t");

  173.   Serial.print("but:");
  174.   Serial.print(z_button, DEC);
  175.   Serial.print(",");
  176.   Serial.print(c_button, DEC);

  177.   Serial.print("\r\n");  // newline
  178.   i++;
  179. }

  180. // Encode data to format that most wiimote drivers except
  181. // only needed if you use one of the regular wiimote drivers
  182. char nunchuk_decode_byte (char x)
  183. {
  184.   x = (x ^ 0x17) + 0x17;
  185.   return x;
  186. }
复制代码


果然读出了数据!!鸡腿二手的20多一个,真实廉价的传感器啊,包括游戏摇杆,2个按钮和3轴加速模块,性价比很高!!

下面是原装的代码(未测试,适用于arduino1.0.1):


  1. /*
  2. * NunchuckPrint
  3. *
  4. * 2007 Tod E. Kurt, [url]http://todbot.com/blog/[/url]
  5. *
  6. * The Wii Nunchuck reading code is taken from Windmeadow Labs
  7. *   [url]http://www.windmeadow.com/node/42[/url]
  8. */

  9. #include <Wire.h>

  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  14.   nunchuck_init(); // send the initilization handshake
  15.   Serial.print ("Finished setup\n");
  16. }

  17. void loop()
  18. {
  19.   nunchuck_get_data();
  20.   nunchuck_print_data();
  21.   delay(100);
  22. }


  23. //
  24. // Nunchuck functions
  25. //

  26. static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

  27. // Uses port C (analog in) pins as power & ground for Nunchuck
  28. static void nunchuck_setpowerpins()
  29. {
  30. #define pwrpin PORTC3
  31. #define gndpin PORTC2
  32.     DDRC |= _BV(pwrpin) | _BV(gndpin);
  33.     PORTC &=~ _BV(gndpin);
  34.     PORTC |=  _BV(pwrpin);
  35.     delay(100);  // wait for things to stabilize        
  36. }

  37. // initialize the I2C system, join the I2C bus,
  38. // and tell the nunchuck we're talking to it
  39. void nunchuck_init()
  40. {
  41.   Wire.begin();                 // join i2c bus as master
  42.   Wire.beginTransmission(0x52); // transmit to device 0x52
  43.   Wire.write(0x40);  // sends memory address
  44.   Wire.write(0x00);  // sends sent a zero.  
  45.   Wire.endTransmission(); // stop transmitting
  46. }

  47. // Send a request for data to the nunchuck
  48. // was "send_zero()"
  49. void nunchuck_send_request()
  50. {
  51.   Wire.beginTransmission(0x52); // transmit to device 0x52
  52.   Wire.write(0x00);  // sends one byte
  53.   Wire.endTransmission(); // stop transmitting
  54. }

  55. // Receive data back from the nunchuck,
  56. int nunchuck_get_data()
  57. {
  58.     int cnt=0;
  59.     Wire.requestFrom (0x52, 6); // request data from nunchuck
  60.     while (Wire.available ()) {
  61.       // receive byte as an integer
  62.       nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
  63.       cnt++;
  64.     }
  65.     nunchuck_send_request();  // send request for next data payload
  66.     // If we recieved the 6 bytes, then go print them
  67.     if (cnt >= 5) {
  68.      return 1;   // success
  69.     }
  70.     return 0; //failure
  71. }

  72. // Print the input data we have recieved
  73. // accel data is 10 bits long
  74. // so we read 8 bits, then we have to add
  75. // on the last 2 bits.  That is why I
  76. // multiply them by 2 * 2
  77. void nunchuck_print_data()
  78. {
  79.   static int i=0;
  80.   int joy_x_axis = nunchuck_buf[0];
  81.   int joy_y_axis = nunchuck_buf[1];
  82.   int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
  83.   int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
  84.   int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;

  85.   int z_button = 0;
  86.   int c_button = 0;

  87.   // byte nunchuck_buf[5] contains bits for z and c buttons
  88.   // it also contains the least significant bits for the accelerometer data
  89.   // so we have to check each bit of byte outbuf[5]
  90.   if ((nunchuck_buf[5] >> 0) & 1)
  91.     z_button = 1;
  92.   if ((nunchuck_buf[5] >> 1) & 1)
  93.     c_button = 1;

  94.   if ((nunchuck_buf[5] >> 2) & 1)
  95.     accel_x_axis += 2;
  96.   if ((nunchuck_buf[5] >> 3) & 1)
  97.     accel_x_axis += 1;

  98.   if ((nunchuck_buf[5] >> 4) & 1)
  99.     accel_y_axis += 2;
  100.   if ((nunchuck_buf[5] >> 5) & 1)
  101.     accel_y_axis += 1;

  102.   if ((nunchuck_buf[5] >> 6) & 1)
  103.     accel_z_axis += 2;
  104.   if ((nunchuck_buf[5] >> 7) & 1)
  105.     accel_z_axis += 1;

  106.   Serial.print(i,DEC);
  107.   Serial.print("\t");
  108.   
  109.   Serial.print("joy:");
  110.   Serial.print(joy_x_axis,DEC);
  111.   Serial.print(",");
  112.   Serial.print(joy_y_axis, DEC);
  113.   Serial.print("  \t");

  114.   Serial.print("acc:");
  115.   Serial.print(accel_x_axis, DEC);
  116.   Serial.print(",");
  117.   Serial.print(accel_y_axis, DEC);
  118.   Serial.print(",");
  119.   Serial.print(accel_z_axis, DEC);
  120.   Serial.print("\t");

  121.   Serial.print("but:");
  122.   Serial.print(z_button, DEC);
  123.   Serial.print(",");
  124.   Serial.print(c_button, DEC);

  125.   Serial.print("\r\n");  // newline
  126.   i++;
  127. }

  128. // Encode data to format that most wiimote drivers except
  129. // only needed if you use one of the regular wiimote drivers
  130. char nunchuk_decode_byte (char x)
  131. {
  132.   x = (x ^ 0x17) + 0x17;
  133.   return x;
  134. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2012-11-3 16:10:52 | 显示全部楼层
想做游戏的买个鸡腿不错!!比红外遥控方便多了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-3 16:28:10 | 显示全部楼层

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

这个是鸡腿的通信协议
回复 支持 反对

使用道具 举报

发表于 2012-11-3 18:04:10 | 显示全部楼层
好鸡腿
回复 支持 反对

使用道具 举报

发表于 2012-11-3 18:14:54 | 显示全部楼层
怎么都是左手控制器,连右手控制器的方法有吗
回复 支持 反对

使用道具 举报

发表于 2013-6-19 20:26:37 | 显示全部楼层
花了22买了一个山寨的,还好能用
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 17:28 , Processed in 0.037480 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表