hmcosta
Joined: 26 Jun 2012 Posts: 3
|
I2C communication between Master and Slave |
Posted: Tue Jun 26, 2012 11:32 am |
|
|
Oi!
Estou programando dois microcontroladores 16F88, para comunicação I2C, o envio do master para o slave está ocorrendo normalmente, ou seja, todos dados enviados pelo master estão sendo lidos pelo slave.
Porém eu gostaria que quando o master fosse fazer alguma pergunta para o slave, o slave respondesse para o master, mais esta parte não consegui.
já li várias publicações neste fórum, porém não consegui fazer ainda.
Hi!
I am programming two microcontrollers 16f88 for I2C communication, sending from the master to the slave is taking place normally, ie, all data sent by the master are read by the slave.
But I wish that when the master has a question for the slave, the slave responds to the master, but I could not get over this part.
I've read several posts in this forum, but its still not working.
Follow my source code below:
Master:
Code: |
#include "16F88.h"
#fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
#use delay(clock=20Mhz)
#use rs232(stream=string,baud=9600, xmit=PIN_B5, rcv=PIN_B2,parity = N, bits = 8)
#define EEPROM_SDA PIN_B1
#define EEPROM_SCL PIN_B4
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
void main()
{
printf("MASTER\r\n");
while(TRUE)
{
i2c_start();
i2c_write(0xA0);
i2c_write(2);
i2c_write('I');
i2c_write('2');
i2c_write('C');
i2c_write('\n');
i2c_stop();
delay_ms(10);
}
}
|
Slave:
Code: |
#include "16F88.h"
#fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT,PUT
#use delay(clock=20Mhz)
#use rs232(stream=string,baud=9600, xmit=PIN_B5, rcv=PIN_B2,parity = N, bits = 8)
#define EEPROM_SDA PIN_B1
#define EEPROM_SCL PIN_B4
#use i2c(slave, sda=EEPROM_SDA, scl=EEPROM_SCL,address=0xA0)
void main()
{
char data;
char buffer_I2C[10];
int i=0;
printf("SLAVE\r\n");
while(TRUE)
{
if(i2c_poll() )
{
data=i2c_read();
if(data != -96)
{
if(data == '\n')
{
if(buffer_I2C[0] == 2)
{
buffer_I2C[i]='\0';
printf("Message - %s\r\n",buffer_I2C);
}
}
else
{
buffer_I2C[i]=data;
i++;
}
}
else
{
i=0;
}
}
}
} |
_________________ Att.
Heverton Costa |
|