003 Read, set PCF8583 RTC with Arduino language

1. Forewords

This example use the console to display and set the time. For setting the time, you must send from the console a string of numbers in this format:

  YYMMDDhhmmss;

So, it will look like this: 100925144300; - it is ending with ";" .and means

 - 10 is 2010 year,
 - 09 is September month,
 - 25 is the day,
 - 14 is the hour,
 - 43 are minutes,
 - 00 are seconds,
 - and ";" character means you finished setting the clock.

On the console date an time are displayed:

2010/09/25 14:43:00

2. Set the board

Just remove the LCD display if is connected. The clock chip is already on board and  I2C bus is physically connected to the ATmega microcontroller on our board.

3. The sketch

#include <Wire.h> // necessary, or the application won't build properly
#include <stdio.h>
#include <PCF8583.h> 
/*****************************************************************************  *  read/write serial interface to PCF8583 RTC via I2C interface  *  *  Sanguino digital pin 16 - I2C SCL (PCF8583 pin 6)  *  Sanguino digital pin 17 - I2C SDA (PCF8583 pin 5)  *  *  You can set the clock by sending the type in this format YYMMDDhhmmss;  *  the semicolon on the end tells it you're done...  *  ******************************************************************************/ int correct_address = 0;
PCF8583 p (0xA2);
void setup(void){
  Serial.begin(9600);
  Serial.print("booting...");
  Serial.println(" done");
}

void loop(void){
  if(Serial.available() > 0){
       p.year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)) + 2000;
       p.month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
       p.day = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
       p.hour  = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
       p.minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
       // Use of (byte) type casting and ascii math to achieve result.        p.second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));

       if(Serial.read() == ';'){
         Serial.println("setting date");
 p.set_time();
       }
  }

  p.get_time();
  char time[20];
  sprintf(time, "%02d/%02d/%02d %02d:%02d:%02d",
  p.year, p.month, p.day, p.hour, p.minute, p.second);
  Serial.println(time);
  delay(990);
}

4. Results

Application start displaying time. Click to zoom

Ready to set the timer. Click to zoom

Timer was set. Click to zoom
Comments