-
-
Save Manawyrm/de3499eed549ea305c3f to your computer and use it in GitHub Desktop.
Arduino ISR Clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TimerOne.h> | |
int hour = 0; | |
int minute = 0; | |
int second = 01; | |
int latchPin = A2; | |
int clockPin = A1; | |
int dataPin = A0; | |
byte numbers[] = { | |
B11111101, | |
B01100001, | |
B11011011, | |
B11110011, | |
B01100111, | |
B10110111, | |
B10111111, | |
B11100001, | |
B11111111, | |
B11110111 | |
}; | |
void setup() | |
{ | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
Timer1.initialize(1000000); | |
Timer1.attachInterrupt( timerIsr ); | |
Serial.begin(57600); | |
} | |
void loop() | |
{ | |
if(Serial.available() >= 4) | |
{ | |
int action = Serial.read(); | |
int newhour = Serial.read(); | |
int newminute = Serial.read(); | |
int newsecond = Serial.read(); | |
if (action == 1) | |
{ | |
hour = newhour; | |
minute = newminute; | |
second = newsecond; | |
Serial.write(15); | |
} | |
} | |
digitalWrite(latchPin, LOW); | |
char chSecond[2]; | |
sprintf(chSecond, "%02d", second); | |
char chMinute[2]; | |
sprintf(chMinute, "%02d", minute); | |
char chHour[2]; | |
sprintf(chHour, "%02d", hour); | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chSecond[1] - 48]); //Sekunde 2 | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chSecond[0] - 48]); //Sekunde 1 | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chMinute[1] - 48]); //Minute 2 | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chMinute[0] - 48]); //Minute 1 | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chHour[1] - 48]); //Stunde 2 | |
shiftOut(dataPin, clockPin, MSBFIRST, ~numbers[(int)chHour[0] - 48]); //Stunde 1 | |
digitalWrite(latchPin, HIGH); | |
delay(10); | |
} | |
void timerIsr() | |
{ | |
second++; | |
if(second == 60) | |
{ | |
second = 0; | |
minute++; | |
} | |
if(minute == 60) | |
{ | |
minute = 0; | |
hour++; | |
} | |
if(hour == 24) | |
{ | |
hour = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment