Haberler:

Forum kuralları güncellendi LÜTFEN  okuyunuz:  https://bit.ly/2IjR3ME

Ana Menü

i2c protokolü

Başlatan alperyazir, 23 Nisan 2012, 00:58:32

alperyazir

üstadlar tekrar merhaba. benim şöyle bi sıkıntım var, 3 tane pic i haberleştirmek istiyorum bunu tx ve rx kullanarak yaptım ama ben rs232 nin terminalinden aldığım bilgiye göre ledleri kontrol etmek istiyorum. işin aslı yapacağım projede 1 tane master ve 2 tane slave picim var. ama yine slave piclerden bilgi almam gerekecek biraz araştırma yaptım ve i2c protokolünün benim için uygun olacağını buldum. şimdi sizlere sorum şu , bu protokolle ilgili bi örnek kodunuz varmı ? ben buldum ama özellikle slave  kısmındaki mantığı çözemedim. bulduğum kod şöle,

#include <16F690.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOCPD
//————————————————————————————————————————————————————————————
#use i2c(Master, sda=PIN_C2, scl=PIN_C3) // I2C by software
//————————————————————————————————————————————————————————————
#use rs232(baud=9600, parity=N, xmit=PIN_B7, rcv=PIN_B5, bits=8, errors)
//————————————————————————————————————————————————————————————
// PORTC.2 [RC2 pin14]  -> I2C SDA (I2C Serial Data)
// PORTC.3 [RC3 pin7]   -> I2C SCL (I2C Serial Clock)
//————————————————————————————————————————————————————————————
// PORTB.5 [RB5 pin12]  -> PicRX (RS232 input)
// PORTB.7 [RB7 pin10]  -> PicTX (RS232 output)
//————————————————————————————————————————————————————————————
int x;

BYTE result;void main()
{
  delay_ms(200); // power up delay
  setup_adc_ports(NO_ANALOGS | VSS_VDD);
  setup_comparator(NC_NC_NC_NC);
  setup_oscillator(OSC_4MHZ);
  set_tris_a(0b11111111);
  set_tris_b(0b01111111 ); // Port B 00110000 ( Bit5/7 - RS232- Rx/tx)
  set_tris_c(0b10110011);

  while(1)
  {
    printf("\r\nI2C Master - Started!\r\n"); // debug to PC serial port - RS232
    output_low(PIN_C6); // Led for visual debug
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    output_high(PIN_C6);
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    delay_ms(500);
    output_low(PIN_C6);

    for(x=0;x<=15;x++)
    {
      i2c_start ();        // init I2C protocol
      i2c_write (0xA0); // slave device address
      i2c_write (x);      // slave 'internal memory address' [0;15]
      i2c_write(15-x);  // data
      i2c_stop();         // stop communication with slave
      delay_ms(50);
      i2c_start ();       // init I2C protocol
      i2c_write (0xA1); // slave device address
      result = i2c_read(0); // read slave last received data
      i2c_stop ();        // stop communication with slave
      printf("Result[%d]=%d\r\n",x,result); // debug data
      delay_ms(50);
    }

// test
      i2c_start ();
      i2c_write (0xA0);
      i2c_write (5); // request slave 'internal memory address' 0×05
      i2c_stop();
      delay_ms(50);
      i2c_start ();
      i2c_write (0xA1);
      result = i2c_read(0); // read slave data
      i2c_stop ();
      printf("ReadAddress(5)=%d\r\n",result);
      delay_ms(50);
   }
}
//————————————————————————————————————————————————————————————
Slave PIC:

#include <16F690.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOCPD
//————————————————————————————————————————————————————————————
#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, address=0xA0, FORCE_HW) // I2C by Hardware
//————————————————————————————————————————————————————————————
// PORTC.2 [RC2 pin14]  -> I2C SDA (I2C Serial Data)
// PORTC.3 [RC3 pin7]   -> I2C SCL (I2C Serial Clock)
//————————————————————————————————————————————————————————————
BYTE incoming, state; // I2C vars
BYTE address, buffer[0×10]; // Address and Array of Bytes

#INT_SSP
void ssp_interupt ()
{
  state = i2c_isr_state();

  if(state < 0×80) //Master is sending data
  {
    if(state == 0)
    {
    }
    if(state == 1)  //First received byte is address
    {
      incoming = i2c_read();
      address = incoming;
    }
    if(state == 2)  //Second received byte is data
    {
      incoming = i2c_read();
      buffer[address] = incoming;
    }
  }
  if(state == 0×80)  //Master is requesting data
  {
    i2c_write (buffer[address]);
  }
}
//————————————————————————————————————————————————————————————
void main()
{
  delay_ms(200); // power up delay
  setup_comparator(NC_NC_NC_NC);
  setup_vref(FALSE);
  set_tris_A ( 0b11111111 );      // Port A 11111111
  set_tris_B ( 0b01111111 );      // Port B 00110000 ( Bit5/7 - RS232- Rx/tx)
  set_tris_C ( 0b11111111 );      // Port C 11111111

  enable_interrupts(INT_SSP);
  enable_interrupts(GLOBAL);

  while(TRUE)
  {
    // main code here ...
  }
}
//————————————————————————————————————————————————————————————
PC Serial Port output - Master PIC Debug

I2C Master - Started!
Result[0]=15
Result[1]=14
Result[2]=13
Result[3]=12
Result[4]=11
Result[5]=10
Result[6]=9
Result[7]=8
Result[8]=7
Result[9]=6
Result[10]=5
Result[11]=4
Result[12]=3
Result[13]=2
Result[14]=1
Result[15]=0
ReadAddress(5)=10



benim haberleştirmek istediğim devrede şu şekilde

http://d1204.hizliresim.com/w/r/4smhy.png     
( sol attaki master, sol üstteki slave1 ve sağ üstteki slave2)
eğer elinizde anlayabilieceğim kod veya bi döküman varsa çok mahbule geçer:) şimdiden teşekkürler

alperyazir

yok mu bi yardım eli:)

skara1214

i2c yi boşver direk rs232 yi kullan ben kullanıyorum mis gibi çalışıyor sen öyle dene takıldıgın yerde yardımcı olmaya çalışırım
Herkes ölür ama herkes gerçekten yaşamaz