Benritrovati,
sto usando un codice per settare l'RTC DS3231 attraverso un progrmmino per windows. Il codice dello sketch e lo stesso programmino li ho prelevati da questo sito:
http://www.lucadentella.it/2013/11/27/rtcsetup/
Anche se nel sito si parla di un RTC DS1307, sistemando leggermente il codice, il tutto funziona bene:
#include <Wire.h>
#include "RTClib.h"
#define BUFFER_SIZE 20
#define VERSION "1.0"
// RTC object
RTC_DS3231 rtc;
// Buffer for incoming data
char serial_buffer[BUFFER_SIZE];
int buffer_position;
void setup() {
Serial.begin(57600);
Wire.begin();
rtc.begin();
buffer_position = 0;
}
void loop() {
// Wait for incoming data on serial port
if (Serial.available() > 0) {
// Read the incoming character
char incoming_char = Serial.read();
// End of line?
if (incoming_char == '\n') {
// Parse the command
// ##
if (serial_buffer[0] == '#' && serial_buffer[1] == '#')
Serial.println("!!");
// ?V
else if (serial_buffer[0] == '?' && serial_buffer[1] == 'V')
Serial.println(VERSION);
// ?T
else if (serial_buffer[0] == '?' && serial_buffer[1] == 'T') {
DateTime now = rtc.now();
char time_string[20];
sprintf(time_string, "%02d/%02d/%d %02d:%02d:%02d",
now.day(), now.month(), now.year(),
now.hour(), now.minute(), now.second());
Serial.println(time_string);
}
// !T
else if (serial_buffer[0] == '!' && serial_buffer[1] == 'T') {
String time_string = String(serial_buffer);
int day = time_string.substring(2, 4).toInt();
int month = time_string.substring(4, 6).toInt();
int year = time_string.substring(6, 10).toInt();
int hour = time_string.substring(10, 12).toInt();
int minute = time_string.substring(12, 14).toInt();
int second = time_string.substring(14, 16).toInt();
DateTime set_time = DateTime(year, month, day, hour, minute, second);
rtc.adjust(set_time);
Serial.println("OK");
}
// Reset the buffer
buffer_position = 0;
}
// Carriage return, do nothing
else if (incoming_char == '\r');
// Normal character
else {
// Buffer full, we need to reset it
if (buffer_position == BUFFER_SIZE - 1) buffer_position = 0;
// Store the character in the buffer and move the index
serial_buffer[buffer_position] = incoming_char;
buffer_position++;
}
}
}
Ora lo scopo delle mie prove è quello di fondere questo sketch in uno più ampio in modo tale da poter regolare più facilmente l'ora del RTC. Breve digressione: ho costruito una piccola centralina che ho montato sullo scooter che fornisce alcuni parametri vedi rpm, data e ora, voltmetro e temperatura.
Poichè è possibile che la batteria del rtc si scarichi o venga estratta per manutenzione, ecco che sarà necessario risettarel'RTC. Ho pensato appunto che questo metodo fosse più veloce.
Per cominciare, perchè se aggiungo nel codice di sopra le righe:
Serial.print(' ');
if (day < 10) Serial.print('0');
Serial.print(day, DEC);
Serial.print('/');
if (month < 10) Serial.print('0');
Serial.print(month, DEC);
Serial.print('/');
Serial.print(YY);
Serial.print(' ');
if (hour < 10) Serial.print('0');
Serial.print(hour, DEC);
Serial.print(':');
if (minute < 10) Serial.print('0');
Serial.print(minute, DEC);
Serial.println();
il programmino per windows non si sincronizza più con l'RTC?