RTC avec Arduino

Principe

L’application affiche l’heure et la date toutes les secondes

Materiel

Cablage

Code source

#include <Wire.h>
#include <LCDI2C.h>

//I2C display adress (found by I2C scanner
#define LCDI2C_ADDRESS 0x3B
//I2C RTC adress (found by I2C scanner - doc)
#define DS1307_I2C_ADDRESS 0x68

#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  #define I2C_WRITE Wire.write 
  #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0 
  #define I2C_WRITE Wire.send 
  #define I2C_READ Wire.receive
#endif

//int currentValue = 0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte zero;
char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char  *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//I2C display setup : Number of lines, char and i2c address of the display
LCDI2C lcd = LCDI2C(2,16,LCDI2C_ADDRESS);  

void setup() 
{
  //Start I2C bus
  Wire.begin();
  Serial.begin(57600); 
  setDateDs1307();

  //Init the display, clears the display
  lcd.init();                          
  lcd.printstr("Starting...");

  //Init RTC
  zero=0x00;
}

void updateDisplayedValue() 
{
  lcd.clear();

  //Prepare Date string
  String aDate("Date: ");
  String aDoW(Day[dayOfWeek]);
  aDate = aDate.concat(aDoW);
  String aDoM(dayOfMonth, DEC);
  aDate = aDate.concat(aDoM);
  String aMon(Mon[month]);
  aDate = aDate.concat(aMon);

  //Display it in the first line
  char charBuf[50];
  aDate.toCharArray(charBuf, 50);
  lcd.printstr(charBuf);

  //Prepare Time string
  lcd.setCursor(1,0);
  String aTime("Time: ");
  String aHour(hour, DEC);
  aTime = aTime.concat(aHour);
  String aSpace(".");
  aTime = aTime.concat(aSpace);
  String aMin(minute, DEC);
  aTime = aTime.concat(aMin);
  aTime = aTime.concat(aSpace);
  String aSec(second, DEC);
  aTime = aTime.concat(aSec);

  //Display it in the second line
  aTime.toCharArray(charBuf, 50);
  lcd.printstr(charBuf);
}

void setDateDs1307()                
{
  //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip. 
   second = (byte) (0); // Use of (byte) type casting and ascii math to achieve result.  
   minute = (byte) (54);
   hour  = (byte) (12);
   dayOfWeek = (byte) (7);
   dayOfMonth = (byte) (18);
   month = (byte) (02);
   year= (byte) (12);
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   I2C_WRITE(zero);
   I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
   I2C_WRITE(decToBcd(minute));
   I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   I2C_WRITE(decToBcd(dayOfWeek));
   I2C_WRITE(decToBcd(dayOfMonth));
   I2C_WRITE(decToBcd(month));
   I2C_WRITE(decToBcd(year));
   Wire.endTransmission();
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second = bcdToDec(I2C_READ() & 0x7f);
  minute = bcdToDec(I2C_READ());
  hour = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month = bcdToDec(I2C_READ());
  year = bcdToDec(I2C_READ());
}

void loop()
{
  //retrieve RTC info
  getDateDs1307();
  //display time
  updateDisplayedValue();
  //wait a little                       
  delay(1000);                                            
}

librairies

  • LCDI2C : http://www.arduino.cc/playground/Code/LCDi2c
  • Wire : http://www.arduino.cc/en/Reference/Wire
  • Arduino beta 23