CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

I2C communication between Master and Slave

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmcosta



Joined: 26 Jun 2012
Posts: 3

View user's profile Send private message

I2C communication between Master and Slave
PostPosted: Tue Jun 26, 2012 11:32 am     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 26, 2012 3:36 pm     Reply with quote

Here is an example of an i2c slave, where the slave PIC reads an A/D
value, and the Master PIC requests it from the slave:
http://www.ccsinfo.com/forum/viewtopic.php?t=39242&start=6
hmcosta



Joined: 26 Jun 2012
Posts: 3

View user's profile Send private message

PostPosted: Sat Jun 30, 2012 11:40 am     Reply with quote

Thank you!!
with this topic can take away all my doubts.
_________________
Att.
Heverton Costa
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group