Hello and Hi. I'm new to I2C and had been searching around for some code. What I want to do is to make the Wiimote able to get data from the PIC by making the PIC act like the Nunchuk. The original project was done here **broken link removed**. My intention is to finally create the firefighting robot **broken link removed**, but I want to start from scratch to understand everything.
Currently this is the code that I have come up with. It is based on Mike Alon's code Microchip PIC micros and C - source and sample code. I've modified to the best of my current knowledge to mimic the original code, which uses Arduino.
This code I've tried several times with several modifications with no success so far. To detect if the PIC is sending data to the Wiimote, I used a Wiimote Driver (Bian Peek's WiimoteLib). If it works, then it should be indicated on the driver's UI.
I'm not entirely sure if my approach is correct. There are several things I am still unsure of, like:
1. in the original project, the red wire(3.3V ) goes to AREF. I connect mine to the ground. Is that correct? I only did so because otherwise connecting the I2C port to the Wiimote crashes the driver.
The rest of code related thinsg I do not understand are commented in the codes. The PIC runs a s master
Currently this is the code that I have come up with. It is based on Mike Alon's code Microchip PIC micros and C - source and sample code. I've modified to the best of my current knowledge to mimic the original code, which uses Arduino.
This code I've tried several times with several modifications with no success so far. To detect if the PIC is sending data to the Wiimote, I used a Wiimote Driver (Bian Peek's WiimoteLib). If it works, then it should be indicated on the driver's UI.
I'm not entirely sure if my approach is correct. There are several things I am still unsure of, like:
1. in the original project, the red wire(3.3V ) goes to AREF. I connect mine to the ground. Is that correct? I only did so because otherwise connecting the I2C port to the Wiimote crashes the driver.
The rest of code related thinsg I do not understand are commented in the codes. The PIC runs a s master
Code:
/******************************************************************************************/
#include <pic.h>
#include "Delay.h"
#include "Delay.C"
#include "877_i2c.h"
__CONFIG ( 0x3F32 ); //configuration for the microcontroller, is this necessary?
uint8_t outbuf[6]; //buffer to send to Wiimote, need to be send 6 bytes at a time
//original Mike Alon code, for reference
void write_ext_eeprom(unsigned char address, unsigned char data)
{
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_write(data);
i2c_stop();
DelayMs(11);
}
/******************************************************************************************/
//original Mike Alon code, for reference
unsigned char read_ext_eeprom(unsigned char address)
{
unsigned char data;
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_repStart();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
/******************************************************************************************/
//this is where it should send data to the Wiimote
void write_six_bytes(unsigned char address)
{
i2c_start();
i2c_write(0xa0);//don't understand this part, is it to initialise the connection?
i2c_write(address);
//i2c_write(outbuf);//I'm not sure if should send the entire outbuf in one go
i2c_write(outbuf[0]);//or outbuf[0],1,2...somehow trying both didn't work
i2c_write(outbuf[1]);
i2c_write(outbuf[2]);
i2c_write(outbuf[3]);
i2c_write(outbuf[4]);
i2c_write(outbuf[5]);
i2c_stop();
DelayMs(11);
}
/******************************************************************************************/
void init_nunchuk ()
{
// outbuf[0] = nunchuk_encode_byte (125); // joystick X
// outbuf[1] = nunchuk_encode_byte (126); // joystick Y
// outbuf[2] = nunchuk_encode_byte (227); // Axis X
// outbuf[3] = nunchuk_encode_byte (241); // Axis Y
// outbuf[4] = nunchuk_encode_byte (140); // Axis Z
// outbuf[5] = nunchuk_encode_byte (1); // Press C button, byte[5] is buttons
//C,Z and accelaration data
//outbuf[5] = nunchuk_encode_byte(2); // Press Z button
//outbuf[5] = nunchuk_encode_byte(0); // Press Z and C button
//the byte allocation without the encryption
outbuf[0] = 125
outbuf[1] = 126
outbuf[2] = 227
outbuf[3] = 241
outbuf[4] = 140
outbuf[5] = 1
}
/******************************************************************************************/
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_encode_byte (char x)
{
x = x - 0x17;
x = (x ^ 0x17);
return x;
}
/******************************************************************************************/
void main()
{
unsigned char i=0;
PORTB=0x00; // port
PORTC=0x00; // port
//setup ADC
ADCON1 = 0b00000110;
TRISB=0x00; //portb out
TRISC=0x00; //portc out
i2c_init(); // init i2c
init_nunchuk();
while(1)
{
//PORTB=i; //
write_six_bytes(0xA4); //
DelayMs(255);
}
}
/******************************************************************************************/