Page 1 of 1

I2c Interface issue with PIC16F886

PostPosted: Tue Aug 01, 2017 1:46 pm
by ajitnayak87
Dear all,

I am newbie to Pic Programming, I am using MPLAb & Hitech compiler to execute above code. I am trying to Interface PIC16F886 with ISL12022M Real time I2C device. i copied code example written for DS1307 interface with 16F887A PIC. I have capable to inteface Basic functionality with above . In below code While write into ISL12022M o could able to see data what i have send in memory register But as when Trying to read rtc time i could able to read last memory write value From SSPBUF. please share any working example code for I2C communcation or let me know any error in below code.

once I2c read value should be displayed on 4 digit seven segment display.

I think I am doing Misatake in this part. while Reading data i m just sending address so whatever last written in address it displaying.



Code: Select all


unsigned int  Write_ISL12022M(unsigned short address, unsigned short w_data)
 {
     I2C_Start(); // Start I2C communication
     I2C_Write(0XD0);
     I2C_Write(address); //write address to write data
     I2C_Write(w_data); //write data into hexadecimal
     I2C_Stop();//stop I2C communication
     return(w_data);
}

unsigned short Read_ISL12022M(unsigned short address)
{
 
     I2C_Start();
     I2C_Write(address); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
      I2C_Write(address);
      I2C_ReStart();
      I2C_Write(0xD1); //0x68 followed by 1 --> 0xD1
     r_data=I2C_Read(0);
     I2C_Stop();
  return(r_data);
}

void SetDateTime()
{

        I2C_Start();   
        I2C_Write(0xD0); 
        I2C_Write(0x00);
        sec= Write_ISL12022M(0X00, 12); //01 sec
        min = Write_ISL12022M(0X01,52); //01 sec
        hour = Write_ISL12022M(0X02,9); //01 sec
        day= Write_ISL12022M(0X03,7); //01 sec
        date = Write_ISL12022M(0X04, 29); //01 sec
        month =Write_ISL12022M(0X05,07); //01 sec
        year = Write_ISL12022M(0X06,17); //01 sec
       I2C_Stop();

}
   



void RTC_GetDateTime()
{
    I2C_Start();                            // Start I2C communication
    I2C_Send_ACK();
    sec = I2C_Read(1);                // read second and return Positive ACK
   I2C_Send_ACK();
      min = I2C_Read(1);                 // read minute and return Positive ACK
   I2C_Send_ACK();
    hour= I2C_Read(0);               // read hour and return Negative/No ACK
   I2C_Send_ACK();
     day = I2C_Read(1);           // read weekDay and return Positive ACK
   I2C_Send_ACK();
    date= I2C_Read(1);              // read Date and return Positive ACK
   I2C_Send_ACK();
     month=I2C_Read(1);            // read Month and return Positive ACK
   I2C_Send_ACK();
     year =I2C_Read(0);             // read Year and return Negative/No ACK
   I2C_Send_ACK();
    I2C_Stop();                              // Stop I2C communication after reading the Date
}



unsigned char I2C_Write( unsigned char i2cWriteData )
   {
   i2c_waitForIdle();
    SSPBUF = i2cWriteData;
    return (!ACKSTAT);                   // function returns '1'
    }

int I2C_Read( unsigned char ack )
   {
      unsigned char i2cReadData;
     //unsigned int  i2cReadData;
   i2c_waitForIdle();
    RCEN = 1;
    SDA=1;
    SCK=1;
   i2c_waitForIdle();
     
    i2cReadData = SSPBUF;
    SCK=0;
   i2c_waitForIdle();
    SCK=1;
    if(ack)
        {
        ACKDT = 0;
        }
    else
        {
        ACKDT = 1;
        }
     ACKEN = 1;               // send acknowledge sequence

    return( i2cReadData );
   }

  unsigned int bcdtodecimal(unsigned int bcd)
{
    unsigned int decimal;
    decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
    return decimal;
}