Advertisement
KRITSADA

Arduino UNO with DS3231 RTC

Oct 7th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include "Wire.h"
  2. #define DS3231_I2C_ADDRESS 0x68
  3. // Convert normal decimal numbers to binary coded decimal
  4. byte decToBcd(byte val)
  5. {
  6.   return( (val/10*16) + (val%10) );
  7. }
  8. // Convert binary coded decimal to normal decimal numbers
  9. byte bcdToDec(byte val)
  10. {
  11.   return( (val/16*10) + (val%16) );
  12. }
  13. void setup()
  14. {
  15.   Wire.begin();
  16.   Serial.begin(9600);
  17.   // set the initial time here:
  18.   // DS3231 seconds, minutes, hours, day, date, month, year
  19.   setDS3231time(00,16,1,4,17,8,16);
  20. }
  21. void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
  22. {
  23.   // sets time and date data to DS3231
  24.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  25.   Wire.write(0); // set next input to start at the seconds register
  26.   Wire.write(decToBcd(second)); // set seconds
  27.   Wire.write(decToBcd(minute)); // set minutes
  28.   Wire.write(decToBcd(hour)); // set hours
  29.   Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  30.   Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  31.   Wire.write(decToBcd(month)); // set month
  32.   Wire.write(decToBcd(year)); // set year (0 to 99)
  33.   Wire.endTransmission();
  34. }
  35. void readDS3231time(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
  36. {
  37.   Wire.beginTransmission(DS3231_I2C_ADDRESS);
  38.   Wire.write(0); // set DS3231 register pointer to 00h
  39.   Wire.endTransmission();
  40.   Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  41.   // request seven bytes of data from DS3231 starting from register 00h
  42.   *second = bcdToDec(Wire.read() & 0x7f);
  43.   *minute = bcdToDec(Wire.read());
  44.   *hour = bcdToDec(Wire.read() & 0x3f);
  45.   *dayOfWeek = bcdToDec(Wire.read());
  46.   *dayOfMonth = bcdToDec(Wire.read());
  47.   *month = bcdToDec(Wire.read());
  48.   *year = bcdToDec(Wire.read());
  49. }
  50. void displayTime()
  51. {
  52.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  53.   // retrieve data from DS3231
  54.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);
  55.   // send it to the serial monitor
  56.   Serial.print(hour, DEC);
  57.   // convert the byte variable to a decimal number when displayed
  58.   Serial.print(":");
  59.   if (minute<10)
  60.   {
  61.     Serial.print("0");
  62.   }
  63.   Serial.print(minute, DEC);
  64.   Serial.print(":");
  65.   if (second<10)
  66.   {
  67.     Serial.print("0");
  68.   }
  69.   Serial.print(second, DEC);
  70.   Serial.print(" ");
  71.   Serial.print(dayOfMonth, DEC);
  72.   Serial.print("/");
  73.   Serial.print(month, DEC);
  74.   Serial.print("/");
  75.   Serial.print(year, DEC);
  76.   Serial.print(" Day of week: ");
  77.   switch(dayOfWeek){
  78.   case 1:
  79.     Serial.println("Sunday");
  80.     break;
  81.   case 2:
  82.     Serial.println("Monday");
  83.     break;
  84.   case 3:
  85.     Serial.println("Tuesday");
  86.     break;
  87.   case 4:
  88.     Serial.println("Wednesday");
  89.     break;
  90.   case 5:
  91.     Serial.println("Thursday");
  92.     break;
  93.   case 6:
  94.     Serial.println("Friday");
  95.     break;
  96.   case 7:
  97.     Serial.println("Saturday");
  98.     break;
  99.   }
  100.  
  101. }
  102. void loop()
  103. {
  104.   displayTime(); // display the real-time clock data on the Serial Monitor,
  105.   delay(1000); // every second
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement