#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
// Shift register pin tanımlamaları
const int dataPin = 7; // DS
const int latchPin = 9; // ST_CP
const int clockPin = 8; // SH_CP
// CD74HC4067 pin tanımlamaları
const int S0 = 2;
const int S1 = 3;
const int S2 = 4;
const int S3 = 5;
const int Z = 6;
// Buton pin tanımlamaları
const int hourButtonPin = 10;
const int minuteButtonPin = 11;
// 7 segment rakam kodları
const byte numToSegment[] = {
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111 // 9
};
unsigned long lastUpdate = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC pilini takın!");
rtc.adjust(DateTime(2024, 10, 9, 12, 0, 0));
}
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(Z, OUTPUT);
pinMode(hourButtonPin, INPUT_PULLUP);
pinMode(minuteButtonPin, INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
// Saat butonu
if (digitalRead(hourButtonPin) == LOW) {
int newHour = (now.hour() + 1) % 24;
rtc.adjust(DateTime(now.year(), now.month(), now.day(), newHour, now.minute(), now.second()));
updateDisplays();
delay(250);
return;
}
// Dakika butonu
if (digitalRead(minuteButtonPin) == LOW) {
int newMinute = (now.minute() + 1) % 60;
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), newMinute, 0));
updateDisplays();
delay(250);
return;
}
// Normal calisma: saniyede bir guncelle
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
updateDisplays();
}
}
void updateDisplays() {
DateTime now = rtc.now();
int tens = now.minute() / 10;
int ones = now.minute() % 10;
shiftOutDisplay(ones, tens);
updateHourLED(now.hour());
}
void shiftOutDisplay(int ones, int tens) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, numToSegment[ones]);
shiftOut(dataPin, clockPin, MSBFIRST, numToSegment[tens]);
digitalWrite(latchPin, HIGH);
}
void shiftOut(int dataPin, int clockPin, int bitOrder, byte val) {
for (int i = 0; i < 8; i++) {
if (bitOrder == MSBFIRST) {
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
} else {
digitalWrite(dataPin, !!(val & (1 << i)));
}
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
}
void updateHourLED(int hour) {
hour = hour % 12;
if (hour == 0) {
hour = 12;
}
selectChannel(hour);
}
void selectChannel(int channel) {
digitalWrite(Z, LOW);
digitalWrite(S0, channel & 0x01);
digitalWrite(S1, (channel >> 1) & 0x01);
digitalWrite(S2, (channel >> 2) & 0x01);
digitalWrite(S3, (channel >> 3) & 0x01);
digitalWrite(Z, HIGH);
}Loading
cd74hc4067
cd74hc4067