master code:
Code: Select all
void send(unsigned short send_data, int dir) // function to send data
{
I2C1_Init(100000);
I2C1_Start();
I2C1_Wr(dir);
I2C1_Wr(send_data);
I2C1_Stop();
}
unsigned int band =0;
unsigned short cont =0;
unsigned short cont2 =0;
void main()
{
while(1)
{
if((portb.b0==1) && (band == 1)) // condition to send data to slave 1
{
send(100+cont, 0xa0);
cont++;
band=0;
}
if((portb.b2==1) && (band== 1)) //condition to send data to slave 2
{
send(10+cont2, 0xaa);
cont2++;
band=0;
}
if(portb.b1==1) // condition that enables sending data
{
band=1;
}
}
}
slave 1 code:
Code: Select all
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
const Addy = 0xa0; // Slave address 1
unsigned short j; // just dummy for buffer read
unsigned short rxbuffer;
char text[20];
void Init()
{
ADCON1 = 0; // All ports set to digital
trisc.b3=1; // set as input
trisc.b4=1; // set as input
SSPADD = Addy; // Get address (7bit). Lsb is read/write flag
SSPCON = 0x3e; // Set to I2C slave with 7-bit address
PIE1.SSPIE = 1; // enable SSP interrupts
INTCON = 0xC0; // enable INTCON.GIE
}
void interrupt() // I2C slave interrupt handler
{
if (PIR1.SSPIF == 1) // I2C Interrupt
{
if (SSPSTAT.BF == 0) // Transmission completed
{
if(sspcon.sspov == 0) // without overflow
{
if(sspstat.d_a == 1) // Data [not address]
{
rxbuffer = sspbuf; // get data
PIR1.SSPIF = 0; // reset SSP interrupt flag
}
}
}
}
j = SSPBUF; // read buffer to clear flag [address]
return;
}
void main()
{
Init();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
lcd_out(1,1,"Esclavo 1");
delay_ms(250);
while(1)
{
shorttostr(rxbuffer,text);
lcd_out(2,1,text);
Delay_ms(250);
}
}
slave 2 code:
Code: Select all
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
const Addy = 0xaa; // Slave address 2
unsigned short j; // just dummy for buffer read
unsigned short rxbuffer;
char text[20];
void Init()
{
ADCON1 = 0; // All ports set to digital
trisc.b3=1; // set as input
trisc.b4=1; // set as input
SSPADD = Addy; // Get address (7bit). Lsb is read/write flag
SSPCON = 0x3e; // Set to I2C slave with 7-bit address
PIE1.SSPIE = 1; // enable SSP interrupts
INTCON = 0xC0; // enable INTCON.GIE
}
void interrupt() // I2C slave interrupt handler
{
if (PIR1.SSPIF == 1) // I2C Interrupt
{
if (SSPSTAT.BF == 0) // Transmission completed
{
if(sspcon.sspov == 0) // without overflow
{
if(sspstat.d_a == 1) // Data [not address]
{
rxbuffer = sspbuf; // get data
PIR1.SSPIF = 0; // reset SSP interrupt flag
}
}
}
}
j = SSPBUF; // read buffer to clear flag [address]
return;
}
void main()
{
Init();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
lcd_out(1,1,"Esclavo 1");
delay_ms(250);
while(1)
{
shorttostr(rxbuffer,text);
lcd_out(2,1,text);
Delay_ms(250);
}
}
Infinitely thanking .... :D