1 Reply Latest reply on Jun 27, 2020 9:21 AM by shabaz

    Beaglebone AI I2C write issue with DS1307

    bhome

      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

       

       

      }

      }

        • Re: Beaglebone AI I2C write issue with DS1307
          shabaz

          Hi Mahesh,

           

          There must be a hundred examples of DS1307 google'able on the Internet, I think there's no need to reinvent the wheel, you can use the code from the hundred other examples, find examples that ideally work with Linux and if needed modify slightly to suit your specific board.

          There's a chance your code may have errors in it. Some say there could be an error in every 10 lines of code. That means you could have maybe 10-12 errors. You can try to give your code a fighting chance by reducing the code - I'm not saying it will work, but sometimes there's a chance errors can be identified more easily with less code. To reduce the code, consider using loops and arrays and functions in C, those features are there for a reason : )

           

          Also, just as you may need syntax highlighting when writing code or viewing other people's tutorials or demo code, so do people reading your code.

          You can see how to use syntax highlighting here: How to Ask Questions using the Create Discussion tool