Skip to content

Instantly share code, notes, and snippets.

@Manawyrm
Created October 24, 2012 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manawyrm/de3499eed549ea305c3f to your computer and use it in GitHub Desktop.
Save Manawyrm/de3499eed549ea305c3f to your computer and use it in GitHub Desktop.
Arduino ISR Clock
#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