网站颜色:

PCF8583 I2C Real Time Clock RTCMEGA 2560 Arduino实时时钟


@

                    PCF8583 I2C Real Time Clock RTC Shield

                                For  R3 MEGA 1280 (咨询特价)

 

# PCF8583 based RTC with CR2032 battery backup
# Default PCF8583 RTC SCL connected to AN5, SDA connected to AN4
 
PCF8583 Demo

#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
 *
 *  Arduino analog input 5 - I2C SCL (PCF8583 pin 6)
 *  Arduino analog input 4 - I2C SDA (PCF8583 pin 5)
 *
 *  You can set the type by sending it YYMMddhhmmss;
 *  the semicolon on the end tells it you're done...
 *
 ******************************************************************************/

int correct_address = 0;
PCF8583 p (0xA0);
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));
       p.second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.

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


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

  delay(3000);
}

热门设计服务