Advertisement
bal_gennady

clock with Nokia 5110 LCD and DS1307

Jan 14th, 2022
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Arduino real time clock with Nokia 5110 LCD and DS1307.
  3.  * This is a free software with NO WARRANTY.
  4.  * https://simple-circuit.com/
  5.  */
  6.  
  7. #include <Wire.h>              // include Wire library (required for I2C devices)
  8. #include <SPI.h>               // include SPI library
  9. #include <Adafruit_GFX.h>      // include adafruit graphics library
  10. #include <Adafruit_PCD8544.h>  // include adafruit PCD8544 (Nokia 5110) library
  11.  
  12. // Nokia 5110 LCD module connections (CLK, DIN, D/C, CS, RST)
  13. Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
  14.  
  15. // button definitions
  16. #define button1   9   // button B1 is connected to Arduino pin 9
  17. #define button2   8   // button B2 is connected to Arduino pin 8
  18.  
  19. void setup()
  20. {
  21.   pinMode(button1, INPUT_PULLUP);
  22.   pinMode(button2, INPUT_PULLUP);
  23.   delay(1000);   // wait 1 second
  24.   Wire.begin();  // join I2C bus
  25.  
  26.   // initialize the display
  27.   display.begin();
  28.  
  29.   // you can change the contrast around to adapt the display
  30.   // for the best viewing!
  31.   display.setContrast(50);
  32.  
  33.   display.clearDisplay();   // clear the screen and buffer
  34.   display.display();
  35.  
  36.   display.setTextSize(1);
  37.   display.setTextColor(BLACK, WHITE);
  38.   display.setCursor(3, 0);
  39.   display.println("ARDUINO CLOCK");
  40.   display.print("NOKIA 5110 LCD");
  41.   display.setCursor(28, 32);
  42.   display.print("TIME:");
  43.   display.display();
  44. }
  45.  
  46. // variable declarations
  47. char Time[] = "  :  :  ";
  48. char Date[] = "  /  /20  ";
  49. unsigned short i, second, minute, hour, w_day, day, month, year;
  50.  
  51. // a small function for button1 (B1) debounce
  52. bool debounce ()
  53. {
  54.   byte count = 0;
  55.   for(byte i = 0; i < 5; i++)
  56.   {
  57.     if ( !digitalRead(button1) )
  58.       count++;
  59.     delay(10);
  60.   }
  61.  
  62.   if(count > 2)  return 1;
  63.   else           return 0;
  64. }
  65.  
  66. // function for display day of the week
  67. void day_display()
  68. {
  69.   switch(w_day)
  70.   {
  71.     case 1:  draw_text(12, 16, "  SUNDAY  "); break;
  72.     case 2:  draw_text(12, 16, "  MONDAY  "); break;
  73.     case 3:  draw_text(15, 16, " TUESDAY ");  break;
  74.     case 4:  draw_text(15, 16, "WEDNESDAY");  break;
  75.     case 5:  draw_text(12, 16, " THURSDAY "); break;
  76.     case 6:  draw_text(12, 16, "  FRIDAY  "); break;
  77.     default: draw_text(12, 16, " SATURDAY ");
  78.   }
  79. }
  80.  
  81. void RTC_display()
  82. {
  83.   // convert BCD to decimal
  84.   second = (second >> 4) * 10 + (second & 0x0F);
  85.   minute = (minute >> 4) * 10 + (minute & 0x0F);
  86.   hour   = (hour   >> 4) * 10 + (hour   & 0x0F);
  87.   day    = (day    >> 4) * 10 + (day    & 0x0F);
  88.   month  = (month  >> 4) * 10 + (month  & 0x0F);
  89.   year   = (year   >> 4) * 10 + (year   & 0x0F);
  90.   // end conversion
  91.  
  92.   // update time array
  93.   Time[7] = second % 10 + '0';
  94.   Time[6] = second / 10 + '0';
  95.   Time[4] = minute % 10 + '0';
  96.   Time[3] = minute / 10 + '0';
  97.   Time[1] = hour   % 10 + '0';
  98.   Time[0] = hour   / 10 + '0';
  99.  
  100.   // update date array
  101.   Date[9] = year   % 10 + '0';
  102.   Date[8] = year   / 10 + '0';
  103.   Date[4] = month  % 10 + '0';
  104.   Date[3] = month  / 10 + '0';
  105.   Date[1] = day    % 10 + '0';
  106.   Date[0] = day    / 10 + '0';
  107.  
  108.   draw_text(12,  24, Date);  // print date
  109.   draw_text(18, 40, Time);   // print time
  110. }
  111.  
  112. void blink_parameter()
  113. {
  114.   byte j = 0;
  115.   while(j < 100 && digitalRead(button1) && digitalRead(button2))
  116.   {
  117.     j++;
  118.     delay(5);
  119.   }
  120. }
  121.  
  122. byte edit(byte x_pos, byte y_pos, byte parameter)
  123. {
  124.   char text[3];
  125.   sprintf(text,"%02u", parameter);
  126.   while(debounce());  // call debounce function (wait for B1 to be released)
  127.  
  128.   while(1)
  129.   {
  130.     while(!digitalRead(button2))
  131.     {
  132.       parameter++;
  133.       if(i == 0 && parameter > 31)   // if date > 31 ==> date = 1
  134.         parameter = 1;
  135.       if(i == 1 && parameter > 12)   // if month > 12 ==> month = 1
  136.         parameter = 1;
  137.       if(i == 2 && parameter > 99)   // if year > 99 ==> year = 0
  138.         parameter = 0;
  139.       if(i == 3 && parameter > 23)   // if hours > 23 ==> hours = 0
  140.         parameter = 0;
  141.       if(i == 4 && parameter > 59)   // if minutes > 59 ==> minutes = 0
  142.         parameter = 0;
  143.      
  144.       sprintf(text,"%02u", parameter);
  145.       draw_text(x_pos, y_pos, text);
  146.      
  147.       delay(200);   // wait 200ms
  148.     }
  149.  
  150.     draw_text(x_pos, y_pos, "  ");
  151.     blink_parameter();
  152.    
  153.     draw_text(x_pos, y_pos, text);
  154.     blink_parameter();
  155.  
  156.     if(!digitalRead(button1)) // if button B1 is pressed
  157.     if(debounce())            // call debounce function (make sure if B1 is pressed)
  158.     {
  159.       i++;                // increment 'i' for the next parameter
  160.       return parameter;   // return parameter value and exit
  161.     }
  162.   }
  163. }
  164.  
  165. // print text on the LCD
  166. void draw_text(byte x_pos, byte y_pos, char *text)
  167. {
  168.   display.setCursor(x_pos, y_pos);
  169.   display.print(text);
  170.   display.display();
  171. }
  172.  
  173. // main loop
  174. void loop()
  175. {
  176.   if(!digitalRead(button1))     // if button B1 is pressed
  177.     if(debounce())   // call debounce function (make sure if B1 is pressed)
  178.     {
  179.       i = 0;
  180.       while(debounce());  // call debounce function (wait for button B1 to be released)
  181.       while(1)
  182.       {
  183.         while( !digitalRead(button2) )    // while button B2 pressed
  184.         {
  185.           w_day++;         // increment w_day
  186.           if(w_day > 7)
  187.             w_day = 1;
  188.           day_display();   // call day_display function
  189.           delay(500);   // wait 500 ms
  190.         }
  191.         draw_text(15, 16, "         ");
  192.         blink_parameter();   // call blink_parameter function
  193.  
  194.         day_display();      // call day_display function
  195.         blink_parameter();  // call blink_parameter function
  196.         if( !digitalRead(button1) )  // if button B1 is pressed
  197.           break;
  198.       }
  199.  
  200.     day    = edit(12, 24, day);    // edit date
  201.     month  = edit(30, 24, month);  // edit month
  202.     year   = edit(60, 24, year);   // edit year
  203.     hour   = edit(18, 40, hour);   // edit hours
  204.     minute = edit(36, 40, minute); // edit minutes
  205.  
  206.     // convert decimal to BCD
  207.     minute = ((minute / 10) << 4) + (minute % 10);
  208.     hour   = ((hour   / 10) << 4) + (hour   % 10);
  209.     day    = ((day    / 10) << 4) + (day    % 10);
  210.     month  = ((month  / 10) << 4) + (month  % 10);
  211.     year   = ((year   / 10) << 4) + (year   % 10);
  212.     // end conversion
  213.  
  214.     while(debounce());  // call debounce function (wait for button B1 to be released)
  215.  
  216.     // write data to DS1307 RTC
  217.     Wire.beginTransmission(0x68);  // start I2C protocol with DS1307 address
  218.     Wire.write(0);       // send register address
  219.     Wire.write(0);       // reset sesonds and start oscillator
  220.     Wire.write(minute);  // write minute
  221.     Wire.write(hour);    // write hour
  222.     Wire.write(w_day);   // write day
  223.     Wire.write(day);     // write date
  224.     Wire.write(month);   // write month
  225.     Wire.write(year);    // write year
  226.     Wire.endTransmission();  // stop transmission and release the I2C bus
  227.  
  228.     delay(200); // wait 200ms
  229.   }
  230.  
  231.   Wire.beginTransmission(0x68); // start I2C protocol with DS1307 address
  232.   Wire.write(0);                // send register address
  233.   Wire.endTransmission(false);  // I2C restart
  234.   Wire.requestFrom(0x68, 7);    // request 7 bytes from DS1307 and release I2C bus at end of reading
  235.   second = Wire.read();         // read seconds from register 0
  236.   minute = Wire.read();         // read minuts from register 1
  237.   hour   = Wire.read();         // read hour from register 2
  238.   w_day  = Wire.read();         // read day from register 3
  239.   day    = Wire.read();         // read date from register 4
  240.   month  = Wire.read();         // read month from register 5
  241.   year   = Wire.read();         // read year from register 6
  242.  
  243.   day_display();    // print day of the week
  244.   RTC_display();    // print time & date
  245.  
  246.   delay(100);   // wait 100 ms
  247. }
  248. // end of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement