# Bus I2C y lenguaje "C", no se resolver el codigo



## electrodo (Mar 12, 2009)

Hola a todos.

tengo problemas para comunicar dos pic 16f887 con el bus I2C, lo que quiero hacer es de lo mas sencillo, ingresar un dato por un puerto, y leerlo en el otro pic desde algun otro puerto.
el problema es que creo entender como trabaj el protocolo, pero no consigo llevarlo a la line de codigo... 

si alguien puede darme una ayuda les estaria muy agradecido. los codigos que utilizo son:


MASTER:

```
#include main.h

void main()
{
int result;

   SET_TRIS_A (0xff);
   SET_TRIS_b (0x00);
   SET_TRIS_d (0xff);
      
      
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!


pto_b= 0;

while(TRUE)
   {
      
      if(input(PIN_D0))    //read inputs when PIN d0 is depressed
      {
         delay_ms(10);
         i2c_start ();           //begin communication
         i2c_write (0xa0);       //send slave address
         i2c_write (0x05);       //request slave internal memory address for data
         i2c_stop();             //stop write cycle to shift to read cycle
         i2c_start ();           //send repeated start command to begin read cycle
         i2c_write (0xa1);       //add 1 to the address to send a write bit
         result = i2c_read(0);   //read analogue informaciónrmation from the slave
         i2c_stop ();            //terminate communication
         pto_b=result;           //display analogue informaciónrmation from the slave
      }
      while(!input(PIN_D0))      //read inputs when PIN d0 is depressed
      {
         delay_ms(10);
         i2c_start ();           //begin communication
         i2c_write (0xa0);       //send slave address
         i2c_write(0x06);        //posicion del buffer 
         i2c_write (0xaa);       //request slave internal memory address for data
         i2c_stop();             //stop write cycle to shift to read cycle
      }
   }
}
```



SLAVE:


```
#include main.h

//BYTE address, buffer[0x10];

int *address,

SET_TRIS_A (0xff)
SET_TRIS_B (0x00)


#INT_SSP
  void ssp_interupt ()
  {
     state = i2c_isr_state();
     if(state < 0xa1)                 //master is sending data
     {
       if(state == 0)
        {
        }
        if(state == 1)                   //first received byte is address
        {
           address = i2c_read();
        }
        if(state == 2)                   //second received byte is data
        {
           *address = i2c_read();
        }
     }

     if(state == 0xa1)                //master is requesting data
     {
        i2c_write (*address);  //send requested data
     }

  }

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!



   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

   while (TRUE) {
   delay_ms(10);
   pto_b= 0x00;
   delay_ms(10);
   pto_b=0xff;
   }
}
```


estos condigos fueron creados en ccs, y montados simulados sobre proteus. pero no andan!

los archivos que envio, pertenecen a los codigos aqui expuestos, y al fichero de proteus.

Por favor, si alguien pudiera ayudarme, estaria agradecido.


----------

