Picproje Elektronik Sitesi

DERLEYİCİLER => Microchip XC Serisi => Microchip XC8 => Konuyu başlatan: ademtosun58 - 20 Nisan 2017, 10:45:29

Başlık: I2C haberleşme
Gönderen: ademtosun58 - 20 Nisan 2017, 10:45:29
Arkadaşlar İyi günler,

Öncelikle forumda devamlı bazı şeyler hakkında hazır bir bilgi almaya çalışıyorum.Bunun nedeni yazılım ile ilgili fazla bir bilgim olmaması sadece kullandıgım bazı elemanlarda ihtiyacım oldugu içindir.

Sorunum şu
Bir adet control box var elimde ve bu cihazın içindeki configurasyonları pic kullanarak lcd ekrana bastırmak istiyorum burada control box master durumda pic ise slave konumunda kullanılacak.Elimde I2c kütüphanesi mevcut ama nasıl bir sey yapıcagım hakkında fikrim yok.Bazı arkadaslar emeklemeden koşmaya çalışma felan diyorlar ama elektronik çıkışlı oldugum için yazılım kısmında bu tür complex şeyleri ilk başta yapamıyorum sizden ricam bana bu konuda örnek bir kod göstermeniz  ı2c kütüphanemi paylaşıyorum yardımlarınızı bekliyorum.

Elimdeki 1.Kütüphane

void InitI2C(void)
{
SDA_DIR = 1; // Make SDA and
SCK_DIR = 1; // SCK pins input

SSPADD  = ((_XTAL_FREQ/4000)/I2C_SPEED) - 1;
SSPSTAT = 0x80; // Slew Rate control is disabled
SSPCON1 = 0x28;
}


// Function Purpose: I2C_Start sends start bit sequence
void I2C_Start(void)
{
SEN = 1; // Send start bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}


// Function Purpose: I2C_ReStart sends start bit sequence
void I2C_ReStart(void)
{
RSEN = 1; // Send Restart bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}


//Function : I2C_Stop sends stop bit sequence
void I2C_Stop(void)
{
PEN = 1; // Send stop bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}



//Function : I2C_Send_ACK sends ACK bit sequence
void I2C_Send_ACK(void)
{
ACKDT = 0; // 0 means ACK
ACKEN = 1; // Send ACKDT value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}


//Function : I2C_Send_NACK sends NACK bit sequence
void I2C_Send_NACK(void)
{
ACKDT = 1; // 1 means NACK
ACKEN = 1; // Send ACKDT value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}


// Function Purpose: I2C_Write_Byte transfers one byte
bit I2C_Write_Byte(unsigned char Byte)
{
SSPBUF = Byte; // Send Byte value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit

return ACKSTAT; // Return ACK/NACK from slave
}


// Function Purpose: I2C_Read_Byte reads one byte
unsigned char I2C_Read_Byte(void)
{
RCEN = 1; // Enable reception of 8 bits
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit

    return SSPBUF; // Return received byte
}



2.Kütüphane

void interrupt I2C_Slave_Read()
{
    if(SSPIF == 1)
    {
       SSPCONbits.CKP = 0;
       
       if ((SSPCONbits.SSPOV) || (SSPCONbits.WCOL))
       {
             z = SSPBUF;            // Read the previous value to clear the buffer
             SSPCONbits.SSPOV = 0; // Clear the overflow flag
             SSPCONbits.WCOL = 0;   // Clear the collision bit
             SSPCONbits.CKP = 1;
       }

      if(!SSPSTATbits.D_nA && !SSPSTATbits.R_nW)
       {
           z = SSPBUF;
           while(!BF);
           PORTD = SSPBUF;
           SSPCONbits.CKP = 1;
           SSPM3 = 0;
       }
       else if(!SSPSTATbits.D_nA && SSPSTATbits.R_nW)
       {
           z = SSPBUF;
           BF = 0;
           SSPBUF = PORTB ;
           SSPCONbits.CKP = 1;
           while(SSPSTATbits.BF);
       }
       
       SSPIF = 0;
    }
}

void I2C_Slave_Init(short address)
{
    SSPSTAT = 0x80;
    SSPADD = address;
    SSPCON = 0x36;
    SSPCON2 = 0x01;
    TRISC3 = 1;
    TRISC4 = 1;
    GIE = 1;
    PEIE = 1;
    SSPIF = 0;
    SSPIE = 1;
}