I am working with I2C, I have interface Ds1307 RTC and use I2C for reading DS1307. I am able to read time and date, but not able to set time and date using I2C to help me out on the same.
Please refer my code as below,
/****************************************************************************
/* set rtc and date for DS1307
****************************************************************************/
void DS1307_set_rtc_date(void)
{
char address;
int i2cFile;
unsigned char buf[10];
i2cFile=i2c_open(DS1307_BUS, 0x68);
////Write Time/////////////////////////////////
i2c_write_byte(i2cFile, 0xD0);
//delay_ms(10);
// Write seconds
i2c_write_byte(i2cFile, 0x00);
//delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x80);
//delay_ms(10);//usleep(6000); //
// Write Minutes
//i2c_write_byte(i2cFile, 0x01);
// delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x10);
//delay_ms(10);//usleep(6000); //
// Write Hours
//i2c_write_byte(i2cFile, 0x02);
//delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x10);
//delay_ms(10);//usleep(6000); //
////Write day, Date,montn,year/////////////////////////////////
/* // Write day
//i2c_write_byte(i2cFile, 0x03);
// delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x04);
//delay_ms(10);//usleep(6000); //
// Write Date
//i2c_write_byte(i2cFile, 0x04);
// delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x16);
//delay_ms(10);//usleep(6000); //
// Write Month
// i2c_write_byte(i2cFile, 0x05);
// delay_ms(10);//usleep(6000); //
i2c_write_byte(i2cFile, 0x02);
//delay_ms(10);//usleep(6000); //
// Write year
// i2c_write_byte(i2cFile, 0x06);
// delay_ms(10);//usleep(8000); //
i2c_write_byte(i2cFile, 0x20);
//delay_ms(10);//usleep(8000); //
// Write Control register
// i2c_write_byte(i2cFile, 0x07);
// delay_ms(10);//usleep(8000); //
i2c_write_byte(i2cFile, 0x00);
//delay_ms(10);//usleep(8000); // */
i2c_close(i2cFile);
usleep(1000); //
// RTC start
i2cFile=i2c_open(DS1307_BUS, 0x68);
////Read Time/////////////////////////////////
i2c_write_byte(i2cFile, 0xD0);
//delay_ms(10);//usleep(8000); //
// Read seconds
i2c_write_byte(i2cFile, 0x00);
//delay_ms(10);//usleep(8000); //
// usleep(1000); //
i2c_write_byte(i2cFile, 0x10);
//(10);//usleep(8000); //
i2c_close(i2cFile);
usleep(1000); //
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
char address;
int i2cFile;
int rx_seconds[1],rx_minutes[1],rx_hours[1],rx_day[1],rx_date[1],rx_month[1],rx_year[2],rx_control[1];
int seconds,seconds_1,seconds_2,minutes,hours,day,date,month,year,control;
int main()
{
DS1307_set_rtc_date();
usleep(1000);
while (1)
{
i2cFile=i2c_open(DS1307_BUS, 0x68);
usleep(1000); //
// i2c_write_byte(i2cFile, 0x02);
// i2c_write_byte(i2cFile, 0x02);
////Read Time/////////////////////////////////
// Read seconds
i2c_write_byte(i2cFile, 0x00);
i2c_read_byte(i2cFile, rx_seconds);
// Read Minutes
i2c_write_byte(i2cFile, 0x01);
i2c_read_byte(i2cFile, rx_minutes);
// Read Hours
i2c_write_byte(i2cFile, 0x02);
i2c_read_byte(i2cFile, rx_hours);
////Read Date,montn,year/////////////////////////////////
// Read Date
i2c_write_byte(i2cFile, 0x04);
i2c_read_byte(i2cFile, rx_date);
// Read Month
i2c_write_byte(i2cFile, 0x05);
i2c_read_byte(i2cFile, rx_month);
// Read year
i2c_write_byte(i2cFile, 0x06);
i2c_read_byte(i2cFile, rx_year);
i2c_close(i2cFile);
usleep(15000); // Read seconds
/// hours=bcd2int(rx_hours);
// minutes=bcd2int(rx_minutes);
// seconds=bcd2int(rx_seconds);
///Time/////////////////////////////
hours=rx_hours[0];
hours = (hours >> 4) * 10 + (hours & 0x0F);
minutes=rx_minutes[0];
minutes = (minutes >> 4) * 10 + (minutes & 0x0F);
seconds=rx_seconds[0];
seconds= (seconds >> 4) * 10 + (seconds & 0x0F);
///Date/////////////////////////////
date=rx_date[0];
date = (date >> 4) * 10 + (date & 0x0F);
month=rx_month[0];
month = (month >> 4) * 10 + (month & 0x0F);
year=rx_year[0];
year= (year >> 4) * 10 + (year & 0x0F);
//seconds_1=rx_seconds[0];
//seconds_1= (seconds_1 >> 4) * 10 + (seconds_1 & 0x0F);;
// seconds_2=BCD2LowerCh(rx_seconds);
printf("Time : Hours Minutes Seconds \n");
printf(" : %d %d %d \n",hours,minutes,seconds);
// printf(" \n");
printf(" \n");
printf("Date : Day Month Year \n");
printf(" : %d %d %d \n",date,month,year);
printf(" \n");
printf(" \n");
// printf("Seconds \n");
// printf(" %d \n",seconds_1);
usleep(15000); // Read seconds
}
}