// DS1307 variables byte second = 0, minute = 0, hour = 0, dayofweek = 0, dayofmonth = 0, month = 0, year = 0;

Download (0)

Full text

(1)

// (c) 2013 M. Ray. Burnette, Atlanta, GA // Arduino UNO Compiler version 1.0.1+

// Binary sketch size: 13,664 bytes (of a 32,256 byte maximum) // #include <SoftwareSerial.h> // default lib searched in 1.0+ #include <AXE133Y.h> // Library

#include "Wire.h" // System library for I2C #define DS1307_I2C_ADDRESS 0x68 // Realtime Clock module // Type Variable Declarations

int incomingByte = 0; // for incoming serial data int OLED_Tx = 3; // Serial out for OLED // DS1307 variables

byte second = 0, minute = 0, hour = 0, dayOfWeek = 0, dayOfMonth = 0, month = 0, year = 0;

AXE133Y OLED = AXE133Y(OLED_Tx);

// Private function: from http://arduino.cc/playground/Code/AvailableMemory int freeRam () {

extern int __heap_start, *__brkval; int v;

return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); }

// 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 void getDataDs1307(byte *second,

byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) {

// Reset the register pointer

Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write((byte)0);

Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits *second = bcdToDec(Wire.read() & 0x7f);

*minute = bcdToDec(Wire.read());

*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); }

(2)

void setDataDs1307(byte second, // 0-59 byte minute, // 0-59 byte hour, // 1-23 byte dayOfWeek, // 1-7 byte dayOfMonth, // 1-28/29/30/31 byte month, // 1-12 byte year) // 0-99 { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0);

Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock Wire.write(decToBcd(minute));

Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); }

// Arduino-specific configuration code initialization section void setup() {

Wire.begin();

Serial.begin(9600); // Hardware serial port to PC, port for Rx from IR controller ATtiny85

Serial.println("Coming online...");

while (Serial.available() > 0) Serial.read(); // Pseudo .flush() to clear input buffer R1x garbage for IR

OLED.clearScreen();

OLED.splash("Coming online..."); delay(500);

OLED.clearScreen(); }

// Main Program Loop void loop() {

//

____________________________________________________________________________________ _________________

getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); Serial.print(hour, DEC); Serial.print(":"); Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); Serial.print(" "); Serial.print(month, DEC); Serial.print("/"); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.println(year, DEC); // ____________________________________________________________________________________ _________________

if (Serial.available() > 0) // Something from IR is in the serial buffer {

while(true) // Big loop ensures similar behavior for Time and Date setup

{

// read the incoming byte: incomingByte = Serial.read();

(3)

// log what was extracted to system log: Serial.print("IR keycode received: "); Serial.println(incomingByte, DEC);

if (incomingByte != 80) break; // Only interested in Menu request (ON/off)

{

Serial.println("Menu request received on IR link");

while (Serial.available() > 0) Serial.read(); // Pseudo 1.flush() Serial.println("Serial receive buffer 1 cleared");

Serial.println("Entering DS1307 set menu"); restartMenu:

OLED.clearScreen();

OLED.cursorPosition(1,0);

OLED.print("Press 1 for TIME"); OLED.cursorPosition(2,0);

OLED.print("Press 2 for CAL");

while (Serial.available() > 0) Serial.read(); // Pseudo 1.flush() while (Serial.available() == 0) ; // wait for digit on IR

incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } // on/OFF if (incomingByte == 50) { goto calendarSet; } // #2 if (incomingByte == 49) { goto timeSet; } // #1 goto restartMenu;

//byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; } timeSet: { OLED.clearScreen(); OLED.cursorPosition(1,0); OLED.print("Enter 2 Digits:"); seconds: OLED.cursorPosition(2,0); OLED.print("Seconds hh-mm-SS"); // SS position 14, 15 while (Serial.available() == 0) ; // wait for digit on IR incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; }

int temp = (incomingByte - 48) * 10; // "tens" digit if (temp > 50) { goto seconds; } // bad input OLED.cursorPosition(2, 14);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order seconds digit

while (Serial.available() == 0) ; // wait for digit on IR channel

incomingByte = Serial.read();

temp += (incomingByte - 48); // add tens digit to unit digit

if(temp >59) { goto seconds; } // error check // read the RTC, modify seconds, write it quickly back to RTC getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

second = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 14);

OLED.printFloat(temp, 2, 0); // display seconds // write to system log stream

Serial.print("Seconds have been set to: "); Serial.println(temp, DEC);

minutes:

OLED.cursorPosition(2,0);

OLED.print("Minutes hh-MM"); // MM position 11, 12 while (Serial.available() == 0); // wait for digit on IR

(4)

incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } // on/OFF? temp = (incomingByte - 48) * 10; // "tens" digit if (temp > 50) { goto minutes; } // error check OLED.cursorPosition(2, 11);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order minutes digit

while (Serial.available() == 0) ; // wait for digit on IR serial channel

incomingByte = Serial.read();

temp += (incomingByte - 48); // unit digit added to tens digit

if(temp >59) { goto minutes; } // error check // read the RTC, modify seconds, write it quickly back to RTC getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

minute = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 11);

OLED.printFloat(temp, 2, 0); // display minutes // Write to system log stream

Serial.print("Minutes have been set to: "); Serial.println(temp, DEC);

hours:

OLED.cursorPosition(2, 0);

OLED.print("Hours HH"); // HH position 8, 9 while (Serial.available() == 0) ; // wait for digit on IR incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } // on/OFF ? temp = (incomingByte - 48) * 10; // 'tens' digit if (temp > 20) { goto hours; } // error check OLED.cursorPosition(2, 8);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order minutes digit

while (Serial.available() == 0) ; // wait for digit on IR incomingByte = Serial.read();

temp += (incomingByte - 48);

if(temp >23) { goto hours; } // error check

// read the RTC, modify seconds, write it quickly back to RTC getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

hour = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 0);

OLED.print("Time: "); // HH position 8, 9 OLED.cursorPosition(2, 8);

OLED.printFloat(temp, 2, 0);

// send status information over system log link Serial.print("Hours have been set to: ");

Serial.println(temp, DEC);

while (Serial.available() > 0) Serial.read(); // Pseudo 1.flush() OLED.cursorPosition(1,0);

OLED.print("Press Remote OFF"); incomingByte = Serial.read(); }

// Display the real-time second-by-second while remote power ON/off is ON

do {

getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

(5)

OLED.print("Time: "); // HH position 8, 9 MM position 11, 12 SS position 14, 15 OLED.cursorPosition(2, 8); OLED.printFloat(hour, 2, 0); OLED.cursorPosition(2, 11); OLED.printFloat(minute, 2, 0); OLED.cursorPosition(2, 14); OLED.printFloat(second, 2, 0); delay(1000); incomingByte = Serial.read(); } while( incomingByte != 80); goto menuexit;

// User pressed "2" in response to 1/2 menu choice calendarSet:

{

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; int temp; OLED.clearScreen(); OLED.cursorPosition(1,0); OLED.print("Enter Day Of Mn"); // dayOfMonth dayMonth: OLED.cursorPosition(2,0);

OLED.print("Date: yy-mm-DD"); // DD position 14, 15 while (Serial.available() == 0) ; // wait for digit on IR

incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } // on/OFF pressed? temp = (incomingByte - 48) * 10;

if (temp > 30) { goto dayMonth; } OLED.cursorPosition(2, 14);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order digit

while (Serial.available() == 0) ; // wait for digit on IR

incomingByte = Serial.read(); temp += (incomingByte - 48); if(temp > 31) { goto dayMonth; }

// read the RTC, modify dayOfMonth, write it quickly back to RTC getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

dayOfMonth = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 14);

OLED.printFloat(temp, 2, 0); // display day-of-month

Serial.print("Day Of Month has been set to: "); Serial.println(temp, DEC);

// Month Month:

OLED.cursorPosition(2, 0);

OLED.print("Date: yy-MM-"); // MM position 11, 12 while (Serial.available() == 0) ; // wait for digit on IR

incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } // on/OFF pressed? temp = (incomingByte - 48) * 10;

if (temp > 10) { goto Month; } OLED.cursorPosition(2, 11);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order digit

(6)

while (Serial.available() == 0); // wait for digit on IR

incomingByte = Serial.read(); temp += (incomingByte - 48); if(temp > 31) { goto Month; }

// read the RTC, modify month, write it quickly back to RTC getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

month = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 11);

OLED.printFloat(temp, 2, 0); // display month Serial.print("Month has been set to: ");

Serial.println(temp, DEC);

// Year Year:

OLED.cursorPosition(2,0);

OLED.print("Date: YY-"); // YY position 8, 9 while (Serial.available() == 0) ; // wait for digit on IR incomingByte = Serial.read() ;

if (incomingByte == 80) { goto menuexit; } temp = (incomingByte - 48) * 10;

if (temp > 10) { goto Year; } OLED.cursorPosition(2, 8);

OLED.printFloat(incomingByte - 48, 1, 0); // show the high-order digit

while (Serial.available() == 0) ; // wait for digit on IR incomingByte = Serial.read();

temp += (incomingByte - 48); // any year from 00 to 99 OK

// read the RTC, modify year, write it quickly back to RTC

getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

year = temp;

setDataDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

OLED.cursorPosition(2, 8);

OLED.printFloat(temp, 2, 0); // display year Serial.print("Year has been set to: ");

Serial.println(temp, DEC);

while (Serial.available() > 0) Serial.read(); // Pseudo 1.flush() OLED.cursorPosition(1,0);

OLED.print("Press Remote OFF"); incomingByte = Serial.read();

// Display the date while remote power ON/off is ON

getDataDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

do // loop date until on/OFF pressed

{

OLED.cursorPosition(2, 0);

OLED.print("Date: 20 - - "); // 20YY position 6, 7, 8, 9 MN position 11, 12 DOM position 14, 15

OLED.cursorPosition(2, 8); OLED.printFloat(year, 2, 0); OLED.cursorPosition(2, 11); OLED.printFloat(month, 2, 0); OLED.cursorPosition(2, 14); OLED.printFloat(dayOfMonth, 2, 0); incomingByte = Serial.read(); } while( incomingByte != 80); }

(7)

menuexit:

while (Serial.available() > 0) Serial.read(); // Pseudo 1.flush() OLED.clearScreen();

} }

delay(1000); // Wait 1 second, then update the display }

Figure

Updating...

References

Related subjects :