Hello all, first off you must know that I am a complete noob to this stuff. I have been trying to learn a little bit at a time. I am trying to make a single digit seven segment clock and I have the prototype almost working. Today I decided to see if I could switch from USB power( I am using the Arduino Uno as power) to battery power to run my Attiny. When I switched the time reset to 12:00. So I hooked up my Uno to the RTC and set the correct time again. The time was correct again. I proceeded to switch from USB power to battery power. Again it resets to 12:00.
I have done this about 10 times now with the same results. I am using a supercap for RTC backup power and it is charged when power is off. I am pretty sure all my wiring is correct. In fact it used to work, I could unplug the UNO over a weekend and come back and plug the Uno back in and the time would show up correct. I am at a loss, I don't know if I did something to the code or if it is the way I am setting the time. Any help would be greatly appreciated.
Here is my sketch
#include <Wire.h>
// I2C address
#define DS1307 B1101000
// pinout for the 7 segment display
// Starts top center, clock-wise, then
// center line, then dot
byte segment[] = {10, 9, 8, 7, 3, 2, 1};
byte second=0, minute=0, hour=0;
const int buttonPin = 0; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
bool showTime = false;
void updatetime();
void printtime();
void print7digit(byte number);
byte DECTOBCD(byte val);
byte BCDTODEC(byte val);
void clearDisplay();
void checkButton();
void setup() {
byte i;
Wire.begin();
for (i=0; i<8; i++) {
pinMode(segment[i], OUTPUT);
}
clearDisplay();
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void checkButton() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
showTime = true;
}
}
void loop() {
checkButton();
if(showTime) {
updatetime();
printtime();
showTime = false;
}
}
// Pull current time off the DS1307
void updatetime() {
Wire.beginTransmission(DS1307);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, 3);
// Don't actually use seconds value. Feel free to.
second = BCDTODEC(Wire.read() & 0x7f);
minute = BCDTODEC(Wire.read());
hour = BCDTODEC(Wire.read() & 0x3f);
}
// Flash each digit of the time in turn on the 7 segment
void printtime() {
byte j;
// Convert from 24 hour to 12 hour mode
// Should be done on the DS1307. I'm sorry.
byte temphour = hour % 12;
if (temphour == 0) {
// Not zero based, not one based, but 12 based.
temphour = 12;
}
int betweenSegmentDelay = 500;
int clearDelay = 200;
// Hour
// Only display highest digit if it's nonzero
if (temphour > 9) {
print7digit(temphour/10);
delay(betweenSegmentDelay); // Repeated magic values in my code? Never!
clearDisplay();
delay(clearDelay);
} else {
print7digit(0);
delay(betweenSegmentDelay); // Repeated magic values in my code? Never!
clearDisplay();
delay(clearDelay);
}
print7digit(temphour % 10);
delay(betweenSegmentDelay);
clearDisplay();
delay(clearDelay);
// Minute
print7digit(minute/10);
delay(betweenSegmentDelay);
clearDisplay();
delay(clearDelay);
print7digit(minute%10);
delay(betweenSegmentDelay);
clearDisplay();
delay(clearDelay);
/*
// Seconds
print7digit(second/10);
delay(betweenSegmentDelay);
clearDisplay();
delay(clearDelay);
print7digit(second%10);
delay(betweenSegmentDelay);
clearDisplay();
delay(clearDelay);
*/
delay(2000);
}
// Display a single digit on the 7 segment
void print7digit(byte number) {
// Bitfield for digits 0-9
const byte numtable[] = {B01000000, // Numeral 0.
B01111001, // Numeral 1.
B00100100, // Numeral 2.
B00110000, // Numeral 3.
B00011001, // Numeral 4.
B00010010, // Numeral 5.
B00000010, // Numeral 6.
B01111000, // Numeral 7.
B00000000, // Numeral 8.
B00011000 // Numeral 9.
};
byte litpins = numtable[number];
byte i;
for (i=0; i<7; i++) {
digitalWrite(segment[i], (litpins>>i) & 1);
}
}
void clearDisplay() {
int j;
for (j=0; j<8; j++) {
digitalWrite(segment[j], HIGH);
}
}
byte DECTOBCD(byte val) {
return ((val/10)<<4) + (val%10);
}
byte BCDTODEC(byte val) {
return (val>>4) * 10 + (val & 0xf);
}
Here is the sketch on how I am setting the time, it uses the Computer time and sets it automatically. there are three files in the sketch in the attachments.
Again I am a NOOB, most of this was pieced together with help from some of my friends and people on the forums. Thanks in advance
RTCtimeSet.ino (9.05 KB)
RTCtimeUtils.cpp (1.99 KB)
RTCtimeUtils.h (188 Bytes)





