NB1A Real-Time Clock
This program initializes the DS1337 real-time clock (RTC) to output a
time and date string every second to the serial port. Until the DS1337
is set for the first time (or reset after a battery change) the time
registers will be set at zero. The code in the following example will
initialize the date to August 25th, 2009 and the time to 3:00PM.
// initialize the date to August 25th, 2009
rtc.set_date(2009, 8, 25);
// intialize the clock to 3PM
rtc.set_time(15, 0, 0);
NB1A RTC Example
#include <avr/interrupt.h>
#include <avr/io.h>
#include <LED_debug.h>
#include <TWI.h>
#include <RTC.h>
#define int_enable PCICR |= (1 << PCIE1)
#define int_disable PCICR &= ~(1 << PCIE1)
LED_debug led;
RTC rtc;
void setup() {
// Compensating for the 12MHz XTAL
// 12800 = (16/12) * 9600
// 25600 = (16/12) * 19200
Serial.begin(12800);
// setup the TWI
TWBR = TWI_TWBR; // Set bit rate register (Baudrate). Defined in TWI.h
TWDR = 0xFF; // Default content = SDA released.
TWCR = (1<<TWEN)| // Enable TWI-interface and release TWI pins.
(0<<TWIE)|(0<<TWINT)| // Disable Interupt.
(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)| // No Signal requests.
(0<<TWWC);
int_enable;
// initialize the date
rtc.set_date(2009, 10, 14);
// intialize the clock
rtc.set_time(9, 4, 15);
}
void loop() {
char timestr[22];
while(1) {
rtc.read_regs();
rtc.localtime(timestr);
Serial.print(timestr);
Serial.print("\n");
led.blink(2);
delay(1000);
}
}