Objectif:
Connaître l’heure et la date (lecture et réglage)
Ce dont vous avez besoin:
- RTC Module
- Arduino UNO
- 4 fils connecteurs
- Disponibles ici
Avec le RTC module, nous pouvons suivre l’évolution dans le temps d’un projet en cours. En effet, le RTC module nous indique l’heure et la date .Il contient en outre une batterie CR1225 lui permettant de fonctionner de façon autonome.
Son circuit étant constitué du circuit intégré DS1307, le système d’oscillation à quartz est celui utilisé. Ainsi une meilleure précision de l’heure est obtenue.
Facile à utiliser; il dispose de cinq pins dont seulement les quatre seront utilisés après les avoir soudés pour bien les fixer sur une breadboard.
Ces pins sont :
-SCL : Serial Clock
-SDA :Serial Data
-SQW
-GND
-5V
SCL et SDA seront respectivement connectés aux entrées analogiques A5 et A4 pour l’Arduino UNO. Le système fonctionne sous 5V .Donc seul le SQW n’est pas relié.
Utilisant des registres, on procèdera alors pour le programme sous arduino à une conversion de la base binaire à la base décimale. En effet cela est dû au DS1307 qui code toutes les données en BCD (code binaire décimal).
Comme toute montre on peut régler l’heure et la date.
Dans l’exemple 1, nous lisons à travers le Serial monitor l’heure et la date.
Exemple 1:
//library “Wire” using
#include <Wire.h>
#define DS1307_ADDRESS 0x68
////////////////////////////////////////////////////
////////////////////SETUP//////////////////////////
//////////////////////////////////////////////////
void setup(){
//Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(9600);
}
//print date and time every second by calling “printDate()”function
////////////////////////////////////////////////////////////////////
///////////////////LOOP////////////////////////////////////////
//////////////////////////////////////////////////////////////
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
//Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
//define printDate() function
void printDate(){
//Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday – Saturday
switch(weekDay){
case 0:Serial.print("Sunday");break;
case 1:Serial.print("Monday");break;
case 2:Serial.print("Tuesday");break;
case 3:Serial.print("Wednesday");break;
case 4:Serial.print("Thursday");break;
case 5:Serial.print("Friday");break;
case 6:Serial.print("Saturday");break;
}
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(" ");
if(monthDay<10)
Serial.print("0");
Serial.print(monthDay,DEC);
Serial.print("/");
if(month<10)
Serial.print("0");
Serial.print(month,DEC);
Serial.print("/");
Serial.print(year);
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.print(":");
if(second<10)
Serial.print("0");
Serial.println(second,DEC);
}
//library “Wire” using
#include <Wire.h>
#define DS1307_ADDRESS 0x68
////////////////////////////////////////////////////
////////////////////SETUP//////////////////////////
//////////////////////////////////////////////////
void setup(){
//Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(9600);
}
//print date and time every second by calling “printDate()”function
////////////////////////////////////////////////////////////////////
///////////////////LOOP////////////////////////////////////////
//////////////////////////////////////////////////////////////
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
//Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
//define printDate() function
void printDate(){
//Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday – Saturday
switch(weekDay){
case 0:Serial.print("Sunday");break;
case 1:Serial.print("Monday");break;
case 2:Serial.print("Tuesday");break;
case 3:Serial.print("Wednesday");break;
case 4:Serial.print("Thursday");break;
case 5:Serial.print("Friday");break;
case 6:Serial.print("Saturday");break;
}
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(" ");
if(monthDay<10)
Serial.print("0");
Serial.print(monthDay,DEC);
Serial.print("/");
if(month<10)
Serial.print("0");
Serial.print(month,DEC);
Serial.print("/");
Serial.print(year);
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.print(":");
if(second<10)
Serial.print("0");
Serial.println(second,DEC);
}
Dans l’exemple 2 ,nous réglons l’heure et la date. Notons également qu’avec ce programme,on initialise l’heure et la date à chaque fois qu’on ouvre le Serial monitor.
Exemple 2:
// library “Wire” using
#include <Wire.h>
#define DS1307_ADDRESS 0x68
///////////////////////////////////////////////////////
//////////////SETUP/////////////////////////////////
/////////////////////////////////////////////////////
void setup(){
//Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}
// print date and time every second by calling “printDate()”function
/////////////////////////////////////////////////////////
////////////////LOOP/////////////////////////////////
////////////////////////////////////////////////////
void loop(){
printDate();
delay(1000);
}
//define "setDateTime()"function
void setDateTime(){
byte zero = 0x00;
byte second = 07; //0-59
byte minute = 13; //0-59
byte hour = 9; //0-23
byte weekDay = 3; //0-6
byte monthDay = 20; //1-31
byte month = 03; //1-12
byte year = 11; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
//Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
//Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
//define "printDate()"function
void printDate(){
byte zero = 0x00;
//Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
switch(weekDay){
case 0:Serial.print("Sunday");break;
case 1:Serial.print("Monday");break;
case 2:Serial.print("Tuesaday");break;
case 3:Serial.print("Wednesday");break;
case 4:Serial.print("Thursday");break;
case 5:Serial.print("Friday");break;
case 6:Serial.print("Saturday");break;
}
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(" ");
if(monthDay<10)
Serial.print("0");
Serial.print(monthDay,DEC);
Serial.print("/");
if(month<10)
Serial.print("0");
Serial.print(month,DEC);
Serial.print("/");
Serial.print(year);
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.print(":");
if(second<10)
Serial.print("0");
Serial.println(second,DEC);
}
Si vous avez des questions n’hésitez pas à les posez en commentaires.
// library “Wire” using
#include <Wire.h>
#define DS1307_ADDRESS 0x68
///////////////////////////////////////////////////////
//////////////SETUP/////////////////////////////////
/////////////////////////////////////////////////////
void setup(){
//Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}
// print date and time every second by calling “printDate()”function
/////////////////////////////////////////////////////////
////////////////LOOP/////////////////////////////////
////////////////////////////////////////////////////
void loop(){
printDate();
delay(1000);
}
//define "setDateTime()"function
void setDateTime(){
byte zero = 0x00;
byte second = 07; //0-59
byte minute = 13; //0-59
byte hour = 9; //0-23
byte weekDay = 3; //0-6
byte monthDay = 20; //1-31
byte month = 03; //1-12
byte year = 11; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
//Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
//Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
//define "printDate()"function
void printDate(){
byte zero = 0x00;
//Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
switch(weekDay){
case 0:Serial.print("Sunday");break;
case 1:Serial.print("Monday");break;
case 2:Serial.print("Tuesaday");break;
case 3:Serial.print("Wednesday");break;
case 4:Serial.print("Thursday");break;
case 5:Serial.print("Friday");break;
case 6:Serial.print("Saturday");break;
}
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(" ");
if(monthDay<10)
Serial.print("0");
Serial.print(monthDay,DEC);
Serial.print("/");
if(month<10)
Serial.print("0");
Serial.print(month,DEC);
Serial.print("/");
Serial.print(year);
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.print(":");
if(second<10)
Serial.print("0");
Serial.println(second,DEC);
}
Aucun commentaire:
Enregistrer un commentaire