I am trying to connect Arduino Uno to PIC18F4620 using I2C protocol,
i wrote this code for Arduino
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Wire.beginTransmission(0xA0);
Wire.write(2);
Wire.write('I');
Wire.write('2');
Wire.write('C');
Wire.write('\n');
Wire.endTransmission();
Serial.println("Send to I2C Slave 0xA0");
delay(1000);
}
and this code for PIC18F4620 using CCS Compiler :
#include <slave.h>
#use rs232(stream=string,baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity = N, bits = 8)
#define P_SDA PIN_C4
#define P_SCL PIN_C3
#use i2c(slave, sda=P_SDA, scl=P_SCL,address=0xA0)
void main()
{
char data;
char buffer_I2C[10];
int i=0;
printf("SLAVE\r\n");
while(TRUE)
{
if(i2c_poll())
{
data = i2c_read();
printf("%d \r\n" ,data);
if(data != -96)
{
if(data == '\n')
{
if(buffer_I2C[0] == 2)
{
buffer_I2C[i]='\0';
printf("Message - %s\r\n",buffer_I2C);
}
}
else
{
buffer_I2C[i]=data;
i++;
}
}
else
{
i=0;
}
}
}
}
the problem is that the PIC does not respond to Arduino, i mean when the arduino send data to PIC, the PIC does not receive any thing,
what is the problem in this code??
Thanks in advance.
Well, first thing I'd advise you to get a logic analyzer, such as the logic
or the openbench
or the buspirate
or an arduino based one (or any other more expensive options that can interpret i2c
).
Then, you'll see if there is no electronic problem.
There are a few parameters you should test:
SDA
really wired to SDA
, SCL
to SCL
?twi.c
file). You can change that setting to higher speed by setting it to 400kHz before including Wire.h
, i.e.: #define TWI_FREQ 400000L
if none of what I said works, then I guess it's time for you do determine which of the devices is mute or deaf, from an i2c perspective…
Then, try to use both of your boards with another i2c device. i.e., try to communicate with your computer with the PIC board, either by using a buspirate, an i2c dongle or your display connector and then use i2cdetect
to play with it.
On the other end, try to connect to any kind of industrial i2c component using your Arduino. Knowing the one that does not work will help you investigate which one is not working, or misbehaving.
And finally, if you're comfortable enough with software engineering, I advice you a lot to use the i2cdev
library, which actually makes a decent abstraction on top of the flawed Arduino implementation.
HTH