Autor Tema: Comunicación 2 pic16F877A en I2C falla....  (Leído 2215 veces)

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

Desconectado freshdesing

  • Colaborador
  • PIC12
  • *****
  • Mensajes: 88
Comunicación 2 pic16F877A en I2C falla....
« en: 13 de Julio de 2010, 11:58:08 »
Buenas, he conectado dos pics 16F877A mediante I2C. El maestro tiene cuatro leds, cuando se enciende el primero, manda como dato el "1", cuando se enciende el segundo manda como dato el "2" y así hasta el 4. Y el esclavo tiene una pantalla LCD que al principio muestra "esperando dato, y cuando le llega (debería) representa el dato que le ha llegado en la pantalla LCD. El problema es que no funciona, se enciende el primer led del maestro y se así se queda eternamente. Ambos pic´s tiene un led de "status" que parpadea cada medio segundo, y en ambos funciona bien, por lo que no se queda en ningún bucle ni colgado. Agradecería alguna ayudita... Adjunto los códigos:

MAESTRO:
Código: C
  1. //    RB7     --> ENTRADA ON/OFF
  2. //    RB6     --> ENTRADA ON/OFF
  3. //    RB5     --> ENTRADA ON/OFF
  4. //    RB4     --> ENTRADA ON/OFF
  5. //    RB0/INT -->
  6. //    RA0     --> LED STATUS
  7. //    RC7     -->
  8. //    RC6     -->
  9. //    RC5     -->
  10. //    RC4     --> SDA
  11. //    RC3     --> SCL
  12. //    RC2     -->
  13. //    RC1     -->
  14. //    RD0     --> OUT E
  15. //    RD1     --> OUT RS
  16. //    RD2     --> OUT RW
  17. //    RD3     -->  
  18. //    RD4     --> OUT D4
  19. //    RD5     --> OUT D5              
  20. //    RD6     --> OUT D6
  21. //    RD7     --> OUT D7
  22. //
  23. //
  24. //
  25. ////////////////////////////////////////////////////////////////////////////////
  26.  
  27. #include <16f877A.h>                    //pic a utilizar
  28. #fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT   //ordenes para el programador
  29. #use delay (clock=4000000)         //Fosc=4Mhz
  30.  
  31.  
  32. #use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,FAST,FORCE_HW)
  33.  
  34. #use fast_io(A)
  35. #use fast_io(B)
  36. #use fast_io(C)
  37. #use fast_io(D)
  38. #use fast_io(E)
  39.  
  40. #byte port_a = 0x05
  41. #byte port_b = 0x06
  42. #byte port_c = 0x07
  43. #byte port_d = 0x08
  44. #byte port_e = 0x09
  45.  
  46.  
  47. int dato=0;
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. void i2c(int dato)
  55. {
  56.    i2c_start();      //condicion de inicio
  57.    i2c_write(0xa0);  //direccion del esclavo con el que haremos la comunicacion
  58.                      //A6 A5 A4 A2 A1 A0 R/W  -> R/W = 0 escritura; 1 lectura
  59.                      
  60.  
  61.    i2c_write(dato);  //enviamos dato
  62.    i2c_stop();       //finalizacion de la comunicacion
  63.    delay_ms(1000);   //introducimos retardo para que no este constantemte escribiendo
  64.    
  65. }
  66.  
  67.  
  68.  
  69.  
  70. #INT_TIMER1
  71. void control_timer1(void)
  72.    {
  73.    output_bit(pin_a0,!input(pin_a0));
  74.    set_timer1(3036);
  75.    }
  76.  
  77.  
  78.  
  79. void main(void)
  80. {
  81.  
  82.    disable_interrupts(GLOBAL);
  83.    set_tris_a(0x00);       //    RA0   --> LED STATUS  
  84.                            //    RA1   -->  
  85.                            //    RA2   -->  
  86.                            //    RA3   -->  
  87.                            //    RA4   -->  
  88.                            //    RA5   -->  
  89.  
  90.    port_b_pullups(false);   // Resistencias de polarización del puerto B
  91.  
  92.    set_tris_b(0xF0);       //    RB7   -->
  93.                            //    RB6   -->
  94.                            //    RB5   -->
  95.                            //    RB4   -->
  96.                            //    RB3   -->  
  97.                            //    RB2   -->  
  98.                            //    RB1   -->    
  99.                            //    RB0   -->  
  100.  
  101.                            
  102.    set_tris_c(0x00);       //    RC0   -->  
  103.                            //    RC1   -->  
  104.                            //    RC2   -->  
  105.                            //    RC3   -->   SCL
  106.                            //    RC4   -->   SDA  
  107.                            //    RC5   -->                  
  108.                            //    RC6   -->  
  109.                            //    RC7   -->  
  110.                            
  111.    set_tris_d(0x00);     //    RD0   -->   OUT E
  112.    output_d (0x00);        //    RD1   -->   OUT RS
  113.                            //    RD2   -->   OUT RW
  114.                            //    RD3   -->  
  115.                            //    RD4   -->   OUT D4
  116.                            //    RD5   -->   OUT D5              
  117.                            //    RD6   -->   OUT D6
  118.                            //    RD7   -->   OUT D7
  119.  
  120.    setup_adc_ports(NO_ANALOGS);
  121.    setup_comparator(NC_NC_NC_NC);
  122.    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
  123.    enable_interrupts(INT_TIMER1);
  124.    enable_interrupts(GLOBAL);
  125.  
  126.    
  127.    while(1)
  128.    {
  129.       delay_ms(2000);
  130.       output_bit(pin_d0,1);
  131.       dato=0;
  132.       i2c(dato);
  133.       delay_ms(500);
  134.       output_bit(pin_d0,0);
  135.      
  136.       delay_ms(2000);
  137.       output_bit(pin_d1,1);
  138.       dato=1;
  139.       i2c (dato);
  140.       delay_ms(500);
  141.       output_bit(pin_d1,0);
  142.      
  143.       delay_ms(2000);
  144.       output_bit(pin_d2,1);
  145.       dato=2;
  146.       i2c(dato);
  147.       delay_ms(500);
  148.       output_bit(pin_d2,0);
  149.      
  150.       delay_ms(2000);
  151.       output_bit(pin_d3,1);
  152.       dato=3;
  153.       i2c(dato);
  154.       delay_ms(500);
  155.       output_bit(pin_d3,0);
  156.    }
  157.    
  158. }



ESCLAVO
Código: C
  1. /    RD0     --> OUT LED
  2. //    RD1     --> OUT LED
  3. //    RD2     --> OUT LED
  4. //    RD3     --> OUT LED
  5. //    RA0     --> LED STATUS
  6. //    RC4     --> SDA
  7. //    RC3     --> SCL
  8. //
  9. //
  10. //
  11. ////////////////////////////////////////////////////////////////////////////////
  12.  
  13. #include <16f877A.h>                    //pic a utilizar
  14. #fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT   //ordenes para el programador
  15. #use delay (clock=4000000)         //Fosc=4Mhz
  16.  
  17. #define use_portd_lcd TRUE
  18. #include <lcd.c>
  19.  
  20. #use fast_io(A)
  21. #use fast_io(B)
  22. #use fast_io(C)
  23. #use fast_io(D)
  24. #use fast_io(E)
  25.  
  26. #byte port_a = 0x05
  27. #byte port_b = 0x06
  28. #byte port_c = 0x07
  29. #byte port_d = 0x08
  30. #byte port_e = 0x09
  31.  
  32. #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3,ADDRESS=0xA0,FAST,FORCE_HW)
  33.  
  34.  
  35. int dato=0;
  36.  
  37. BYTE incoming, state; // I2C vars
  38. BYTE address, buffer[0x10]; // Address and Array of Bytes
  39.  
  40.  
  41. #INT_SSP
  42.  
  43. void i2c_isr()
  44.  {
  45.  
  46.       state = i2c_isr_state();
  47.  
  48.       if(state < 0x80) //Master is sending data
  49.         {
  50.           if(state == 0)
  51.              {
  52.              }
  53.      
  54.           if(state == 1)  //First received byte is address
  55.             {
  56.             incoming = i2c_read();
  57.             address = incoming;
  58.             }
  59.          if(state == 2)  //Second received byte is data
  60.            {
  61.            incoming = i2c_read();
  62.            printf(LCD_PUTC, "\fEl dato recibido es:\n %u",incoming);
  63.            buffer[address] = incoming;
  64.            }
  65.         }
  66.      
  67.         if(state == 0x80)  //Master is requesting data
  68.            {
  69.            i2c_write (buffer[address]);
  70.            }
  71.  }
  72.  
  73.  
  74. #INT_TIMER1
  75. void control_timer1(void)
  76.    {
  77.    output_bit(pin_a0,!input(pin_a0));
  78.    set_timer1(3036);
  79.    }
  80.  
  81.  
  82. void main(void)
  83. {
  84.    delay_ms(200); // power up delay
  85.    disable_interrupts(GLOBAL);
  86.    set_tris_a(0x00);       //    RA0   -->  
  87.                            //    RA1   -->  
  88.                            //    RA2   -->  
  89.                            //    RA3   -->  
  90.                            //    RA4   -->  
  91.                            //    RA5   -->  
  92.  
  93.    port_b_pullups(false);   // Resistencias de polarización del puerto B
  94.  
  95.    set_tris_b(0xF1);      
  96.  
  97.                            
  98.    set_tris_c(0xFF);       //    RC0   -->  
  99.                            //    RC1   -->  
  100.                            //    RC2   -->  
  101.                            //    RC3   -->   SCL
  102.                            //    RC4   -->   SDA  
  103.                            //    RC5   -->                  
  104.                            //    RC6   -->  
  105.                            //    RC7   -->  
  106.                            
  107.    //set_tris_d(0x00);       //    RD0   -->   OUT LED  
  108.                            //    RD1   -->   OUT LED  
  109.                            //    RD2   -->   OUT LED  
  110.                            //    RD3   -->   OUT LED  
  111.                            //    RD4   -->    
  112.                            //    RD5   -->                  
  113.                            //    RD6   -->  
  114.                            //    RD7   -->  
  115.  
  116.    
  117.    setup_comparator(NC_NC_NC_NC);
  118.    setup_vref(FALSE);
  119.    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
  120.    enable_interrupts(INT_TIMER1);
  121.    setup_psp(PSP_DISABLED);
  122.    enable_interrupts(INT_SSP);
  123.    enable_interrupts(GLOBAL);
  124.    lcd_init();         //inicializa lcd
  125.    printf(LCD_PUTC, "\fEsperando dato..");
  126.    
  127.    
  128.    
  129.   while(1)
  130.    {
  131.    
  132.    
  133.        
  134.    }
  135.  
  136.  
  137. }



Gracias de antemano.