Arduino and PIC18f2431 I2c/TWI

Hi,

I am currently trying to implement I2C(TWI) on a project I am currently working on.I want the master to be a PIC18F2431 and the slave to be an ATMEGA328P (Arduino Pro mini 3.3). I am starting completely from scratch, can anyone please share advice or point me in the direction of the correct resources I need in order to successfully implement the I2C protocol?

I understand the gist of I2C(TWI) I have the the PIC18 and the Atmega wired up such that SCl -> SCL and SDA -> SDA , both on same PCB ( these are the only 2 devices on the PCB so address pinging is not needed). The coding portion is where I feel completely lost.

The Goal:

I want to be able to push a button (contolled by PIC18) and send the signal to the slave (atmega) via SDA the atmega would then read the correct value and display it on an OLED screen.

Thanks in advance!

PCB-TRONICS:
Hi,

I am currently trying to implement I2C(TWI) on a project I am currently working on.I want the master to be a PIC18F2431 and the slave to be an ATMEGA328P (Arduino Pro mini 3.3). I am starting completely from scratch, can anyone please share advice or point me in the direction of the correct resources I need in order to successfully implement the I2C protocol?

I understand the gist of I2C(TWI) I have the the PIC18 and the Atmega wired up such that SCl -> SCL and SDA -> SDA , both on same PCB ( these are the only 2 devices on the PCB so address pinging is not needed). The coding portion is where I feel completely lost.

The Goal:

I want to be able to push a button (contolled by PIC18) and send the signal to the slave (atmega) via SDA the atmega would then read the correct value and display it on an OLED screen.

Thanks in advance!

Simple I2C Arduino SLAVE code:

#include <Wire.h>

struct DATASTRUCT{
   int integer1;
   float float1;
};

volatile DATASTRUCT data; // volatile because it will be access during a interrupt.



void onRequestEvent(){ //callback for Slave I2C read request

Wire.write((uint8_t*)&data,sizeof(data)); // send it as one block

}


void onReceiveEvent(int howMany){
uint8_t * bp; // byte point to allow easy access to fillup data struct
bp = (uint8_t *)&data;
if(howMany==sizeof(data)){
  for(uint8_t i = 0;i<sizeof(data);i++){
    bp[i]=Wire.read();
    }
  }
}
#define slaveID 8

void setup(){
Wire.begin(slaveID); // slaveID should be >7 and Lessthan 120. not enforced
Wire.onReceive(&onReceiveEvent);
Wire.onRequest(&onRequestEvent);
}

void loop(){}

This code is simple, and does not change the values in data, it will respond to master read and write request to store values in the data structure.
if you wanted to write a value to the slave and have the slave respond to that data I would create a flag that is set in the onReceiveEvent() and check the flag in the loop() code.

something like this:

volatile bool newMessage=false;

void onReceiveEvent( int howMany){
// receive into data struct

newMessage=true;
}

void loop(){
if(newMessage){
   // process message
   newMessage = false;
  }
}

Chuck.