I2C İletişim kopuyor...

Başlatan RcALTIN, 10 Nisan 2010, 22:25:44

RcALTIN

Son günlerde I2C ile uğraşıyorum. CCS örneklerinden Ex_SLAVE.C dosyasını inceledim ve SLAVE PIC içinuyguladım... Slave PIC'e sorunsuzca değer gönderip alabiliyorum, ancak araya farklı adresli başka bir cihaz girdikten sonra artık SLAVE PIC'(ler)e ulaşamıyorum, farklı kodlarda denedim ama nedenini bir türlü bulamadım...









Master.c:
#include "rcaltin_lcd.c"

char saniye=0,dakika=0,saat=01,sayac=0,tus_sn=0;
char tus=99,deneme=0;

#define x0 PIN_E0 
#define x1 PIN_E1 
#define x2 PIN_B2 
#define x3 PIN_B3
#define y0 PIN_B4
#define y1 PIN_B5 
#define y2 PIN_B6
#define y3 PIN_B7

#define io_1 0x8E
#define io_2 0x8C

#define ds1621_1 0x90
#define ds1621_2 0x92
#define ds1621_3 0x94

#define tc74_1 0x9A

#int_RTCC
void  RTCC_isr(void) 
{
   if(++sayac>=15)//orj=100
   {
      sayac=0;
      if(++saniye>=60)
      {
         saniye=0;
         if(++dakika>=60)
         {
            dakika=0;
            if(++saat>=24)
            saat=0;
         }
      }
   }
}

#int_EXT
void  EXT_isr(void) 
{}

#int_EXT1
void  EXT1_isr(void) 
{}

#int_RB
void  RB_isr(void) 
{}

#int_AD
void  AD_isr(void) 
{}

#int_RDA
void  RDA_isr(void) 
{}

#int_TBE
void  TBE_isr(void) 
{}

#int_SSP
void  SSP_isr(void) 
{
 
}

#int_EEPROM
void  EEPROM_isr(void) 
{}

#int_OSCF
void  OSCF_isr(void) 
{}

void bekle_10ms(int tekrar, int1 yenileme)
{
   char i;
   for (i=0; i<=tekrar; i++)
   {
      delay_ms(10);
      if(yenileme)
      {}
   }   
}

int1 slave_hazir(int adres) 
{
   int1 ack;
   i2c_start();
   ack = i2c_write(adres);
   i2c_stop();
   return !ack;
}

void slave_yaz(int adres, int tampon_adresi, int veri) 
{
   while(!slave_hazir(adres) && deneme < 5)
   {
      deneme++;
   }
   i2c_start();
   i2c_write(adres);
   i2c_write(tampon_adresi);
   i2c_write(veri);
   i2c_stop();
}


int slave_oku(int adres, int tampon_adresi) 
{
   int veri;
   while(!slave_hazir(adres) && deneme < 5)
   {
      deneme++;
   }
   i2c_start();
   i2c_write(adres);
   i2c_write(tampon_adresi);
   bekle_10ms(100,1);
   i2c_start();
   i2c_write(adres+1);
   veri = i2c_read(0);
   i2c_stop();
   return(veri);
}

float TC74_oku(int adres)
{
   int d,d2;
   i2c_start();
   i2c_write(adres);
   i2c_write(0x00);
   i2c_start();
   i2c_write(adres+1);
   d = i2c_read(0);
   i2c_stop();
   return (float)d;
}

void DS1621_hazirla(int adres)
{
   i2c_start(); 
   i2c_write(adres); 
   i2c_write(0xac); 
   i2c_write(3); 
   i2c_stop(); 
   //delay_ms(20); 
   bekle_10ms(2,0);
   i2c_start(); 
   i2c_write(adres); 
   i2c_write(0xee); 
   i2c_stop();
}

float DS1621_oku(int adres)
{
   float dh,dl;
   DS1621_hazirla(adres);
   bekle_10ms(75,1);
   i2c_start(); 
   i2c_write(adres); 
   i2c_write(0xaa); 
   i2c_start(); 
   i2c_write(adres+1); 
   dh=i2c_read(); 
   dl=i2c_read(0); 
   i2c_stop();
   if(dh>200)
      dh=dh-256;
   if(dl!=0)
      dh+=0.5;
   return dh;
}

void mtd_TusAl()
{

      //delay_ms(10);
      output_high(y0);
      output_low(y1);
      output_low(y2);
      output_low(y3);
      if(input(x0)){/*output_high(led_tus);*/ while(input(x0)){ } tus=1;}
      else if(input(x1)){/*output_high(led_tus);*/ while(input(x1)){ } tus=4;}
      else if(input(x2)){/*output_high(led_tus);*/ while(input(x2)){ } tus=7;}
      else if(input(x3)){/*output_high(led_tus);*/ while(input(x3)){ } tus=10;}
      output_low(y0);
      output_high(y1);
      output_low(y2);
      output_low(y3);
      if(input(x0)){/*output_high(led_tus);*/ while(input(x0)){ } tus=2;}
      else if(input(x1)){/*output_high(led_tus);*/ while(input(x1)){ } tus=5;}
      else if(input(x2)){/*output_high(led_tus);*/ while(input(x2)){ } tus=8;}
      else if(input(x3)){/*output_high(led_tus);*/ while(input(x3)){ } tus=0;}
      output_low(y0);
      output_low(y1);
      output_high(y2);
      output_low(y3);
      if(input(x0)){/*output_high(led_tus);*/ while(input(x0)){ }tus=3;}
      else if(input(x1)){/*output_high(led_tus);*/ while(input(x1)){ } tus=6;}
      else if(input(x2)){/*output_high(led_tus);*/ while(input(x2)){ } tus=9;}
      else if(input(x3)){/*output_high(led_tus);*/ while(input(x3)){ } tus=11;}
      output_low(y0);
      output_low(y1);
      output_low(y2);
      output_high(y3);
      if(input(x0)){/*output_high(led_tus);*/ while(input(x0)){ }tus=12;}
      else if(input(x1)){/*output_high(led_tus);*/ while(input(x1)){ } tus=13;}
      else if(input(x2)){/*output_high(led_tus);*/ while(input(x2)){ } tus=14;}
      else if(input(x3)){/*output_high(led_tus);*/ while(input(x3)){ } tus=15;}
   
}

void main()
{
   setup_adc_ports(AN0_TO_AN4|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_EXT1);
   enable_interrupts(INT_RB);
   enable_interrupts(INT_AD);
   enable_interrupts(INT_RDA);
   //enable_interrupts(INT_TBE);
   enable_interrupts(INT_SSP);
   enable_interrupts(INT_EEPROM);
   enable_interrupts(INT_OSCF);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);

   // TODO: USER CODE!! 
   lcd_init();
   while(TRUE) 
   {    
      mtd_TusAl();
      //delay_ms(50);
      if(tus==12)    
      {  
         slave_yaz(io_1,0x00,saniye);
         lcd_gotoxy(1,1);
         printf(lcd_putc,"W/R : %04d",saniye);
      } 
      
      if(tus==13)    
      {
         lcd_gotoxy(15,1);
         printf(lcd_putc,"%04d",slave_oku(io_1,0x00));
      }
      
      if(tus==14)
      {
         slave_yaz(io_2,0x00,saniye);
         lcd_gotoxy(1,2);
         printf(lcd_putc,"W/R : %04d",saniye);
         
      }
      
      if(tus==15)    
      {
         lcd_gotoxy(15,2);
         printf(lcd_putc,"%04d",slave_oku(io_2,0x00));
      }
      
      if(tus==11)
      {
         lcd_gotoxy(1,3);
         printf(lcd_putc,"%04.1fC",DS1621_oku(ds1621_1));
         
         lcd_gotoxy(8,3);
         printf(lcd_putc,"%04.1fC",TC74_oku(tc74_1));
         
         lcd_gotoxy(15,3);
         printf(lcd_putc,"%04.1fC",DS1621_oku(ds1621_3));
         
      }
      tus=16;
   } 
}


Master.h :
#include <18F4520.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES LP                       //Low power osc < 200 khz
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(MASTER, sda=PIN_C4,scl=PIN_C3)



Slave.c :
BYTE address, buffer[0x50];

#int_RTCC
void  RTCC_isr(void) 
{}

#int_EXT
void  EXT_isr(void) 
{}

#int_SSP
void  SSP_isr(void) 
{
   int incoming, state;
   state = i2c_isr_state();
   if(state < 0x80)
   {
      incoming = i2c_read();
      if(state == 1)
         address = incoming;
      if(state == 2)
         buffer[address] = incoming;
   }
   if(state == 0x80)
   {
      i2c_write(buffer[address]);
   }
   
}

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   //setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   while(1)
   {
      
   }
}


Slave.h
#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES RC                       //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(SLAVE,sda=PIN_C4,scl=PIN_C3,address=0x8E)


2. slave'in sadece i2c adresi 0x8C'dir...
KİMSEYİ ENGELLEDİĞİM YOK, ÖZEL İLETİ DEVRE DIŞI !

RcALTIN

#1
Problemi anlayabilen birileri yokmu ?  :-\ Ayrıca belirtmeyi unutmuşum Master PIC : 18F4520, Slave PIC'ler : 16F877A, tümü 8mhz çalışmakta...
KİMSEYİ ENGELLEDİĞİM YOK, ÖZEL İLETİ DEVRE DIŞI !

Salih

Bu devre için öyledir demiyorum ama, başıma geldiği için söylüyorum. Bilhassa haberleşmeli sistemlerde
ISIS'de çalışmadığı halde gerçek devre üzerinde çalışan programlarım oldu. Size tavsiyem prototip devre
hazırlayıp, programınızı gerçek devre üzerinde denemenizdir. Nihayetinde zaten gerçek devre üzerinde
mutlaka denenecek. Kolay gelsin.

RcALTIN

Alıntı yapılan: salih - 12 Nisan 2010, 09:13:46
Bu devre için öyledir demiyorum ama, başıma geldiği için söylüyorum. Bilhassa haberleşmeli sistemlerde
ISIS'de çalışmadığı halde gerçek devre üzerinde çalışan programlarım oldu. Size tavsiyem prototip devre
hazırlayıp, programınızı gerçek devre üzerinde denemenizdir. Nihayetinde zaten gerçek devre üzerinde
mutlaka denenecek. Kolay gelsin.

Teşekkürler, öğrenimim dolayısıyla Ispartada bulunmaktayım, burada test imkanım yok ancak İstanbul'a döndüğümde devreyi test edebilirim... Daha önce ISIS'te çalışmayıpta gerçekte çalışan bir tasarımım olmamıştı, bu ihtimal aklıma bile gelmedi doğrusu; döndüğümde en kısa zamanda bir prototip devre kuracağım...
KİMSEYİ ENGELLEDİĞİM YOK, ÖZEL İLETİ DEVRE DIŞI !