Autor Tema: Ayuda i2c enviar bytes  (Leído 1862 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado erickkelvin

  • PIC10
  • *
  • Mensajes: 8
Ayuda i2c enviar bytes
« en: 01 de Marzo de 2013, 14:52:55 »
 :oops: Hola tengo una duda sobre i2c, se como enviar y recibir 1 dato pero tengo problemas al tratar de escribir 3 int byte[], quiero enviar por ejemplo:

byte byteA[8]={0,0,0,0,0,0,0,0};
byte byteB[8]={0,0,0,0,0,0,0,0};
byte byteC[8]={0,0,0,0,0,0,0,0};

y nose como hacerlo, gracias por su ayuda u.u, estoy usando 2 pic 16f887, para enviar y recibir u.u
« Última modificación: 01 de Marzo de 2013, 15:12:34 por erickkelvin »

Desconectado RICHI777

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1498
Re: Ayuda i2c enviar bytes
« Respuesta #1 en: 01 de Marzo de 2013, 14:58:43 »
Hola,

Código: C
  1. char Counter = sizeof( byteA );
  2.  
  3. for ( Counter = 0; Counter < sizeof( byteA); Counter ++ )
  4. {
  5.   send_to_i2c( byteA[ Counter ] );
  6. }
send_to_i2c debes reemplazarlo por la función que usas para transmitir un byte al i2c.

No entiendo la declaración de los arrays, int byte

Saludos !



Desconectado erickkelvin

  • PIC10
  • *
  • Mensajes: 8
Re: Ayuda i2c enviar bytes
« Respuesta #2 en: 01 de Marzo de 2013, 15:11:53 »
ops no me di cuenta de lo que escribir, solo es byte byteA,  y asi sucesivamente, lo corrijo.
Entonces este codigo lo usaria 3 veces seguidas (?)

Código: [Seleccionar]
void envio_I2C()
   {   
      i2c_start();         //Comienzo de la comunicación I2C ...
      i2c_write(0xa0);     //...con la dirección del PIC esclavo...
      for ( Counter = 0; Counter < sizeof( byteA); Counter ++ )
          { i2c_write( byteA[ Counter ] );}
      for ( Counter = 0; Counter < sizeof( byteB); Counter ++ )
          { i2c_write( byteC[ Counter ] );}
      for ( Counter = 0; Counter < sizeof( byteC); Counter ++ )
          { i2c_write( byteB[ Counter ] );}
      i2c_stop();          //Finalización de la transmisión     
  }

asi? o cada uno empezando y parando la transmision de datos
« Última modificación: 01 de Marzo de 2013, 15:17:11 por erickkelvin »

Desconectado RICHI777

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1498
Re: Ayuda i2c enviar bytes
« Respuesta #3 en: 01 de Marzo de 2013, 15:17:28 »
Hola, si o bien podrías usar una función que reciba el address del array y el tamaño, y no repetís tanto los for.

Saludos !

Desconectado erickkelvin

  • PIC10
  • *
  • Mensajes: 8
Re: Ayuda i2c enviar bytes
« Respuesta #4 en: 01 de Marzo de 2013, 15:24:58 »
Hola, si o bien podrías usar una función que reciba el address del array y el tamaño, y no repetís tanto los for.

Saludos !

Gracias por la pronta respuesta, si no fuera molestia, en el caso de la recepcion seria asi?
Código: [Seleccionar]
byte byteA[8]={0,0,0,0,0,0,0,0};
byte byteB[8]={0,0,0,0,0,0,0,0};
byte byteC[8]={0,0,0,0,0,0,0,0};

void main()
{

   int dato;
   lcd_init(); 
   
   while (1)
   {
   
      if(i2c_poll()) {
         byteA[]=i2c_read();
         byteB[]=i2c_read();
         byteC[]=i2c_read();
         }
   }
}

Desconectado RICHI777

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1498
Re: Ayuda i2c enviar bytes
« Respuesta #5 en: 01 de Marzo de 2013, 15:29:52 »
Hola, cuando coloques código con code, colacale code=c lo formatea mejor ...

no, tenes que indicarle en que posición del array vas a almacenar.

Código: C
  1. if(i2c_poll())
  2. {
  3.   byteA[ 0 ]=i2c_read();
  4.   byteA[ 1 ]=i2c_read();
  5.   byteA[ 2 ]=i2c_read();
  6. }

Saludos !

Desconectado erickkelvin

  • PIC10
  • *
  • Mensajes: 8
Re: Ayuda i2c enviar bytes
« Respuesta #6 en: 01 de Marzo de 2013, 15:51:05 »
Uhmm estoy simulando y al parecer con el i2c parece que si envia los datos pero el segundo pic no los recibe, pongo mis codigos :S

Código: C
  1. #include <16F887.h>
  2. #FUSES NOWDT, hs, put
  3. #use delay (clock = 20000000)
  4. #use i2c(MASTER, SDA=PIN_C4, SLOW, SCL=PIN_C3)
  5. #use standard_io(a)
  6. #use standard_io(b)
  7. #use standard_io(c)
  8. #use standard_io(d)
  9. #use standard_io(e)
  10.  
  11. int a=1;
  12. byte byteA[8]={1,0,1,0,1,0,1,0};
  13. byte byteB[8]={0,1,0,1,1,1,1,0};
  14. byte byteC[8]={0,1,0,1,0,1,0,0};
  15. char CounterA = sizeof( byteA );
  16. char CounterB = sizeof( byteB );
  17. char CounterC = sizeof( byteC );
  18. int tecla;
  19.  
  20. /******************************************************************************/
  21. /***************************** Envío I2C **************************************/
  22. void in_out();
  23. void variables();
  24. void key();
  25.  
  26. void envio_I2C()
  27.    {  
  28.       i2c_start();         //Comienzo de la comunicación I2C ...
  29.       i2c_write(0xa0);     //...con la dirección del PIC esclavo...
  30.       //i2c_write(tecla);
  31.       for ( CounterA = 0; CounterA < sizeof( byteA); CounterA ++ )
  32.           { i2c_write( byteA[ CounterA ] );}
  33.       for ( CounterB = 0; CounterB < sizeof( byteB); CounterB ++ )
  34.           { i2c_write( byteC[ CounterB ] );}
  35.       for ( CounterC = 0; CounterC < sizeof( byteC); CounterC ++ )
  36.           { i2c_write( byteB[ CounterC ] );}
  37.       i2c_stop();          //Finalización de la transmisión      
  38.   }
  39.  
  40. /******************************************************************************/
  41. /*************************** FUNCIÓN PRINCIPAL ********************************/
  42.  
  43. void main()
  44. {
  45.    //enable_interrupts(global);
  46.    //enable_interrupts(int_rda);
  47.    while(true)
  48.    {
  49.       if (input(pin_a0)==1)
  50.       {
  51.  
  52.          envio_I2C();
  53.       }      
  54.    }
  55. }

Código: C
  1. #include <16F887.h>
  2. #FUSES NOWDT, hs, put
  3. #use delay (clock = 20000000)
  4. #use I2C(SLAVE, SDA=PIN_C4 ,SLOW, SCL=PIN_C3, ADDRESS=0xa0)
  5. #use standard_io(a)
  6. #use standard_io(b)
  7. #use standard_io(c)
  8. #use standard_io(d)
  9. #use standard_io(e)
  10.  
  11. int a=0;
  12.  
  13. byte byteA[8]={1,1,1,0,0,0,0,0};
  14. byte byteB[8]={0,0,0,0,0,0,0,0};
  15. byte byteC[8]={1,0,0,0,0,0,0,0};
  16.  
  17. void main()
  18. {
  19.    while (true)
  20.    {
  21.    
  22.       if(i2c_poll())
  23.       {
  24.          byteA[ 0 ]=i2c_read();
  25.          byteA[ 1 ]=i2c_read();
  26.          byteA[ 2 ]=i2c_read();
  27.          byteA[ 3 ]=i2c_read();
  28.          byteA[ 4 ]=i2c_read();
  29.          byteA[ 5 ]=i2c_read();
  30.          byteA[ 6 ]=i2c_read();
  31.          byteA[ 7 ]=i2c_read();
  32.          byteB[ 0 ]=i2c_read();
  33.          byteB[ 1 ]=i2c_read();
  34.          byteB[ 2 ]=i2c_read();
  35.          byteB[ 3 ]=i2c_read();
  36.          byteB[ 4 ]=i2c_read();
  37.          byteB[ 5 ]=i2c_read();
  38.          byteB[ 6 ]=i2c_read();
  39.          byteB[ 7 ]=i2c_read();
  40.          byteC[ 0 ]=i2c_read();
  41.          byteC[ 1 ]=i2c_read();
  42.          byteC[ 2 ]=i2c_read();
  43.          byteC[ 3 ]=i2c_read();
  44.          byteC[ 4 ]=i2c_read();
  45.          byteC[ 5 ]=i2c_read();
  46.          byteC[ 6 ]=i2c_read();
  47.          byteC[ 7 ]=i2c_read();
  48.          
  49.          }
  50.    }
  51. }

En algo debo haberme equivocado U.U
« Última modificación: 01 de Marzo de 2013, 15:55:40 por erickkelvin »

Desconectado RICHI777

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1498
Re: Ayuda i2c enviar bytes
« Respuesta #7 en: 01 de Marzo de 2013, 16:02:12 »
Hola, nose si es posible escribir así a lo loco un choclo de bytes ... me pareces que en el medio debes colocar el enmarcado start - stop.

Saludos !

Desconectado erickkelvin

  • PIC10
  • *
  • Mensajes: 8
Re: Ayuda i2c enviar bytes
« Respuesta #8 en: 01 de Marzo de 2013, 17:37:54 »
Hola, nose si es posible escribir así a lo loco un choclo de bytes ... me pareces que en el medio debes colocar el enmarcado start - stop.

Saludos !

si pues  :oops: , bueno probare a ver como sale


 

anything