Created
September 18, 2015 03:46
-
-
Save PeterMitrano/7d22e6c5b3bb9446ad90 to your computer and use it in GitHub Desktop.
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 <LiquidCrystal.h> | |
const int buttonPin = 2; | |
/* volatile means tells compiler not to optimize these | |
* since they might be updated from an ISR | |
*/ | |
volatile int hours, minutes, seconds; | |
LiquidCrystal lcd(12, 11, 6, 5, 4, 3); | |
void setup() { | |
//TIMER | |
//why use a library when you could write directly to registers?! | |
TCCR1A = 0;// disable PWM and diconnect digital pin 11 from timer1 | |
TCCR1B = 0;// use falling edge | |
TCNT1 = 0;//initialize counter value to 0 | |
// set compare match register for 1hz increments | |
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536) | |
TCCR1B |= (1 << WGM12); //actually clear overflow register on match | |
TCCR1B |= (1 << CS12) | (1 << CS10); // Set CS10 and CS12 bits for 1024 prescaler | |
TIMSK1 |= (1 << OCIE1A); // jump to the input capture interrupt vector on match | |
//RESET BUTTON | |
EICRA |= 0x3; // rising edge of INT0 | |
EIMSK |= 0x1; // enable INT0 | |
// set up the LCD's number of columns and rows | |
lcd.begin(16, 2); | |
} | |
void loop() { | |
lcd.setCursor(0, 1); | |
char t[9]; | |
sprintf(t, "%2.2i:%2.2i:%2.2i", hours, minutes, seconds); | |
lcd.print(t); | |
} | |
ISR(INT0_vect){ | |
hours = 0; | |
seconds = 0; | |
minutes = 0; | |
} | |
// Interrupt is called once a millisecond, | |
SIGNAL(TIMER1_COMPA_vect) | |
{ | |
seconds++; | |
if (seconds % 60 == 0 && seconds > 0) { | |
seconds = 0; | |
minutes++; | |
} | |
if (minutes % 60 == 0 && minutes > 0) { | |
minutes = 0; | |
hours++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment