element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Project14
  • Challenges & Projects
  • More
Project14
Forum Beaglebone AI I2C write issue with DS1307
  • Blog
  • Forum
  • Documents
  • Theme Suggestions
  • Polls
  • Members
  • More
  • Cancel
  • New
Join Project14 to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 1 reply
  • Subscribers 178 subscribers
  • Views 577 views
  • Users 0 members are here
Related

Beaglebone AI I2C write issue with DS1307

bhome
bhome over 5 years ago

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

 

 

}

}

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 5 years ago +2
    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…
Parents
  • shabaz
    shabaz over 5 years ago

    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

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • shabaz
    shabaz over 5 years ago

    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

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube