/****************************************************************************** Example program I2C-RTC interface with Arduino. SETUP: I2C-RTC => Arduino PIN1 => A5, PIN2 => A4, PIN3 => ground, PIN6 => +5V Note: The program is written for address 0xD0 (Arduino address 0x68). This program was tested using Arduino Nano Document: DS1340 datasheet Updated: September 4, 2008 E-mail: support@gravitech.us Gravitech (C) Copyright 2008 All Rights Reserved *******************************************************************************/ #include #define Binary 0 #define Hex 1 /******************************************************************************* Function Prototype *******************************************************************************/ unsigned int SerialNumRead (byte); void SetTime(); void DisplayTime(); /******************************************************************************* Global variables *******************************************************************************/ const int I2C_address = 0x68; // I2C write address byte Second; // Store second value byte Minute; // Store minute value byte Hour; // Store hour value byte Day; // Store day value byte Date; // Store date value byte Month; // Store month value byte Year; // Store year value /******************************************************************************* Setup *******************************************************************************/ void setup() { Serial.begin(9600); Wire.begin(); // join i2c bus (address optional for master) delay(1000); } /******************************************************************************* Main Loop *******************************************************************************/ void loop() { boolean Readtime; // Set/Read time flag unsigned int Incoming; // Incoming serial data // Display prompt Serial.println("What would you like to do?"); Serial.println("(0) To set the current time."); Serial.println("(1) To display the current time."); Serial.print("Enter 0 or 1: "); Incoming = SerialNumRead (Binary); // Get input command Serial.println(Incoming, DEC); // Echo the value Serial.println(); if (Incoming == 0) // Process input command SetTime(); else if (Incoming == 1) DisplayTime(); delay (1000); } /******************************************************************************* Read a input number from the Serial Monitor ASCII string Return: A binary number or hex number *******************************************************************************/ unsigned int SerialNumRead (byte Type) { unsigned int Number = 0; // Serial receive number unsigned int digit = 1; // Digit byte i = 0, j, k=0, n; // Counter byte ReceiveBuf [5]; // for incoming serial data while (Serial.available() <= 0); while (Serial.available() > 0) // Get serial input { // read the incoming byte: ReceiveBuf[i] = Serial.read(); i++; delay(10); } for (j=i; j>0; j--) { digit = 1; for (n=0; n