ضبط الساعة المزدوجة باستخدام DS3231 على شاشة 1.8 بوصة ST7735 يشرح هذا المشروع كيفية استخدام الساعة المزدوجة باستخدام DS3231 على شاشة عرض ST7735 مقاس 1.8 بوصة. تعرض لك هذه الساعة ساعتان: التوقيت المحلي ووقت UTC / GMT. يحتوي على 3 أزرار لـ MENU / OK ، زيادة وتقليل القيمة للساعات والدقائق واليوم في الأسبوع والسنة والشهر واليوم والفرق بين التوقيت المحلي والتوقيت العالمي المنسق (توقيت غرينتش). التخطيطي بسيط:
------------------------------------------------------------
------------------------------------------------------------
// Project : Adjusting_Dual_Clock_using_DS3231_on_1.8_ST7735_Display // By : DIY Channel / ABDELLATIF MIMOUNE // My Youtube Channel : https://www.youtube.com/c/DIYChannel2019 #include <TFT_ST7735.h> #include <SPI.h> #include "EEPROM.h" TFT_ST7735 tft = TFT_ST7735(); //------ abdellatif---------------------- uint32_t targetTime = 0; byte omm = 99; boolean initial = 1; byte xcolon = 0; byte xcolon2 = 0; unsigned int colour = 0; static uint8_t conv2d(const char* p) { uint8_t v = 0; if ('0' <= *p && *p <= '9') v = *p - '0'; return 10 * v + *++p - '0'; } //------------mimoune------------------------- #include <Wire.h> char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; byte hh, mm, ss; byte yy, ll, dd, zz; #define DS3231_I2C_ADDRESS 104 byte tMSB, tLSB; float temp3231; float temperatura, temperatura0; #define meniu 2 #define minus 3 #define plus 4 int nivel = 0; byte jos = 10; //byte hh1, mm1, zz1, dd1, ll1, yy1; int hh1, mm1, zz1, dd1, ll1, yy1; byte maxday; int hh2; int hh3; byte hh4; byte semn; void setup(void) { tft.init(); tft.setRotation(1); tft.fillScreen(ST7735_BLACK); tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); pinMode(meniu, INPUT); pinMode(plus, INPUT); pinMode(minus, INPUT); digitalWrite(meniu, HIGH); digitalWrite(plus, HIGH); digitalWrite(minus, HIGH); Serial.begin(9600); Wire.begin(); targetTime = millis() + 1000; hh4 = EEPROM.read(100); semn = EEPROM.read(101); if ((hh4 < 0) || (hh4 > 12)) hh4 = 2; if ((semn < 0) || (semn > 1)) semn = 1; if (semn == 1) hh3 = hh4; if (semn == 0) hh3 = -hh4; hh2 = hh - hh3; } void loop() { if (nivel == 0) { if (digitalRead(meniu) == LOW) { nivel = nivel+1; delay(500); hh1=hh; sterg(); } tft.setTextSize(1); if (targetTime < millis()) { targetTime = millis()+1000; readDS3231time(&ss, &mm, &hh, &zz, &dd, &ll,&yy); hh2 = hh - hh3; if (hh2 > 23) hh2 = hh2-24; if (hh2 < 0) hh2 = 24+hh2; tft.setTextSize(1); if (ss==0 || initial) { for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(0, y, tft.width(), ST7735_BLACK); } initial = 0; tft.setTextColor(ST7735_GREEN, ST7735_BLACK); tft.setCursor (15, 52+jos); tft.print(daysOfTheWeek[zz]); tft.setCursor (90, 52+jos); if (dd <10) tft.print("0"); tft.print(dd); tft.print("."); if (ll <10) tft.print("0"); tft.print(ll); tft.print("."); tft.print(yy+2000); } byte xpos = 16; byte ypos = 0+jos; byte xpos2 = 16; byte ypos2 = 65+jos; tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); tft.setCursor (0, 0); tft.print("GMT"); if (hh3 == 0) tft.print("="); if (hh3 > 0) tft.print("+"); tft.print(hh3); tft.setCursor (0, ypos2+2); tft.print("G"); tft.setCursor (0, ypos2+10); tft.print("M"); tft.setCursor (0, ypos2+18); tft.print("T"); if (omm != mm) { if (digitalRead(meniu) == LOW) { nivel = nivel+1; tft.fillScreen(ST7735_BLACK); delay(500); tft.fillScreen(ST7735_BLACK); } tft.setTextColor(ST7735_BLACK, ST7735_BLACK); tft.drawString("88:88",xpos,ypos,7); tft.drawString("88:88",xpos2,ypos2,7); tft.setTextColor(ST7735_WHITE, ST7735_BLACK); omm = mm; if (hh<10) xpos+= tft.drawChar('0',xpos,ypos,7); xpos+= tft.drawNumber(hh,xpos,ypos,7); xcolon=xpos; xpos+= tft.drawChar(':',xpos,ypos,7); if (mm<10) xpos+= tft.drawChar('0',xpos,ypos,7); tft.drawNumber(mm,xpos,ypos,7); // second clock tft.setTextColor(ST7735_RED, ST7735_BLACK); // Red if (hh2<10) xpos2+= tft.drawChar('0',xpos2,ypos2,7); xpos2+= tft.drawNumber(hh2,xpos2,ypos2,7); xcolon2=xpos2; xpos2+= tft.drawChar(':',xpos2,ypos2,7); if (mm<10) xpos2+= tft.drawChar('0',xpos2,ypos2,7); tft.drawNumber(mm,xpos2,ypos2,7); } //animated points for clocks if (ss%2) { tft.setTextColor(0x39C4, ST7735_BLACK); xpos2+= tft.drawChar(':',xcolon2,ypos2,7); xpos+= tft.drawChar(':',xcolon,ypos,7); } else { tft.setTextColor(ST7735_RED, ST7735_BLACK); tft.drawChar(':',xcolon2,ypos2,7); tft.setTextColor(ST7735_RED, ST7735_BLACK); tft.drawChar(':',xcolon,ypos,7); } } } if (nivel == 1) { if (digitalRead(meniu) == LOW) { nivel = nivel+1; delay(500); mm1 = mm; sterg(); } byte xpos = 6; byte ypos = 0+jos; tft.setTextColor(ST7735_BLACK, ST7735_BLACK); tft.drawString("88",xpos,ypos,7); tft.setTextColor(ST7735_RED, ST7735_BLACK); delay(50); if (hh1<10) xpos+= tft.drawChar('0',xpos,ypos,7); xpos+= tft.drawNumber(hh1,xpos,ypos,7); tft.setTextColor(ST7735_RED, ST7735_BLACK); xcolon=xpos; xpos+= tft.drawChar(':',xpos,ypos,7); if (mm<10) xpos+= tft.drawChar('0',xpos,ypos,7); tft.drawNumber(mm,xpos,ypos,7); delay(50); if (digitalRead(plus) == LOW) { hh1 = hh1+1; delay(150); } if (digitalRead(minus) == LOW) { hh1 = hh1-1; delay(150); } if (hh1 > 23) hh1 = 0; if (hh1 < 0) hh1 = 23; } if (nivel == 2) { if (digitalRead(meniu) == LOW) { delay(500); zz1 = zz; sterg(); nivel = nivel+1; } byte xpos = 6; byte ypos = 0+jos; tft.setTextColor(ST7735_RED, ST7735_BLACK); // Red if (hh1<10) xpos+= tft.drawChar('0',xpos,ypos,7); xpos+= tft.drawNumber(hh1,xpos,ypos,7); xcolon=xpos; xpos+= tft.drawChar(':',xpos,ypos,7); tft.setTextColor(ST7735_BLACK, ST7735_BLACK); tft.drawString("88",xpos,ypos,7); delay(50); tft.setTextColor(ST7735_RED, ST7735_BLACK); if (mm1<10) xpos+= tft.drawChar('0',xpos,ypos,7); tft.drawNumber(mm1,xpos,ypos,7); delay(50); if (digitalRead(plus) == LOW) { mm1 = mm1+1; delay(150); } if (digitalRead(minus) == LOW) { mm1 = mm1-1; delay(150); } if (mm1 > 59) mm1 = 0; if (mm1 < 0) mm1 = 59; } if (nivel == 3) { if (digitalRead(meniu) == LOW) { nivel = nivel+1; delay(500); yy1 = yy;; sterg(); } for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(0, y, tft.width(), ST7735_BLACK); } delay(50); tft.setTextColor(ST7735_GREEN, ST7735_BLACK); tft.setCursor (15, 52+jos); tft.print(daysOfTheWeek[zz1]); delay(50); if (digitalRead(plus) == LOW) { zz1 = zz1+1; delay(150); } if (digitalRead(minus) == LOW) { zz1 = zz1-1; delay(150); } if (zz1 > 6) zz1 = 0; if (zz1 < 0) zz1 = 6; Serial.println(zz1); } if (nivel == 4) { if (digitalRead(meniu) == LOW) { delay(500); ll1 = ll;; sterg(); nivel = nivel+1; } for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(125, y, 35, ST7735_BLACK); } delay(50); tft.setTextColor(ST7735_GREEN, ST7735_BLACK); tft.setCursor (90, 52+jos); if (dd <10) tft.print("0"); tft.print(dd); tft.print("."); if (ll <10) tft.print("0"); tft.print(ll); tft.print("."); tft.print(yy1+2000); delay(50); if (digitalRead(plus) == LOW) { yy1 = yy1+1; delay(150); } if (digitalRead(minus) == LOW) { yy1 = yy1-1; delay(150); } if (yy1 > 49) yy1 = 49; if (yy1 < 16) yy1 = 16; } if (nivel == 5) { if (digitalRead(meniu) == LOW) { delay(500); dd1 = dd;; sterg(); nivel = nivel+1; } for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(106, y, 14, ST7735_BLACK); } delay(50); tft.setCursor (90, 52+jos); if (dd <10) tft.print("0"); tft.print(dd); tft.print("."); if (ll1 <10) tft.print("0"); tft.print(ll1); tft.print("."); tft.print(yy1+2000); delay(50); if (digitalRead(plus) == LOW) { ll1 = ll1+1; delay(150); } if (digitalRead(minus) == LOW) { ll1 = ll1-1; delay(150); } if (ll1 > 12) ll1 = 1; if (ll1 < 1) ll1 = 12; } if (nivel == 6) { if (digitalRead(meniu) == LOW) { delay(500); sterg(); nivel = nivel+1; } for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(90, y, 14, ST7735_BLACK); } delay(50); tft.setCursor (90, 52+jos); if (dd1 <10) tft.print("0"); tft.print(dd1); tft.print("."); if (ll1 <10) tft.print("0"); tft.print(ll1); tft.print("."); tft.print(yy1+2000); delay(50); if (digitalRead(plus) == LOW) { dd1 = dd1+1; delay(150); } if (digitalRead(minus) == LOW) { dd1 = dd1-1; delay(150); } if (ll == 4 || ll == 5 || ll == 9 || ll == 11) { maxday = 30; } else { maxday = 31; } if (ll ==2 && yy % 4 ==0) { maxday = 29; } if (ll ==2 && ll % 4 !=0) { maxday = 28; } if (dd1 > maxday) dd1 = 1; if (dd1 < 1) dd1 = maxday; } if (nivel == 7) // change hours { if (digitalRead(meniu) == LOW) { delay(500); sterg(); nivel = nivel+1; } tft.setCursor (90, 42+jos); tft.print("GMT"); for (int y=52+jos; y < 60+jos; y+=1) { tft.drawFastHLine(90, y, 69, ST7735_BLACK); } delay(50); tft.setCursor (90, 52+jos); if (hh3 > 0) tft.print("+"); tft.print(hh3); delay(50); if (digitalRead(plus) == LOW) { hh3 = hh3+1; delay(150); } if (digitalRead(minus) == LOW) { hh3 = hh3-1; delay(150); } if (hh3 > 12) hh3 = 12; if (hh3 < -12) hh3 = -12; } if (nivel == 8) { sterg(); setDS3231time(0, mm1, hh1, zz1, dd1, ll1, yy1); if (hh3 > 0) { hh4 = hh3; semn = 1; } if (hh3 <0) { hh4 = -hh3; semn = 0; } if (hh3 == 0) { hh4 = 0; semn = 1; } EEPROM.write(100, hh4); EEPROM.write(101, semn); nivel = 9; } if (nivel >=9) {nivel = 0; omm = 99; temperatura0 = 0.0; initial = 1; } } void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) { Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } byte bcdToDec(byte val) { return ( (val / 16 * 10) + (val % 16) ); } // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) { return ( (val / 10 * 16) + (val % 10) ); } void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void sterg() {for (int y=0; y < 127; y+=1) { tft.drawFastHLine(0, y, tft.width(), ST7735_BLACK); } }
No comments:
Post a Comment