View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 22, 2007 2:11 am |
|
|
Try the test program shown below. You should get output like this:
Quote: |
7b 7c 8a 67 69 eb
7b 7c 8a 67 69 eb
7b 7c 8a 67 69 fb
7b 7c 8a 67 69 eb
7b 7c 8a 67 69 eb
7b 7c 8a 67 69 eb
|
The code below uses software i2c on pins B4 and B5. Change the
pins to in the #use i2c() statement to match your own hardware.
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_B5, scl=PIN_B4)
#define NUNCHUCK_I2C_WRITE_ADDR 0xA4
#define NUNCHUCK_I2C_READ_ADDR 0xA5
void nunchuck_init(void)
{
i2c_start();
i2c_write(NUNCHUCK_I2C_WRITE_ADDR);
i2c_write(0x40);
i2c_write(0x00);
i2c_stop();
}
//---------------------------------
void nunchuck_read_data(int8 *buffer)
{
i2c_start();
i2c_write(NUNCHUCK_I2C_WRITE_ADDR);
i2c_write(0);
i2c_stop();
delay_ms(1); // This delay is necessary.
i2c_start();
i2c_write(NUNCHUCK_I2C_READ_ADDR);
buffer[0] = i2c_read();
buffer[1] = i2c_read();
buffer[2] = i2c_read();
buffer[3] = i2c_read();
buffer[4] = i2c_read();
buffer[5] = i2c_read(0);
i2c_stop();
}
// This routine was only used to find the i2c address
// of the Nunchuck.
int8 get_ack_status(int8 address)
{
int8 status;
i2c_start();
status = i2c_write(address);
i2c_stop();
return(status);
}
//===================================
void main()
{
int8 i;
int8 buffer[6] = {0};
// This code is only used to find the i2c address of
// the Nunchuck. It is 0xA4 (in CCS "byte" format).
//for(i=0x50; i < 0xF0; i+=2)
// {
// printf("%x %x \n\r", i, get_ack_status(i));
// delay_ms(100);
// }
nunchuck_init();
delay_ms(100);
while(1)
{
nunchuck_read_data(buffer);
for(i = 0; i < sizeof(buffer); i++)
printf("%x ", buffer[i]);
printf("\n\r");
delay_ms(500);
}
} |
|
|
|
kd5uzz
Joined: 28 May 2006 Posts: 56
|
|
Posted: Thu Nov 22, 2007 9:24 pm |
|
|
All I'm getting is 'ff ff ff ff ff ff'. I'm guessing you are running this with a wii nunchuck and it works? Maybe I'm hooking mine up wrong. I'm trying to avoid cutting the connector off, and am not sure I've got the connections correct. I'm not at the lab right now, I'm access the PC via VNC (and turning the hardware on/off via software, makes remote dev easy.) so I'm not able to look at it right now. Happy Turkey Day! |
|
|
kd5uzz
Joined: 28 May 2006 Posts: 56
|
|
Posted: Thu Nov 22, 2007 9:26 pm |
|
|
I should mention I changed your code just a bit, I changed the baud and clock, as well as the i2c pins. I could have the i2c pins wrong, I changed them the other day just to see if I had them wrong and I may have left them that way. I won't be able to make it to the lab to check until Sat. Thanks for the help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 22, 2007 9:38 pm |
|
|
Yes, I cut the connector off. There are four wires and one ground
sheath. I connected the wires per the instructions on the link that
you posted. I didn't connect the ground braid to anything.
I am also running the unit at +3.1 volts. The reason for that voltage
is because I don't have a 3.3v regulator at my current location. So
I'm using three 1N914A diodes in series with the +5v to drop the
Nunchuck's voltage down to the 3v range. On the website, they say
that the unit is supposed to run at 3.3v and they wonder if running
it at +5v will shorten it's life. Yes, I sure think it would.
The unit apparently has internal pullups for SCL and SDA. You
don't need to add external pullups. Because it's running at 3.1v,
I'm using software i2c on Port B pins. Those pins have TTL input
levels, so they'll work fine with a 3v i2c device. The i2c pins on
Port C have Schmitt Trigger inputs, so they require 4v input levels.
They can't work with 3v i2c. It has to be 5v i2c. That's what they're
using on the website.
You've already gone through two units. I suggest you cut off the
connector and get it running. |
|
|
kd5uzz
Joined: 28 May 2006 Posts: 56
|
|
Posted: Mon Nov 26, 2007 11:36 am |
|
|
It works! Thanks PCM Programmer. I had in fact swapped the SCL and SDA lines just before break (and before you posted your code). Thanks again!
-Daniel |
|
|
sodb
Joined: 30 Nov 2007 Posts: 1
|
|
Posted: Fri Nov 30, 2007 5:27 pm |
|
|
Thank guys. I was trying the same thing for a few weeks and just got all 0xff back from the nunchuck. I use a 18f242 running on 3V. Using the correct address 0xA4 it works like a dream. This is just an intermediate step in connecting the nunchuck g-sensor to a Lego Mindstorm NXT (internally using also i2c). Using the PIC I at least know that the nunchuck & I2C is working as it should. |
|
|
joven
Joined: 30 Jan 2007 Posts: 56 Location: Viana do Castelo - Portugal
|
|
Posted: Mon Dec 22, 2008 10:13 am |
|
|
Did you need to decode the data received or not?
|
|
|
Kova
Joined: 06 May 2007 Posts: 35
|
|
Posted: Fri May 29, 2009 11:58 pm |
|
|
PCM programmer wrote: | You've already gone through two units. I suggest you cut off the
connector and get it running. |
Hi,
I have resumed this thread for a question.
I want to emulate the nunchuck or the classic wii controller.
I'm explaining better.
This is my goal:
I want to connect a interface with a PIC to Wiimote:
Wiimote <--> PIC interface (for example it emulates only the A key).
This interface will be able to emulate, for example, the A wii classic controller key.
I have readed all documentation about the protocol, but I don't understand very well how can I do that.
Many articles explain how to connect the wii-controllers (nunchuck-classic controll) to a PC or a MCU (for example arduino).
Somebody can help me?
What is the initial handshaking?
My interface will be a slave in the I2C communication, right?
After this:
The communication between the Wiimote and the Extension Controllers is 400kHz "fast" I2C, with the slave address 0x52.
What are the other packets/reports that I have to send?
A pseudo-code also will be great.
Thanks a lot for all.
Bye guys . |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 30, 2009 1:25 am |
|
|
You have dreamed up a project, but then you propose that we should
research the protocol and write an outline of the code for you.
Shouldn't you be the one to do this ?
|
|
|
Kova
Joined: 06 May 2007 Posts: 35
|
|
Posted: Sat May 30, 2009 2:14 am |
|
|
PCM programmer wrote: | Shouldn't you be the one to do this ?
|
Naaa, It's not so.
I want only to know what are the "packets" that wiimote <--> interface have to send/receive.
The code is not a problem.
If you say me these:
Wiimote send 0xff at 0xA4 address
Interface send 0x01 at 0xA4.. etc.
for me will be enough
I have readed the documentation but many docs say only the packet that wiimote sends when a controller is plugged, but I haven't readed nothing about the response given by the nunchuck/wii classic (the handshaking)
Am I blind?
;)
Bye |
|
|
ricard0g0mes
Joined: 19 May 2009 Posts: 6
|
|
Posted: Tue Jul 21, 2009 1:57 am |
|
|
hello,
I'm trying to use this code but my project works at 20Mhz.
Do I need to change it to a lower value to work with i2c?
How can I define the speed of the i2c communication?
My pic is a 18f4550.
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 21, 2009 11:00 am |
|
|
Just set the oscillator #fuses statement to HS (instead of XT) and set
the #use delay() statement to 20 MHz.
If you do that, CCS will automatically use the same speed for the i2c
clock (or as close as possible). |
|
|
btklister
Joined: 16 Jul 2009 Posts: 14
|
|
Posted: Sat Jun 26, 2010 7:28 pm |
|
|
The code posted in the previous page works for a Chinese wireless nunchuk? The addresses are the same? |
|
|
|