#include #define LCD_ADDR 0x3D // 7-bit Adresse (SA0=VDD) #define RESET_PIN 8 // Zeitvariablen int sekunde = 0, minute = 0, stunde = 0; int tag = 1, monat = 1, jahr = 2025; unsigned long letzteAktualisierung = 0; // Serielle Eingabe String inputBuffer = ""; void lcdCommand(uint8_t cmd, bool cont = false) { Wire.write(cont ? 0x80 : 0x00); // Control Byte: Co=cont, RS=0 Wire.write(cmd); } void lcdData(uint8_t data) { Wire.beginTransmission(LCD_ADDR); Wire.write(0x40); // Control Byte: Co=0, RS=1 Wire.write(data); Wire.endTransmission(); } void setContrast(uint8_t contrast) { // Contrast = 0–63 → C5/C4 (oberste 2 Bits), C3–C0 (unterste 4 Bits) uint8_t c5_c4 = (contrast >> 4) & 0x03; uint8_t c3_c0 = contrast & 0x0F; Wire.beginTransmission(LCD_ADDR); lcdCommand(0x39, true); // Function Set (RE=0, IS=1) lcdCommand(0x54 | c5_c4, true); // Power Control (C5/C4) lcdCommand(0x70 | c3_c0, true); // Contrast Set (C3–C0) lcdCommand(0x38, false); // Function Set (RE=0, IS=0) Wire.endTransmission(); } void lcdInit() { digitalWrite(RESET_PIN, LOW); delay(5); digitalWrite(RESET_PIN, HIGH); delay(5); Wire.beginTransmission(LCD_ADDR); lcdCommand(0x3A, true); // Function Set: 8-bit, RE=1 lcdCommand(0x09, true); // 4-line display lcdCommand(0x06, true); // Entry mode (bottom view) lcdCommand(0x1E, true); // Bias lcdCommand(0x39, true); // Function Set (RE=0, IS=1) lcdCommand(0x1B, true); // Internal OSC lcdCommand(0x6E, true); // Follower control lcdCommand(0x57, true); // Power control lcdCommand(0x72, true); // Contrast Set default lcdCommand(0x38, false);// Function Set (RE=0, IS=0) Wire.endTransmission(); delay(10); lcdCommandSingle(0x01); // Clear Display delay(2); lcdCommandSingle(0x0C); // Display ON, Cursor OFF, Blink OFF } void lcdCommandSingle(uint8_t cmd) { Wire.beginTransmission(LCD_ADDR); lcdCommand(cmd, false); Wire.endTransmission(); } void lcdSetCursor(uint8_t line, uint8_t pos) { uint8_t addr = 0x80; switch (line) { case 2: addr = 0xA0; break; case 3: addr = 0xC0; break; case 4: addr = 0xE0; break; } lcdCommandSingle(addr + pos); } void lcdPrint(const char* text) { while (*text) lcdData(*text++); } void updateClock() { unsigned long jetzt = millis(); if (jetzt - letzteAktualisierung >= 1000) { letzteAktualisierung = jetzt; sekunde++; if (sekunde >= 60) { sekunde = 0; minute++; if (minute >= 60) { minute = 0; stunde++; if (stunde >= 24) { stunde = 0; tag++; int maxTage = 31; if (monat == 4 || monat == 6 || monat == 9 || monat == 11) maxTage = 30; else if (monat == 2) maxTage = 28; if (tag > maxTage) { tag = 1; monat++; if (monat > 12) { monat = 1; jahr++; } } } } } char datum[17]; snprintf(datum, sizeof(datum), "%02d.%02d.%04d", tag, monat, jahr); lcdSetCursor(3, 0); lcdPrint("Datum: "); lcdSetCursor(3, 7); lcdPrint(datum); char uhr[17]; snprintf(uhr, sizeof(uhr), "%02d:%02d:%02d", stunde, minute, sekunde); lcdSetCursor(4, 0); lcdPrint("Uhrzeit: "); lcdSetCursor(4, 5); lcdPrint(uhr); } } void parseSerialInput() { while (Serial.available()) { char c = Serial.read(); if (c == '\n' || c == '\r') { if (inputBuffer.startsWith("SET ")) { int d, m, y, h, min, s; if (sscanf(inputBuffer.c_str(), "SET %d.%d.%d %d:%d:%d", &d, &m, &y, &h, &min, &s) == 6) { tag = constrain(d, 1, 31); monat = constrain(m, 1, 12); jahr = constrain(y, 2000, 2099); stunde = constrain(h, 0, 23); minute = constrain(min, 0, 59); sekunde = constrain(s, 0, 59); lcdSetCursor(1, 0); lcdPrint("Zeit gesetzt! "); } else { lcdSetCursor(1, 0); lcdPrint("Fehler Eingabe! "); } } inputBuffer = ""; } else { inputBuffer += c; } } } void setup() { pinMode(RESET_PIN, OUTPUT); Wire.begin(); Serial.begin(9600); delay(100); lcdInit(); setContrast(50); // Fester Kontrastwert lcdSetCursor(1, 0); lcdPrint("DOGM204 Zeit-Demo"); lcdSetCursor(2, 0); lcdPrint("I2C-Display"); Serial.println("Gib ein: SET TT.MM.JJJJ HH:MM:SS"); } void loop() { updateClock(); parseSerialInput(); }