100% found this document useful (1 vote)
2K views2 pages

Mikroc - I2C - Master and Slave

This document describes a master-slave I2C communication project between two PIC microcontrollers. The master code contains functions to send data to and read data from the slave device. The slave code initializes I2C as a slave device, and contains interrupt handling to read data from the master and send data from its own ports back to the master. The main functions continuously write received data to PORTB and send the value of PORTC back to the master.

Uploaded by

jonathan_carrera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views2 pages

Mikroc - I2C - Master and Slave

This document describes a master-slave I2C communication project between two PIC microcontrollers. The master code contains functions to send data to and read data from the slave device. The slave code initializes I2C as a slave device, and contains interrupt handling to read data from the master and send data from its own ports back to the master. The main functions continuously write received data to PORTB and send the value of PORTC back to the master.

Uploaded by

jonathan_carrera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

* 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.
}
/*
* 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){
SSPBUF = tx_data;
SSPCON.CKP = 1;
j = SSPBUF;
return;
}
if (SSPSTAT.BF == 0){
j = SSPBUF;
return;
}
//recieve data from master
if (SSPSTAT.D_A == 1){
rxbuffer = SSPBUF;
j = SSPBUF;
return;
}
}
j = SSPBUF;

//
//
//
//

Read request from master


Get data to send
Release SCL line
That's it

// all done,
// Nothing in buffer so exit

// Data [not address]


// get data
// read buffer to clear flag [address]

// read buffer to clear flag [address


]
}
//-----------------------------------------------------------------------------void main(){
Init();
while(1){
PORTB = rxbuffer;
tx_data = PORTC;
Delay_ms(Delay_Time);
}
}

You might also like