Autor Tema: :: pic18F2550: Parpadea dos veces:: I2C::  (Leído 1427 veces)

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

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
:: pic18F2550: Parpadea dos veces:: I2C::
« en: 04 de Julio de 2012, 15:11:54 »
Hola

Tras poner a comunicar por i2c un master con dos esclavos y mandarle datos a distintas direcciones
y ver que el terminal virtual de protesus me imprime ambas tramas, la de su direcion y la
de la otra, decido probar en placa, y le coloco unos led como banderas.

Otra cosa que no logro comprender porque si lo monto en placa parece hacer todo bien
excepto una cosa de los esclavos me enciende dos veces en vez de una.

me explico si del master enciendo led rojo cuando envio y cuando temino de enviar apago
en el esclavo enciende led rojo dentro de la interrupcion y cuando sale le apago desde el main y le enciendo led verde y retardo el apagado.

despues hace lo mismo con el segundo esclavo.

pero porque me parpadea dos veces si solo le puse una..

que cosa mas rara.

Código: C++
  1. /*********************************************
  2. *
  3. * MASTER main_jn.c:
  4. *
  5. **********************************************/
  6.  
  7. #include "main_jn.h"
  8.  
  9.  
  10. void main()
  11. {
  12.  
  13.    setup_adc_ports(NO_ANALOGS|VSS_VDD);
  14.    setup_adc(ADC_CLOCK_DIV_2);
  15.    setup_wdt(WDT_OFF);
  16.    setup_timer_0(RTCC_INTERNAL);
  17.    setup_timer_1(T1_DISABLED);
  18.    setup_timer_2(T2_DISABLED,0,1);
  19.    setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  20.    setup_comparator(NC_NC_NC_NC);
  21.    setup_vref(FALSE);
  22. //Setup_Oscillator parameter not selected from Intr Oscillator Config tab
  23.  
  24.    // TODO: USER CODE!!
  25.  
  26.    int8 dato=0;
  27.  
  28.    delay_ms(1000);  //Hay que dejar tiempo que se inicialize el esclavo
  29.  
  30.    while(true)
  31.    {
  32.       output_high(PIN_B7);
  33.      
  34.       delay_ms(1000);
  35.  
  36.       i2c_start();    // Condicion Inicio
  37.  
  38.       i2c_write(PIC2);// Direccion dispositivo
  39.  
  40.       delay_ms(3);    // *** Necesario ***
  41.  
  42.       i2c_write(dato);// dato
  43.  
  44.       delay_ms(3);    // *** Necesario ***
  45.  
  46.       i2c_write(++dato);// dato
  47.  
  48.       i2c_stop();     // Stop
  49.          
  50.       output_low(PIN_B7);
  51.       delay_ms(1000);
  52.      
  53.      
  54.       output_high(PIN_B6);
  55.       delay_ms(1000);
  56.      
  57.       i2c_start();    // Condicion Inicio
  58.  
  59.       i2c_write(PIC3);// Direccion dispositivo
  60.  
  61.       delay_ms(3);    // *** Necesario ***
  62.  
  63.       i2c_write(dato);// dato
  64.  
  65.       delay_ms(3);    // *** Necesario ***
  66.  
  67.       i2c_write(++dato);// dato
  68.  
  69.       i2c_stop();     // Stop
  70.      
  71.       output_low(PIN_B6);
  72.       delay_ms(1000);
  73.     }
  74.  
  75. }


Código: C++
  1. /*********************************************
  2. *
  3. * MASTER main_jn.h:
  4. *
  5. **********************************************/
  6. #include <18F2550.h>
  7. #device adc=8
  8.  
  9. #FUSES NOWDT                    //No Watch Dog Timer
  10. #FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
  11. #FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
  12. #FUSES NOPROTECT                //Code not protected from reading
  13. #FUSES NOBROWNOUT               //No brownout reset
  14. #FUSES BORV20                   //Brownout reset at 2.0V
  15. #FUSES NOPUT                    //No Power Up Timer
  16. #FUSES NOCPD                    //No EE protection
  17. #FUSES STVREN                   //Stack full/underflow will cause reset
  18. #FUSES NODEBUG                  //No Debug mode for ICD
  19. #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
  20. #FUSES NOWRT                    //Program memory not write protected
  21. #FUSES NOWRTD                   //Data EEPROM not write protected
  22. #FUSES IESO                     //Internal External Switch Over mode enabled
  23. #FUSES FCMEN                    //Fail-safe clock monitor enabled
  24. #FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
  25. #FUSES NOWRTC                   //configuration not registers write protected
  26. #FUSES NOWRTB                   //Boot block not write protected
  27. #FUSES NOEBTR                   //Memory not protected from table reads
  28. #FUSES NOEBTRB                  //Boot block not protected from table reads
  29. #FUSES NOCPB                    //No Boot Block code protection
  30. #FUSES MCLR                     //Master Clear pin enabled
  31. #FUSES LPT1OSC                  //Timer1 configured for low-power operation
  32. #FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
  33. #FUSES PLL12                    //Divide By 12(48MHz oscillator input)
  34. #FUSES CPUDIV4                  //System Clock by 4
  35. #FUSES USBDIV                   //USB clock source comes from PLL divide by 2
  36. #FUSES VREGEN                   //USB voltage regulator enabled
  37.  
  38. #use delay(clock=20000000)
  39. #define I2C_SDA   PIN_B0
  40. #define I2C_SCL   PIN_B1
  41. #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
  42. #use i2c(Master,Slow,sda=PIN_B0,scl=PIN_B1)
  43.  
  44. //Leds que se iluminaran
  45. #DEFINE LED1 PIN_B7  //PIN 3
  46.  
  47. #DEFINE PIC2 0x02    //Direccion dispositivo PIC 2 (esclavo)
  48. #DEFINE PIC3 0x04    //Direccion dispositivo PIC 3 (esclavo)


Código: C++
  1. /*********************************************
  2. *
  3. * MASTER main_s1_jn.c:
  4. *
  5. **********************************************/
  6. #include "main_s1_jn.h"
  7. //VARIABLES GLOBALES
  8. int8 buffer[2];
  9. int1 recibido=0;
  10.  
  11. #int_SSP
  12. void  SSP_isr(void) //Rutina Interrupción I2C
  13. {
  14. disable_interrupts(int_ssp);  // *** Necesario ***
  15. output_high(PIN_B7);
  16.  
  17.    buffer[0]=i2c_read();
  18.    
  19.    buffer[1]=i2c_read();
  20.    
  21.    recibido=1;
  22.    
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29. void main()
  30. {
  31.  
  32.    setup_adc_ports(NO_ANALOGS|VSS_VDD);
  33.    setup_adc(ADC_CLOCK_DIV_2);
  34.    setup_wdt(WDT_OFF);
  35.    setup_timer_0(RTCC_INTERNAL);
  36.    setup_timer_1(T1_DISABLED);
  37.    setup_timer_2(T2_DISABLED,0,1);
  38.    setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  39.    setup_comparator(NC_NC_NC_NC);
  40.    setup_vref(FALSE);
  41.    //enable_interrupts(INT_SSP);
  42.    //enable_interrupts(GLOBAL);
  43. //Setup_Oscillator parameter not selected from Intr Oscillator Config tab
  44.  
  45.    // TODO: USER CODE!!
  46.  
  47. delay_ms(50);
  48.  
  49.    //lcd_init();
  50.    //lcd_putc("fPreparado...");
  51.    printf("\n\r Preparado...");
  52.  
  53.    delay_ms(300);
  54.  
  55.    enable_interrupts(int_ssp);
  56.    enable_interrupts(global);
  57.  
  58.    while(true)
  59.    {
  60.       if(recibido==1)
  61.       {
  62.          delay_ms(500);
  63.          output_low(PIN_B7);
  64.          output_high(PIN_B6);
  65.          delay_ms(500);
  66.          output_low(PIN_B6);
  67.          //printf(lcd_putc,"fb[0]=%U b[2]=%u",buffer[0],buffer[1]);
  68.          printf("\n\r fb[0]=%U b[2]=%u",buffer[0],buffer[1]);
  69.          
  70.  
  71.          recibido=0;    
  72.  
  73.            
  74.          enable_interrupts(int_ssp);  // *** Necesario ***
  75.  
  76.  
  77.       }
  78.      
  79.    }
  80.  
  81.  
  82. }

Código: C++
  1. /*********************************************
  2. *
  3. * MASTER main_s1_jn.h:
  4. *
  5. **********************************************/
  6. #include <18F2550.h>
  7. #device adc=8
  8.  
  9. #FUSES NOWDT                    //No Watch Dog Timer
  10. #FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
  11. #FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
  12. #FUSES NOPROTECT                //Code not protected from reading
  13. #FUSES NOBROWNOUT               //No brownout reset
  14. #FUSES BORV20                   //Brownout reset at 2.0V
  15. #FUSES NOPUT                    //No Power Up Timer
  16. #FUSES NOCPD                    //No EE protection
  17. #FUSES STVREN                   //Stack full/underflow will cause reset
  18. #FUSES NODEBUG                  //No Debug mode for ICD
  19. #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
  20. #FUSES NOWRT                    //Program memory not write protected
  21. #FUSES NOWRTD                   //Data EEPROM not write protected
  22. #FUSES IESO                     //Internal External Switch Over mode enabled
  23. #FUSES FCMEN                    //Fail-safe clock monitor enabled
  24. #FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
  25. #FUSES NOWRTC                   //configuration not registers write protected
  26. #FUSES NOWRTB                   //Boot block not write protected
  27. #FUSES NOEBTR                   //Memory not protected from table reads
  28. #FUSES NOEBTRB                  //Boot block not protected from table reads
  29. #FUSES NOCPB                    //No Boot Block code protection
  30. #FUSES MCLR                     //Master Clear pin enabled
  31. #FUSES LPT1OSC                  //Timer1 configured for low-power operation
  32. #FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
  33. #FUSES PLL12                    //Divide By 12(48MHz oscillator input)
  34. #FUSES CPUDIV4                  //System Clock by 4
  35. #FUSES USBDIV                   //USB clock source comes from PLL divide by 2
  36. #FUSES VREGEN                   //USB voltage regulator enabled
  37.  
  38. #use delay(clock=20000000)
  39. #define I2C_SDA   PIN_B0
  40. #define I2C_SCL   PIN_B1
  41.  
  42. #DEFINE PIC2 0x02
  43.  
  44. #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
  45. #use i2c(Slave,Slow,sda=PIN_B0,scl=PIN_B1,address=PIC2)
  46.  
  47.  
  48. #define use_portb_lcd TRUE //Necesario si usamos el portB para el LCD, y que sea compatible con LCD.C
  49. #include <LCD.C>


Que le esta sucediendo al circuito ?
¿que se me pasa por alto y no deberia?

sigo con esto..



 

anything