Problems connecting PIC 18F4550 and Arduino Mega

Hi everyone,

I'm having issues trying to communicate my PIC18F4550 as Slave with my Arduino Mega as Master over I2C. The idea is to read a byte over Arduino Serial and send it to turn on and off a LED.
Please if anyone can help, I will seriously appreciate it.

Here is my PIC code:
#include <18f4550.h>
#fuses HS, WDT, NOPROTECT, NOPUT, NOPBADEN
#use delay (crystal = 4000000)
#use i2c(SLAVE, SDA = PIN_B0, SLOW, SCL = PIN_B1, NOFORCE_SW, ADDRESS = 0xA0)

char data = 0;

void main() {
set_tris_b(0xFF);
set_tris_c(0x00);
while(1){
if(i2c_poll()){
data = i2c_read(2);
if(data == 1) output_high(PIN_C0);
else if(data == 0) output_low(PIN_C0);
}
}
}

Im using CCS as IDE.

Here's my Arduino code:

#include <Wire.h>

char data = -1;

void serialEvent() {
data = Serial.read();
Serial.println(data);
}

void setup() {
Serial.begin(9600);
Wire.begin();
}

void loop() {
if(data > -1){
Wire.beginTransmission(0xA0);
Wire.write(data);
Wire.endTransmission();
data = -1;
}
}

Here's my circuit, including some I2C debug info:

Thanks in advance!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.