PIC 2 PIC Master/Slave Communication using I2C... Enjoy
Posted: 06 Dec 2006 05:04
Hi all, here is some simple code for Master/Slave I2C projects. I can't take all of the credit for writing this. I used mB and mP code/methods that I found on the forum and translated it into this. Feel free to comment, but I might not be quick to respond because I only get on here about 1-2 times a week.
Here's the Master code
Here's the Slave code
Enjoy,
Tom
Here's the Master code
Code: Select all
/*
* Project name:
Master
* Description:
Master control for PIC 2 PIC I2C Communication
* Configuration:
MCU: PIC16F876A
SW: mikroC v6.0
* NOTES:
*/
//------------------------------------------------------------------------------
const Slave_Addy = 0x69;
void send(unsigned short send_data){
I2C_Init(100000);
I2C_Start();
I2C_Wr(Slave_Addy);
I2C_Wr(send_data);
I2C_Stop();
}
unsigned short read(){
unsigned short read_data;
I2C_Init(100000);
I2C_Start();
I2C_Wr(Slave_Addy + 1);
read_data = I2C_Rd(0);
delay_ms(100);
I2C_Stop();
return read_data;
}
void main(){
// Main program code place here.
// Use sub-routine "send" to send data to slave.
// Use "read" to read data from slave.
}
Code: Select all
/*
* Project name:
Slave
* Description:
Slave code for PIC 2 PIC I2C Communication
Write data from Master to PORTB
Send PORTC data to Master
* Configuration:
MCU: PIC16F876A
SW: mikroC v6.0
* NOTES:
*/
//------------------------------------------------------------------------------
const Addy = 0x69; // set I2C device address
const Delay_Time = 250; // port check delay
//------------------------------------------------------------------------------
// Global Processing Variables
//------------------------------------------------------------------------------
unsigned short j; // just dummy for buffer read
unsigned short rxbuffer; //
unsigned short tx_data; //
//------------------------------------------------------------------------------
void Init(){
ADCON1 = 0; // All ports set to digital
TRISA = 0; // Set PORTA as output
TRISB = 0; // Set PORTB as output
TRISC = 0xFF; // Set PORTC as input
SSPADD = Addy; // Get address (7bit). Lsb is read/write flag
SSPCON = 0x36; // Set to I2C slave with 7-bit address
PIE1.SSPIF = 1; // enable SSP interrupts
INTCON = 0xC0; // enable INTCON.GIE
}
//------------------------------------------------------------------------------
void interrupt(){ // I2C slave interrupt handler
if (PIR1.SSPIF == 1){ // I2C Interrupt
PIR1.SSPIF = 0; // reset SSP interrupt flag
//transmit data to master
if (SSPSTAT.R_W == 1){ // Read request from master
SSPBUF = tx_data; // Get data to send
SSPCON.CKP = 1; // Release SCL line
j = SSPBUF; // That's it
return;
}
if (SSPSTAT.BF == 0){ // all done,
j = SSPBUF; // Nothing in buffer so exit
return;
}
//recieve data from master
if (SSPSTAT.D_A == 1){ // Data [not address]
rxbuffer = SSPBUF; // get data
j = SSPBUF; // read buffer to clear flag [address]
return;
}
}
j = SSPBUF; // read buffer to clear flag [address]
}
//------------------------------------------------------------------------------
void main(){
Init();
while(1){
PORTB = rxbuffer;
tx_data = PORTC;
Delay_ms(Delay_Time);
}
}
Tom