// * Korekce času pomocí tlačítka na pinu 9. pullUp zapnut
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#define DS3231_I2C_ADDRESS 0x68
const int ledPin = 13; // číslo pinu LED
int buttonState = 0; // proměnná pro čtení stavu tlačítka
Adafruit_7segment matrix = Adafruit_7segment();
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val){
return ( (val/16*10) + (val%16) );
}
////////////////// korekce času /////////////////////////
void resetTime(){
pinMode(9,INPUT_PULLUP); // nastavení vstupu a pullup odporu
buttonState = digitalRead(9);
// zkontroluje, zda-li je tlačítko stlačené.
if (buttonState == LOW) {digitalWrite(ledPin, HIGH);}
else {digitalWrite(ledPin, LOW);}
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs3231(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
//korekce sekund zpět
if (buttonState == LOW && second<30) {setDS3231time(0, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);}
//korekce sekund vpřed
if (buttonState == LOW && second>30) {setDS3231time(59, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);}
}
void setup() {
Wire.begin();
matrix.begin(0x70);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(0,59,13,5,28,9,17);
pinMode(ledpin, OUTPUT);
}
void loop() {
displayTime(); // display the real-time clock data
displayTemp(); // display the temperature
// blink dot
for (uint16_t counter = 0; counter < 5; counter++) {
matrix.writeDigitRaw(2,0x4);
matrix.writeDisplay();
delay(500);
matrix.writeDigitRaw(2,0x8);
matrix.writeDisplay();
delay(500);
}
resetTime(); // reset sekund pokud je zmačknute tlačitko
displayTime(); // display the real-time clock data
displayDate(); // display the date
}
void displayTime(){
for (uint16_t counter = 0; counter < 10; counter++) {
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs3231(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
matrix.writeDigitNum(0, (hour / 10), false);
matrix.writeDigitNum(1, (hour % 10), false);
matrix.drawColon(true);
matrix.writeDigitNum(3, (minute / 10), false);
matrix.writeDigitNum(4,(minute % 10), false);
matrix.writeDisplay();
delay(500);
matrix.drawColon(false);
matrix.writeDisplay();
delay(500);
}
}
void displayDate(){
for (uint16_t counter = 0; counter < 10; counter++) {
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs3231(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
matrix.writeDigitNum(0, (dayOfMonth / 10), false);
matrix.writeDigitNum(1, (dayOfMonth % 10), false);
matrix.drawColon(false);
matrix.writeDigitNum(3, (month / 10), false);
matrix.writeDigitNum(4,(month % 10), false);
matrix.writeDisplay();
delay(500);
}
}
void displayTemp(){
byte temp = get3231Temp();
matrix.clear();
matrix.writeDigitRaw(4,57); // 57 = "C"
matrix.writeDigitRaw(3,99); // 57 = "C"
matrix.writeDigitNum(0, (temp/10), false); // position 0, value 1, show decimal)
matrix.writeDigitNum(1,( temp % 10), false); // position 1, value 9, show decimal)
matrix.writeDisplay();
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void getDateDs3231(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
float get3231Temp(){
byte tMSB, tLSB;
float temp3231;
Wire.beginTransmission(0x68);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(0x68, 2);
if(Wire.available()) {
tMSB = Wire.read(); //2's complement int portion
tLSB = Wire.read(); //fraction portion
temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8
}
else {
//oh noes, no data!
}
return temp3231;
}
Comments
Please log in or sign up to comment.