Pergunta

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.

Foi útil?

Solução

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:

  • is there line continuity between both components, many i2c problems can come from a faulty cable or bad trace on the PCB:
    • Use a tester, or better, do a HIGH/LOW loop on the three pins you use for i2c on the arduino, and see if you can read it on the PIC side.
  • did you wire i2c correctly on both sides?
    • this may look stupid, but shit happens… Is SDA really wired to SDA, SCL to SCL?
  • is the i2c speed configured the same on both sides?
    • per default, speed is set up at 100kHz on arduino (though doublecheck your 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
    • I do not know much about PIC's side of things, but check that the speed is correctly set.
    • using the logic analyzer, check that the clock line is indeed spinning at the advertised value. As all clocks are related on an AVR, sometimes you may expect a given speed, but get only half of it because a fuse is set wrongly or some setting is wrong making the MCU's clock half speed (and all other clocks as well).
  • as I said, I don't know much about PIC, but are the pins you're using on the PIC able to cope with i2c's speed?
    • I know that on AVR, bit banged i2c communication may need to be done at a slower speed. Could it be the case for PIC?

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

Outras dicas

Have you verified that the PIC will send the serial to your PC?

Also, you can drop in code to turn on LED's at different points in your code for troubleshooting.

I know that earlier versions of MPLAB have had pretty good debugging simulators to help find code issues. I'm sure MPLABX does as well.

Have you set your configuration bits for the PIC? I don't see then in your code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top