Referência:
Exemplos da biblioteca DS1307.h
Lista de Materiais:
IDE Arduino 1.0.6
1x Board Arduino UNO
1x Protoboard 830 furos
1x Cabo USB-A male to USB-B male
1x Conj. de fios jumper p/ protoboard
1x Módulo RTC DS1307
1x Bateria Maxell CR2032 3V
Montagem:
Firmware:
#include <Wire.h>
#include <DS1307.h>
char dateTime[22];
int RTCValues[7], i = 0, year, month, dayOfMonth, dayOfWeek, hour, minute,
second;
//*******************************************************************************
void setup() {
Serial.begin(9600);
Serial.println("Favor entrar com data e hora no formato \"DD-MM-YYYY HH:MM:SS D\",");
Serial.println("Onde D e o numero do dia da semana (0 = Domingo, 6 = Sabado).");
Serial.println("Examplo: 11-04-2003 02:25:27 6");
DS1307.begin();
while (i < 21) {
if (Serial.available()) {
char c = Serial.read();
dateTime[i] = c;
i++;
}
}
dateTime[i] = '\0';
dayOfMonth = 10 * (dateTime[0] - 48) + (dateTime[1] - 48);
month = 10 * (dateTime[3] - 48) + (dateTime[4] - 48);
year = 10 * (dateTime[8] - 48) + (dateTime[9] - 48);
dayOfWeek = (dateTime[20] - 48);
hour = 10 * (dateTime[11] - 48) + (dateTime[12] - 48);
minute = 10 * (dateTime[14] - 48) + (dateTime[15] - 48);
second = 10 * (dateTime[17] - 48) + (dateTime[18] - 48);
DS1307.setDate(year, month, dayOfMonth, dayOfWeek, hour, minute, second);
Serial.println("Data e hora configurados!");
Serial.println("Lendo dados do RTC...");
}
//*******************************************************************************
void loop() {
DS1307.getDate(RTCValues);
sprintf(dateTime, "%02d-%02d-20%02d %02d:%02d:%02d", RTCValues[2],
RTCValues[1], RTCValues[0], RTCValues[4], RTCValues[5],
RTCValues[6]);
Serial.print(dateTime);
Serial.print(" - Dia da semana: ");
Serial.println(fromNumberToWeekDay(RTCValues[3]));
delay(1000);
}
//*******************************************************************************
char fromNumberToWeekDay(int week){
switch (week){
case 0:
Serial.print("Dom.");
break;
case 1:
Serial.print("Seg.");
break;
case 2:
Serial.print("Ter.");
break;
case 3:
Serial.print("Qua.");
break;
case 4:
Serial.print("Qui.");
break;
case 5:
Serial.print("Sex.");
break;
case 6:
Serial.print("Sab.");
break;
}
}
Download: DS1307.h
Nenhum comentário:
Postar um comentário