//**************************************************************//
// Name : shiftOutCode, 4-Digit 7Segment //
// Author : AbdolHakim Esmaeili, Iran, AqQaleh //
// Date : 05 Mar, 2020 //
// Version : 1.0 //
// Notes : Code for using two 74HC595 Shift Register //
// : to show clock using only 3 wire //
//****************************************************************
//RTC-DS1307 Wiring
//SCL to A5 Arduino Pro Mini Or Uno
//SDA to A4 Arduino Pro Mini Or Uno
//GND to GND
//VCC to VCC
#include <RTClib.h>
RTC_DS1307 rtc;
//Pin connected to ST_CP of 74HC595
int latchPin = 6;
//Pin connected to SH_CP of 74HC595
int clockPin = 7;
////Pin connected to DS of 74HC595
int dataPin = 5;
//Common Anode
const byte Num[13]={0b00000011,0b10011111,0b00100101,0b00001101,0b10011001,0b01001001,0b01000001,0b00011111,0b00000001,0b00001001,0b11111110,0b00111111,0b11011111};//0,1,2,3,4,5,6,7,8,9,Dot,:,Degree
const byte Pos[5]={0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};//L,Dig1,Dig2,Dig3,Dig4
unsigned long startMillis;
unsigned long currentMillis;
int HH;
int MM;
unsigned int Dly1 = 4;
unsigned int Dly2 = 1;
void setup() {
if (! rtc.begin())
{
while (1);
}
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
startMillis = millis(); //initial start time
}
void loop() {
DateTime now = rtc.now();
HH=now.hour();
MM=now.minute();
TimeShow(HH,MM);
}
void Segment(int number,int digit){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, Num[number]);
shiftOut(dataPin, clockPin, LSBFIRST, Pos[digit]);
digitalWrite(latchPin, HIGH);
}
void DoubleDot(){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, Num[11]);
shiftOut(dataPin, clockPin, LSBFIRST, Pos[0]);
digitalWrite(latchPin, HIGH);
delay(1);
}
void TimeShow(int hh,int mm){
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
int dig4=hh/10;
int dig3=hh%10;
int dig2=mm/10;
int dig1=mm%10;
if (currentMillis - startMillis <= 500) //test whether the period has elapsed
{
Segment(dig4,4);
delay(Dly1);
Segment(dig3,3);
delay(Dly1);
Segment(dig2,2);
delay(Dly1);
Segment(dig1,1);
delay(Dly1);
DoubleDot();
}
else
{
Segment(dig4,4);
delay(Dly2);
Segment(dig3,3);
delay(Dly2);
Segment(dig2,2);
delay(Dly2);
Segment(dig1,1);
}
if (currentMillis - startMillis >= 1000)
{
startMillis = currentMillis; //IMPORTANT to save the start time of the current state
}
[FONT=Yekan]}[/FONT]