I finally found this again.
Thank you, it was very helpful.
I am doing a RTC in Arduino Mega (Real Time Clock).
Using interrupts and other things.
(Lots of Serial.print statements for debug.)
Am still figuring what I can throw away in this, but here is code:
/* **************************************************************************/
/ B Conley, Circuitsville Engineering LLC 15 February 2010 */
/* **************************************************************************/
/ Real Time Clock application in Arduino Mega, using ATMega1280 /
/ Inputs: None (?) /
/ Outputs: Digital: D7 (PH4) Seconds blink LED /
/ D8 (PH5) Minutes blink LED /
/ D9 (PH6) Hours Blink LED /
/ Variables: Secs, Mins, Hrs /
/ Requirements: /
/ Set ORC1A register to 0xF424 to cause interrupt once per second /
/ Use Prescale of 256 (CS12:CS10 = 100, these TCCR1B LSBs) /
/ Set TCCR1B to bxxx0 1100 /
/ Set TCCR1A to bxxxx xx00 /
/ NOTES: /
/ Decimal 60 = b0011 1100 = 0x3C /
/ Decimal 59 = b0011 1011 = 0x3b /
/ Decimal 24 = b0001 1000 = 0x18 /
/ Decimal 23 = b0001 0111 = 0x17 /
/ ***************************************************************************/
#define D7 7 / Seconds, Green LED, PH4 /
#define D8 8 / Minutes, Yellow LED, PH5 /
#define D9 9 / Hours, Red LED, PH6 */
int Secs =0;
int Mins =0;
int Hrs =0;
int OutS =0;
int OutM =0;
int OutH =0;
byte IFMask = B00000010;
byte IFClrMask = B11111101;
byte result;
byte IFreset;
void setup()
{
Serial.begin(9600);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);
pinMode(D9,OUTPUT);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
digitalWrite(D9,HIGH);
delay(3000);
digitalWrite(D9,LOW);
delay(3000);
digitalWrite(D8,LOW);
delay(3000);
digitalWrite(D7,LOW);
Secs =0;
Mins =57;
Hrs =0;
TCCR1A = B01000000;
TCCR1B = B00001100;
TIMSK1 = B00000010; /* Enables the Timer1 Compare A interrupt /
OCR1AH = 0xF4;
OCR1AL = 0x24;
} / End Setup() /
/**************************************************************************/
/ timer1CompareFlag Function() An ISR (Interrupt Service Routine) /
/ Deals with Timer1A compare flag which should happen when one second is /
/ has passed /
/ Essential Function: /
/ Update seconds until seconds is >59 /
/ At Seconds >59 update Seconds, {reset to 00},(increment) Minutes /
/ Check if Minutes >59 if so update Minutes {reset to 00}, /
/ Increment hours /
/ Check Hours if >23 if so update Hours {reset to 00} /
/ Is actually an Interrupt Service Routine, so should be named & treated thus /
/**************************************************************************/
ISR (TIMER1_COMPA_vect){
noInterrupts();
//Serial.print("Beg ISR Seconds ");
//Serial.println(Secs,DEC);
Secs = Secs +1;
if (OutS == 0) / Begin Blink Green (Seconds) LED /
{
digitalWrite(D7,LOW);
OutS = 1;
}
else
{
digitalWrite(D7,HIGH);
OutS = 0;
} / End Blink Green LED /
if (Secs > 59)
{
//Serial.print("Inc Mins ISR Seconds ");
//Serial.println(Secs,DEC);
Secs = 0;
Mins += 1;
if (OutM == 0) / Begin Blink Yellow (minutes) LED /
{
digitalWrite(D8,HIGH);
OutM = 1;
}
else
{
digitalWrite(D8,LOW);
OutM = 0;
} / End Blink Yellow LED /
}
else
{
interrupts();
// Serial.print("Seconds ");
// Serial.println(Secs,DEC);
// Serial.print("Minutes ");
// Serial.println(Mins,DEC);
// Serial.print("Hours ");
// Serial.println(Hrs,DEC);
return;
} / End Seconds If/Else /
if(Mins > 59)
{
Mins = 0;
Hrs += 1;
if (OutH == 0) / Blink Hours (Red) LED /
{
digitalWrite(D9,HIGH);
OutH = 1;
}
else
{
digitalWrite(D9,LOW);
OutH = 0;
} / End Hours (Red) LED Blink /
} / End minutes If /
else
{
interrupts();
//Serial.print("End Ms ISR Seconds ");
//Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
return;
} / End Minutes If/Else, end Minutes Else /
if(Hrs > 23)
{
Hrs = 0;
} / End Hours If /
else
{
interrupts();
return;
} / End Hours Else, end Hours IF/ELSE /
//Serial.print("End Hrs ISR Seconds ");
//Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
interrupts();
return;
} / End function timer1CompareFlag() ISR /
void loop()
{
while(1){
interrupts();
Serial.print('B ', BYTE); // send a capital B
Serial.print(TIFR1,BIN);
Serial.print(' Secs ', BYTE); // send seconds
Serial.print(Secs, DEC);
Serial.print(' Mins ', BYTE); // send minutes
Serial.print(Mins, DEC);
Serial.print(' Hrs ', BYTE); // send hours
Serial.println(Hrs, DEC);
result = TIFR1&IFMask;
if (result > 0){
Serial.println('Ever Go This Way?', BYTE);
//timer1CompareFlag();
IFreset = TIFR1&IFClrMask; / This should clear the Timer 1 Flag (OCF1A) /
TIFR1 = IFreset;
}
establishContact();
}
} / End Loop() */
/* ***************************************************** /
/ Function establishContact() /
/ It sends out an ASCII character via USB to rcvr /
/ ***************************************************** */
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A', BYTE); // send a capital A
Serial.print(' ',BYTE);
Serial.print(Secs,DEC);
Serial.print(' ',BYTE);
Serial.print(Mins,DEC);
Serial.print(' ',BYTE);
Serial.println(Hrs,DEC);
} /* End while loop /
return;
} / End function establishContact() */