Salve a tutti, sono nuovo sull'utilizzo di Arduino ma ho già fatto tante prove di quelle presenti su "Learning" adesso mi sto cimentando con un RTC è precisamente il DS1307.
Premetto che tutto sembra funzionare ol sketch caricato è il seguente:
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val){
return ( (val/16*10) + (val%16) );
}
void setDateDs1307(){
second = 00;
minute = 23;
hour = 17;
dayOfWeek = 5;
dayOfMonth = 31;
month = 3;
year = 11;
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.send(decToBcd(second));
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
void getDateDs1307(){
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
}
void setup () {
Serial.begin(9600);
Wire.begin();
pinMode(13, OUTPUT);
setDateDs1307();
}
void loop () {
getDateDs1307();
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.println(" ");
delay(2000);
}
Detto ciò non capisco perché ogni volta che apro il serial monitor l'orario che leggo dall'RTC viene nuovamente riportato a quello che inizializzo nel setup.
Grazie
Saluti
Nicola