diff --git a/Fetch_rain/Fetch_rain.ino b/Fetch_rain/Fetch_rain.ino new file mode 100644 index 0000000..0fcfc6f --- /dev/null +++ b/Fetch_rain/Fetch_rain.ino @@ -0,0 +1,176 @@ +#include +#include // This library is already built in to the Arduino IDE +#include //This library you can add via Include Library > Manage Library > + +#include "secrets.h" + +LiquidCrystal_I2C lcd(0x27, 16, 2); + + +const char* ssid = WIFI_SSID; // defined in secrets.h (not versioned) +const char* password = WIFI_PASSWORD; // defined in secrets.h (not versioned) + +const char* host = "api.thingspeak.com"; + +void setup() { + Serial.begin(115200); + // We start by connecting to a WiFi network + + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + Serial.println(""); + delay(500); +} + + +void loop() { + + int t = today(); + if (t != -1 ) { + Serial.print("Today: "); + Serial.println(t); + delay(500); + } else { + Serial.println("Something went wrong, resetting..."); + setup(); + } + + int tm = tmrw(); + if (tm != -1 ) { + Serial.print("Tomorrow: "); + Serial.println(tm); + delay(500); + } else { + Serial.println("Something went wrong, resetting..."); + setup(); + } + + int aft = aftmrw(); + if (aft != -1 ) { + Serial.print("After tomorrow: "); + Serial.println(aft); + delay(500); + } else { + Serial.println("Something went wrong, resetting..."); + setup(); + } + + Serial.println(""); +} + + + + + + + + + + + + + + + + + +int today() { + + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return -1; + } + + // We now create a URI for the request + String url = "/apps/thinghttp/send_request?api_key=" + String(THINGHTTP_KEY_TODAY); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + delay(500); + + // Read all the lines of the reply from server and print them to Serial + String line = client.readStringUntil('\r'); + for (int i = 0; i < 18; i++) { + line = client.readStringUntil('\r'); + } + line = client.readStringUntil('%'); + int rain = line.toInt(); + return rain; +} + +int tmrw() { + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return -1; + } + + // We now create a URI for the request + String url = "/apps/thinghttp/send_request?api_key=" + String(THINGHTTP_KEY_TMRW); + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + delay(500); + + // Read all the lines of the reply from server and print them to Serial + String line = client.readStringUntil('\r'); + for (int i = 0; i < 18; i++) { + line = client.readStringUntil('\r'); + } + line = client.readStringUntil('%'); + + int rain = line.toInt(); + return rain; +} + +int aftmrw() { + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return -1; + } + + // We now create a URI for the request + String url = "/apps/thinghttp/send_request?api_key=" + String(THINGHTTP_KEY_AFTMRW); + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + delay(500); + + // Read all the lines of the reply from server and print them to Serial + String line = client.readStringUntil('\r'); + for (int i = 0; i < 18; i++) { + line = client.readStringUntil('\r'); + } + line = client.readStringUntil('%'); + + int rain = line.toInt(); + return rain; +} diff --git a/libs/DS3231/DS3231.cpp b/libs/DS3231/DS3231.cpp new file mode 100644 index 0000000..aa2f78b --- /dev/null +++ b/libs/DS3231/DS3231.cpp @@ -0,0 +1,447 @@ +/* + DS3231.cpp - Arduino/chipKit library support for the DS3231 I2C Real-Time Clock + Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved + + This library has been made to easily interface and use the DS3231 RTC with + an Arduino or chipKit. + + You can find the latest version of the library at + http://www.RinkyDinkElectronics.com/ + + This library is free software; you can redistribute it and/or + modify it under the terms of the CC BY-NC-SA 3.0 license. + Please see the included documents for further information. + + Commercial use of this library requires you to buy a license that + will allow commercial use. This includes using the library, + modified or not, as a tool to sell products. + + The license applies to all part of the library including the + examples and tools supplied with the library. +*/ +#include "DS3231.h" + +// Include hardware-specific functions for the correct MCU +#if defined(__AVR__) + #include "hardware/avr/HW_AVR.h" +#elif defined(__PIC32MX__) + #include "hardware/pic32/HW_PIC32.h" +#elif defined(__arm__) + #include "hardware/arm/HW_ARM.h" +#endif + +#define REG_SEC 0x00 +#define REG_MIN 0x01 +#define REG_HOUR 0x02 +#define REG_DOW 0x03 +#define REG_DATE 0x04 +#define REG_MON 0x05 +#define REG_YEAR 0x06 +#define REG_CON 0x0e +#define REG_STATUS 0x0f +#define REG_AGING 0x10 +#define REG_TEMPM 0x11 +#define REG_TEMPL 0x12 + +#define SEC_1970_TO_2000 946684800 + +static const uint8_t dim[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; + +/* Public */ + +Time::Time() +{ + this->year = 2014; + this->mon = 1; + this->date = 1; + this->hour = 0; + this->min = 0; + this->sec = 0; + this->dow = 3; +} + +DS3231::DS3231(uint8_t data_pin, uint8_t sclk_pin) +{ + _sda_pin = data_pin; + _scl_pin = sclk_pin; +} + +Time DS3231::getTime() +{ + Time t; + _burstRead(); + t.sec = _decode(_burstArray[0]); + t.min = _decode(_burstArray[1]); + t.hour = _decodeH(_burstArray[2]); + t.dow = _burstArray[3]; + t.date = _decode(_burstArray[4]); + t.mon = _decode(_burstArray[5]); + t.year = _decodeY(_burstArray[6])+2000; + return t; +} + +void DS3231::setTime(uint8_t hour, uint8_t min, uint8_t sec) +{ + if (((hour>=0) && (hour<24)) && ((min>=0) && (min<60)) && ((sec>=0) && (sec<60))) + { + _writeRegister(REG_HOUR, _encode(hour)); + _writeRegister(REG_MIN, _encode(min)); + _writeRegister(REG_SEC, _encode(sec)); + } +} + +void DS3231::setDate(uint8_t date, uint8_t mon, uint16_t year) +{ + if (((date>0) && (date<=31)) && ((mon>0) && (mon<=12)) && ((year>=2000) && (year<3000))) + { + year -= 2000; + _writeRegister(REG_YEAR, _encode(year)); + _writeRegister(REG_MON, _encode(mon)); + _writeRegister(REG_DATE, _encode(date)); + } +} + +void DS3231::setDOW() +{ + int dow; + byte mArr[12] = {6,2,2,5,0,3,5,1,4,6,2,4}; + Time _t = getTime(); + + dow = (_t.year % 100); + dow = dow*1.25; + dow += _t.date; + dow += mArr[_t.mon-1]; + if (((_t.year % 4)==0) && (_t.mon<3)) + dow -= 1; + while (dow>7) + dow -= 7; + _writeRegister(REG_DOW, dow); +} + +void DS3231::setDOW(uint8_t dow) +{ + if ((dow>0) && (dow<8)) + _writeRegister(REG_DOW, dow); +} + +char *DS3231::getTimeStr(uint8_t format) +{ + static char output[] = "xxxxxxxx"; + Time t; + t=getTime(); + if (t.hour<10) + output[0]=48; + else + output[0]=char((t.hour / 10)+48); + output[1]=char((t.hour % 10)+48); + output[2]=58; + if (t.min<10) + output[3]=48; + else + output[3]=char((t.min / 10)+48); + output[4]=char((t.min % 10)+48); + output[5]=58; + if (format==FORMAT_SHORT) + output[5]=0; + else + { + if (t.sec<10) + output[6]=48; + else + output[6]=char((t.sec / 10)+48); + output[7]=char((t.sec % 10)+48); + output[8]=0; + } + return (char*)&output; +} + +char *DS3231::getDateStr(uint8_t slformat, uint8_t eformat, char divider) +{ + static char output[] = "xxxxxxxxxx"; + int yr, offset; + Time t; + t=getTime(); + switch (eformat) + { + case FORMAT_LITTLEENDIAN: + if (t.date<10) + output[0]=48; + else + output[0]=char((t.date / 10)+48); + output[1]=char((t.date % 10)+48); + output[2]=divider; + if (t.mon<10) + output[3]=48; + else + output[3]=char((t.mon / 10)+48); + output[4]=char((t.mon % 10)+48); + output[5]=divider; + if (slformat==FORMAT_SHORT) + { + yr=t.year-2000; + if (yr<10) + output[6]=48; + else + output[6]=char((yr / 10)+48); + output[7]=char((yr % 10)+48); + output[8]=0; + } + else + { + yr=t.year; + output[6]=char((yr / 1000)+48); + output[7]=char(((yr % 1000) / 100)+48); + output[8]=char(((yr % 100) / 10)+48); + output[9]=char((yr % 10)+48); + output[10]=0; + } + break; + case FORMAT_BIGENDIAN: + if (slformat==FORMAT_SHORT) + offset=0; + else + offset=2; + if (slformat==FORMAT_SHORT) + { + yr=t.year-2000; + if (yr<10) + output[0]=48; + else + output[0]=char((yr / 10)+48); + output[1]=char((yr % 10)+48); + output[2]=divider; + } + else + { + yr=t.year; + output[0]=char((yr / 1000)+48); + output[1]=char(((yr % 1000) / 100)+48); + output[2]=char(((yr % 100) / 10)+48); + output[3]=char((yr % 10)+48); + output[4]=divider; + } + if (t.mon<10) + output[3+offset]=48; + else + output[3+offset]=char((t.mon / 10)+48); + output[4+offset]=char((t.mon % 10)+48); + output[5+offset]=divider; + if (t.date<10) + output[6+offset]=48; + else + output[6+offset]=char((t.date / 10)+48); + output[7+offset]=char((t.date % 10)+48); + output[8+offset]=0; + break; + case FORMAT_MIDDLEENDIAN: + if (t.mon<10) + output[0]=48; + else + output[0]=char((t.mon / 10)+48); + output[1]=char((t.mon % 10)+48); + output[2]=divider; + if (t.date<10) + output[3]=48; + else + output[3]=char((t.date / 10)+48); + output[4]=char((t.date % 10)+48); + output[5]=divider; + if (slformat==FORMAT_SHORT) + { + yr=t.year-2000; + if (yr<10) + output[6]=48; + else + output[6]=char((yr / 10)+48); + output[7]=char((yr % 10)+48); + output[8]=0; + } + else + { + yr=t.year; + output[6]=char((yr / 1000)+48); + output[7]=char(((yr % 1000) / 100)+48); + output[8]=char(((yr % 100) / 10)+48); + output[9]=char((yr % 10)+48); + output[10]=0; + } + break; + } + return (char*)&output; +} + +char *DS3231::getDOWStr(uint8_t format) +{ + char *output = "xxxxxxxxxx"; + char *daysLong[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; + char *daysShort[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; + Time t; + t=getTime(); + if (format == FORMAT_SHORT) + output = daysShort[t.dow-1]; + else + output = daysLong[t.dow-1]; + return output; +} + +char *DS3231::getMonthStr(uint8_t format) +{ + char *output= "xxxxxxxxx"; + char *monthLong[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; + char *monthShort[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + Time t; + t=getTime(); + if (format == FORMAT_SHORT) + output = monthShort[t.mon-1]; + else + output = monthLong[t.mon-1]; + return output; +} + +long DS3231::getUnixTime(Time t) +{ + uint16_t dc; + + dc = t.date; + for (uint8_t i = 0; i<(t.mon-1); i++) + dc += dim[i]; + if ((t.mon > 2) && (((t.year-2000) % 4) == 0)) + ++dc; + dc = dc + (365 * (t.year-2000)) + (((t.year-2000) + 3) / 4) - 1; + + return ((((((dc * 24L) + t.hour) * 60) + t.min) * 60) + t.sec) + SEC_1970_TO_2000; + +} + +void DS3231::enable32KHz(bool enable) +{ + uint8_t _reg = _readRegister(REG_STATUS); + _reg &= ~(1 << 3); + _reg |= (enable << 3); + _writeRegister(REG_STATUS, _reg); +} + +void DS3231::setOutput(byte enable) +{ + uint8_t _reg = _readRegister(REG_CON); + _reg &= ~(1 << 2); + _reg |= (enable << 2); + _writeRegister(REG_CON, _reg); +} + +void DS3231::setSQWRate(int rate) +{ + uint8_t _reg = _readRegister(REG_CON); + _reg &= ~(3 << 3); + _reg |= (rate << 3); + _writeRegister(REG_CON, _reg); +} + +float DS3231::getTemp() +{ + uint8_t _msb = _readRegister(REG_TEMPM); + uint8_t _lsb = _readRegister(REG_TEMPL); + return (float)_msb + ((_lsb >> 6) * 0.25f); +} + +/* Private */ + +void DS3231::_sendStart(byte addr) +{ + pinMode(_sda_pin, OUTPUT); + digitalWrite(_sda_pin, HIGH); + digitalWrite(_scl_pin, HIGH); + digitalWrite(_sda_pin, LOW); + digitalWrite(_scl_pin, LOW); + shiftOut(_sda_pin, _scl_pin, MSBFIRST, addr); +} + +void DS3231::_sendStop() +{ + pinMode(_sda_pin, OUTPUT); + digitalWrite(_sda_pin, LOW); + digitalWrite(_scl_pin, HIGH); + digitalWrite(_sda_pin, HIGH); + pinMode(_sda_pin, INPUT); +} + +void DS3231::_sendNack() +{ + pinMode(_sda_pin, OUTPUT); + digitalWrite(_scl_pin, LOW); + digitalWrite(_sda_pin, HIGH); + digitalWrite(_scl_pin, HIGH); + digitalWrite(_scl_pin, LOW); + pinMode(_sda_pin, INPUT); +} + +void DS3231::_sendAck() +{ + pinMode(_sda_pin, OUTPUT); + digitalWrite(_scl_pin, LOW); + digitalWrite(_sda_pin, LOW); + digitalWrite(_scl_pin, HIGH); + digitalWrite(_scl_pin, LOW); + pinMode(_sda_pin, INPUT); +} + +void DS3231::_waitForAck() +{ + pinMode(_sda_pin, INPUT); + digitalWrite(_scl_pin, HIGH); + while (digitalRead(_sda_pin)==HIGH) {} + digitalWrite(_scl_pin, LOW); +} + +uint8_t DS3231::_readByte() +{ + pinMode(_sda_pin, INPUT); + + uint8_t value = 0; + uint8_t currentBit = 0; + + for (int i = 0; i < 8; ++i) + { + digitalWrite(_scl_pin, HIGH); + currentBit = digitalRead(_sda_pin); + value |= (currentBit << 7-i); + delayMicroseconds(1); + digitalWrite(_scl_pin, LOW); + } + return value; +} + +void DS3231::_writeByte(uint8_t value) +{ + pinMode(_sda_pin, OUTPUT); + shiftOut(_sda_pin, _scl_pin, MSBFIRST, value); +} + +uint8_t DS3231::_decode(uint8_t value) +{ + uint8_t decoded = value & 127; + decoded = (decoded & 15) + 10 * ((decoded & (15 << 4)) >> 4); + return decoded; +} + +uint8_t DS3231::_decodeH(uint8_t value) +{ + if (value & 128) + value = (value & 15) + (12 * ((value & 32) >> 5)); + else + value = (value & 15) + (10 * ((value & 48) >> 4)); + return value; +} + +uint8_t DS3231::_decodeY(uint8_t value) +{ + uint8_t decoded = (value & 15) + 10 * ((value & (15 << 4)) >> 4); + return decoded; +} + +uint8_t DS3231::_encode(uint8_t value) +{ + uint8_t encoded = ((value / 10) << 4) + (value % 10); + return encoded; +} + diff --git a/libs/DS3231/DS3231.h b/libs/DS3231/DS3231.h new file mode 100644 index 0000000..cfc4aea --- /dev/null +++ b/libs/DS3231/DS3231.h @@ -0,0 +1,123 @@ +/* + DS3231.cpp - Arduino/chipKit library support for the DS3231 I2C Real-Time Clock + Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved + + This library has been made to easily interface and use the DS3231 RTC with + an Arduino or chipKit. + + You can find the latest version of the library at + http://www.RinkyDinkElectronics.com/ + + This library is free software; you can redistribute it and/or + modify it under the terms of the CC BY-NC-SA 3.0 license. + Please see the included documents for further information. + + Commercial use of this library requires you to buy a license that + will allow commercial use. This includes using the library, + modified or not, as a tool to sell products. + + The license applies to all part of the library including the + examples and tools supplied with the library. +*/ +#ifndef DS3231_h +#define DS3231_h + +#if defined(__AVR__) + #include "Arduino.h" + #include "hardware/avr/HW_AVR_defines.h" +#elif defined(__PIC32MX__) + #include "WProgram.h" + #include "hardware/pic32/HW_PIC32_defines.h" +#elif defined(__arm__) + #include "Arduino.h" + #include "hardware/arm/HW_ARM_defines.h" +#endif + +#define DS3231_ADDR_R 0xD1 +#define DS3231_ADDR_W 0xD0 +#define DS3231_ADDR 0x68 + +#define FORMAT_SHORT 1 +#define FORMAT_LONG 2 + +#define FORMAT_LITTLEENDIAN 1 +#define FORMAT_BIGENDIAN 2 +#define FORMAT_MIDDLEENDIAN 3 + +#define MONDAY 1 +#define TUESDAY 2 +#define WEDNESDAY 3 +#define THURSDAY 4 +#define FRIDAY 5 +#define SATURDAY 6 +#define SUNDAY 7 + +#define SQW_RATE_1 0 +#define SQW_RATE_1K 1 +#define SQW_RATE_4K 2 +#define SQW_RATE_8K 3 + +#define OUTPUT_SQW 0 +#define OUTPUT_INT 1 + +class Time +{ +public: + uint8_t hour; + uint8_t min; + uint8_t sec; + uint8_t date; + uint8_t mon; + uint16_t year; + uint8_t dow; + + Time(); +}; + +class DS3231 +{ + public: + DS3231(uint8_t data_pin, uint8_t sclk_pin); + void begin(); + Time getTime(); + void setTime(uint8_t hour, uint8_t min, uint8_t sec); + void setDate(uint8_t date, uint8_t mon, uint16_t year); + void setDOW(); + void setDOW(uint8_t dow); + + char *getTimeStr(uint8_t format=FORMAT_LONG); + char *getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.'); + char *getDOWStr(uint8_t format=FORMAT_LONG); + char *getMonthStr(uint8_t format=FORMAT_LONG); + long getUnixTime(Time t); + + void enable32KHz(bool enable); + void setOutput(byte enable); + void setSQWRate(int rate); + float getTemp(); + + private: + uint8_t _scl_pin; + uint8_t _sda_pin; + uint8_t _burstArray[7]; + boolean _use_hw; + + void _sendStart(byte addr); + void _sendStop(); + void _sendAck(); + void _sendNack(); + void _waitForAck(); + uint8_t _readByte(); + void _writeByte(uint8_t value); + void _burstRead(); + uint8_t _readRegister(uint8_t reg); + void _writeRegister(uint8_t reg, uint8_t value); + uint8_t _decode(uint8_t value); + uint8_t _decodeH(uint8_t value); + uint8_t _decodeY(uint8_t value); + uint8_t _encode(uint8_t vaule); +#if defined(__arm__) + Twi *twi; +#endif +}; +#endif diff --git a/libs/DS3231/Documentation/DS3231.pdf b/libs/DS3231/Documentation/DS3231.pdf new file mode 100644 index 0000000..92a446c Binary files /dev/null and b/libs/DS3231/Documentation/DS3231.pdf differ diff --git a/libs/DS3231/Documentation/version.txt b/libs/DS3231/Documentation/version.txt new file mode 100644 index 0000000..fab18fe --- /dev/null +++ b/libs/DS3231/Documentation/version.txt @@ -0,0 +1,3 @@ +Version: + 1.0 17 Aug 2014 - initial release + 1.01 25 Aug 2014 - small bugfix \ No newline at end of file diff --git a/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Legal.pdf b/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Legal.pdf new file mode 100644 index 0000000..ed326c9 Binary files /dev/null and b/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Legal.pdf differ diff --git a/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Summary.pdf b/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Summary.pdf new file mode 100644 index 0000000..b35e677 Binary files /dev/null and b/libs/DS3231/License/License - CC BY-NC-SA 3.0 - Summary.pdf differ diff --git a/libs/DS3231/examples/Arduino/DS3231_Serial_Easy/DS3231_Serial_Easy.ino b/libs/DS3231/examples/Arduino/DS3231_Serial_Easy/DS3231_Serial_Easy.ino new file mode 100644 index 0000000..3f1b925 --- /dev/null +++ b/libs/DS3231/examples/Arduino/DS3231_Serial_Easy/DS3231_Serial_Easy.ino @@ -0,0 +1,77 @@ +// DS3231_Serial_Easy +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// quickly send time and date information over a serial link +// +// To use the hardware I2C (TWI) interface of the Arduino you must connect +// the pins as follows: +// +// Arduino Uno/2009: +// ---------------------- +// DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin +// SCL pin -> Arduino Analog 5 or the dedicated SCL pin +// +// Arduino Leonardo: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin +// SCL pin -> Arduino Digital 3 or the dedicated SCL pin +// +// Arduino Mega: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin +// +// Arduino Due: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin +// +// The internal pull-up resistors will be activated when using the +// hardware I2C interfaces. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used, and you will also have to use appropriate, external +// pull-up resistors on the data and clock signals. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + // Uncomment the next line if you are using an Arduino Leonardo + //while (!Serial) {} + + // Initialize the rtc object + rtc.begin(); + + // The following lines can be uncommented to set the date and time + //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY + //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) + //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 +} + +void loop() +{ + // Send Day-of-Week + Serial.print(rtc.getDOWStr()); + Serial.print(" "); + + // Send date + Serial.print(rtc.getDateStr()); + Serial.print(" -- "); + + // Send time + Serial.println(rtc.getTimeStr()); + + // Wait one second before repeating :) + delay (1000); +} diff --git a/libs/DS3231/examples/Arduino/DS3231_Serial_Hard/DS3231_Serial_Hard.ino b/libs/DS3231/examples/Arduino/DS3231_Serial_Hard/DS3231_Serial_Hard.ino new file mode 100644 index 0000000..83635f0 --- /dev/null +++ b/libs/DS3231/examples/Arduino/DS3231_Serial_Hard/DS3231_Serial_Hard.ino @@ -0,0 +1,95 @@ +// DS3231_Serial_Hard +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// retrieve time- and date-data for you to manipulate. +// +// To use the hardware I2C (TWI) interface of the Arduino you must connect +// the pins as follows: +// +// Arduino Uno/2009: +// ---------------------- +// DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin +// SCL pin -> Arduino Analog 5 or the dedicated SCL pin +// +// Arduino Leonardo: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin +// SCL pin -> Arduino Digital 3 or the dedicated SCL pin +// +// Arduino Mega: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin +// +// Arduino Due: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin +// +// The internal pull-up resistors will be activated when using the +// hardware I2C interfaces. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used, and you will also have to use appropriate, external +// pull-up resistors on the data and clock signals. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +// Init a Time-data structure +Time t; + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + // Uncomment the next line if you are using an Arduino Leonardo + //while (!Serial) {} + + // Initialize the rtc object + rtc.begin(); + + // The following lines can be uncommented to set the date and time + //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY + //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) + //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 +} + +void loop() +{ + // Get data from the DS3231 + t = rtc.getTime(); + + // Send date over serial connection + Serial.print("Today is the "); + Serial.print(t.date, DEC); + Serial.print(". day of "); + Serial.print(rtc.getMonthStr()); + Serial.print(" in the year "); + Serial.print(t.year, DEC); + Serial.println("."); + + // Send Day-of-Week and time + Serial.print("It is the "); + Serial.print(t.dow, DEC); + Serial.print(". day of the week (counting monday as the 1th), and it has passed "); + Serial.print(t.hour, DEC); + Serial.print(" hour(s), "); + Serial.print(t.min, DEC); + Serial.print(" minute(s) and "); + Serial.print(t.sec, DEC); + Serial.println(" second(s) since midnight."); + + // Send a divider for readability + Serial.println(" - - - - - - - - - - - - - - - - - - - - -"); + + // Wait one second before repeating :) + delay (1000); +} diff --git a/libs/DS3231/examples/Arduino/DS3231_Temperature/DS3231_Temperature.ino b/libs/DS3231/examples/Arduino/DS3231_Temperature/DS3231_Temperature.ino new file mode 100644 index 0000000..97df676 --- /dev/null +++ b/libs/DS3231/examples/Arduino/DS3231_Temperature/DS3231_Temperature.ino @@ -0,0 +1,64 @@ +// DS3231_Temperature +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// quickly send the current temperature over a serial link +// +// To use the hardware I2C (TWI) interface of the Arduino you must connect +// the pins as follows: +// +// Arduino Uno/2009: +// ---------------------- +// DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin +// SCL pin -> Arduino Analog 5 or the dedicated SCL pin +// +// Arduino Leonardo: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin +// SCL pin -> Arduino Digital 3 or the dedicated SCL pin +// +// Arduino Mega: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin +// +// Arduino Due: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin +// +// The internal pull-up resistors will be activated when using the +// hardware I2C interfaces. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used, and you will also have to use appropriate, external +// pull-up resistors on the data and clock signals. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + // Uncomment the next line if you are using an Arduino Leonardo + //while (!Serial) {} + + // Initialize the rtc object + rtc.begin(); +} + +void loop() +{ + // Send current temperature + Serial.print("Temperature: "); + Serial.print(rtc.getTemp()); + Serial.println(" C"); + delay (1000); +} diff --git a/libs/DS3231/examples/Arduino/DS3231_UnixTime/DS3231_UnixTime.ino b/libs/DS3231/examples/Arduino/DS3231_UnixTime/DS3231_UnixTime.ino new file mode 100644 index 0000000..862411f --- /dev/null +++ b/libs/DS3231/examples/Arduino/DS3231_UnixTime/DS3231_UnixTime.ino @@ -0,0 +1,87 @@ +// DS3231_UnixTime +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// convert date and time to UnixTime +// +// To use the hardware I2C (TWI) interface of the Arduino you must connect +// the pins as follows: +// +// Arduino Uno/2009: +// ---------------------- +// DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin +// SCL pin -> Arduino Analog 5 or the dedicated SCL pin +// +// Arduino Leonardo: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin +// SCL pin -> Arduino Digital 3 or the dedicated SCL pin +// +// Arduino Mega: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin +// +// Arduino Due: +// ---------------------- +// DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin +// SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin +// +// The internal pull-up resistors will be activated when using the +// hardware I2C interfaces. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used, and you will also have to use appropriate, external +// pull-up resistors on the data and clock signals. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +Time t; + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + // Uncomment the next line if you are using an Arduino Leonardo + //while (!Serial) {} + + // Initialize the rtc object + rtc.begin(); +} + +void loop() +{ + // Send Current time + Serial.print("Current Time.............................: "); + Serial.print(rtc.getDOWStr()); + Serial.print(" "); + Serial.print(rtc.getDateStr()); + Serial.print(" -- "); + Serial.println(rtc.getTimeStr()); + + // Send Unixtime + // ** Note that there may be a one second difference between the current time ** + // ** and current unixtime show if the second changes between the two calls ** + Serial.print("Current Unixtime.........................: "); + Serial.println(rtc.getUnixTime(rtc.getTime())); + + // Send Unixtime for 00:00:00 on January 1th 2014 + Serial.print("Unixtime for 00:00:00 on January 1th 2014: "); + t.hour = 0; + t.min = 0; + t.sec = 0; + t.year = 2014; + t.mon = 1; + t.date = 1; + Serial.println(rtc.getUnixTime(t)); + + // Wait indefinitely + while (1) {}; +} diff --git a/libs/DS3231/examples/chipKit/DS3231_Serial_Easy/DS3231_Serial_Easy.pde b/libs/DS3231/examples/chipKit/DS3231_Serial_Easy/DS3231_Serial_Easy.pde new file mode 100644 index 0000000..7448e95 --- /dev/null +++ b/libs/DS3231/examples/chipKit/DS3231_Serial_Easy/DS3231_Serial_Easy.pde @@ -0,0 +1,65 @@ +// DS3231_Serial_Easy +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// quickly send time and date information over a serial link +// +// To use the hardware I2C (TWI) interface of the chipKit you must connect +// the pins as follows: +// +// chipKit Uno32/uC32: +// ---------------------- +// DS3231: SDA pin -> Analog 4 +// SCL pin -> Analog 5 +// *** Please note that JP6 and JP8 must be in the I2C position (closest to the analog pins) +// +// chipKit Max32: +// ---------------------- +// DS3231: SDA pin -> Digital 20 (the pin labeled SDA) +// SCL pin -> Digital 21 (the pin labeled SCL) +// +// The chipKit boards does not have pull-up resistors on the hardware I2C interface +// so external pull-up resistors on the data and clock signals are required. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + + // Initialize the rtc object + rtc.begin(); + + // The following lines can be uncommented to set the date and time + //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY + //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) + //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 +} + +void loop() +{ + // Send Day-of-Week + Serial.print(rtc.getDOWStr()); + Serial.print(" "); + + // Send date + Serial.print(rtc.getDateStr()); + Serial.print(" -- "); + + // Send time + Serial.println(rtc.getTimeStr()); + + // Wait one second before repeating :) + delay (1000); +} diff --git a/libs/DS3231/examples/chipKit/DS3231_Serial_Hard/DS3231_Serial_Hard.pde b/libs/DS3231/examples/chipKit/DS3231_Serial_Hard/DS3231_Serial_Hard.pde new file mode 100644 index 0000000..e17c74b --- /dev/null +++ b/libs/DS3231/examples/chipKit/DS3231_Serial_Hard/DS3231_Serial_Hard.pde @@ -0,0 +1,83 @@ +// DS3231_Serial_Hard +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// retrieve time- and date-data for you to manipulate. +// +// To use the hardware I2C (TWI) interface of the chipKit you must connect +// the pins as follows: +// +// chipKit Uno32/uC32: +// ---------------------- +// DS3231: SDA pin -> Analog 4 +// SCL pin -> Analog 5 +// *** Please note that JP6 and JP8 must be in the I2C position (closest to the analog pins) +// +// chipKit Max32: +// ---------------------- +// DS3231: SDA pin -> Digital 20 (the pin labeled SDA) +// SCL pin -> Digital 21 (the pin labeled SCL) +// +// The chipKit boards does not have pull-up resistors on the hardware I2C interface +// so external pull-up resistors on the data and clock signals are required. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +// Init a Time-data structure +Time t; + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + + // Initialize the rtc object + rtc.begin(); + + // The following lines can be uncommented to set the date and time + //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY + //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) + //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 +} + +void loop() +{ + // Get data from the DS3231 + t = rtc.getTime(); + + // Send date over serial connection + Serial.print("Today is the "); + Serial.print(t.date, DEC); + Serial.print(". day of "); + Serial.print(rtc.getMonthStr()); + Serial.print(" in the year "); + Serial.print(t.year, DEC); + Serial.println("."); + + // Send Day-of-Week and time + Serial.print("It is the "); + Serial.print(t.dow, DEC); + Serial.print(". day of the week (counting monday as the 1th), and it has passed "); + Serial.print(t.hour, DEC); + Serial.print(" hour(s), "); + Serial.print(t.min, DEC); + Serial.print(" minute(s) and "); + Serial.print(t.sec, DEC); + Serial.println(" second(s) since midnight."); + + // Send a divider for readability + Serial.println(" - - - - - - - - - - - - - - - - - - - - -"); + + // Wait one second before repeating :) + delay (1000); +} diff --git a/libs/DS3231/examples/chipKit/DS3231_Temperature/DS3231_Temperature.pde b/libs/DS3231/examples/chipKit/DS3231_Temperature/DS3231_Temperature.pde new file mode 100644 index 0000000..a352f87 --- /dev/null +++ b/libs/DS3231/examples/chipKit/DS3231_Temperature/DS3231_Temperature.pde @@ -0,0 +1,52 @@ +// DS3231_Temperature +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// quickly send the current temperature over a serial link +// +// To use the hardware I2C (TWI) interface of the chipKit you must connect +// the pins as follows: +// +// chipKit Uno32/uC32: +// ---------------------- +// DS3231: SDA pin -> Analog 4 +// SCL pin -> Analog 5 +// *** Please note that JP6 and JP8 must be in the I2C position (closest to the analog pins) +// +// chipKit Max32: +// ---------------------- +// DS3231: SDA pin -> Digital 20 (the pin labeled SDA) +// SCL pin -> Digital 21 (the pin labeled SCL) +// +// The chipKit boards does not have pull-up resistors on the hardware I2C interface +// so external pull-up resistors on the data and clock signals are required. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + + // Initialize the rtc object + rtc.begin(); +} + +void loop() +{ + // Send current temperature + Serial.print("Temperature: "); + Serial.print(rtc.getTemp()); + Serial.println(" C"); + delay (1000); +} diff --git a/libs/DS3231/examples/chipKit/DS3231_UnixTime/DS3231_UnixTime.pde b/libs/DS3231/examples/chipKit/DS3231_UnixTime/DS3231_UnixTime.pde new file mode 100644 index 0000000..55b2969 --- /dev/null +++ b/libs/DS3231/examples/chipKit/DS3231_UnixTime/DS3231_UnixTime.pde @@ -0,0 +1,75 @@ +// DS3231_UnixTime +// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved +// web: http://www.RinkyDinkElectronics.com/ +// +// A quick demo of how to use my DS3231-library to +// convert date and time to UnixTime +// +// To use the hardware I2C (TWI) interface of the chipKit you must connect +// the pins as follows: +// +// chipKit Uno32/uC32: +// ---------------------- +// DS3231: SDA pin -> Analog 4 +// SCL pin -> Analog 5 +// *** Please note that JP6 and JP8 must be in the I2C position (closest to the analog pins) +// +// chipKit Max32: +// ---------------------- +// DS3231: SDA pin -> Digital 20 (the pin labeled SDA) +// SCL pin -> Digital 21 (the pin labeled SCL) +// +// The chipKit boards does not have pull-up resistors on the hardware I2C interface +// so external pull-up resistors on the data and clock signals are required. +// +// You can connect the DS3231 to any available pin but if you use any +// other than what is described above the library will fall back to +// a software-based, TWI-like protocol which will require exclusive access +// to the pins used. +// + +#include + +// Init the DS3231 using the hardware interface +DS3231 rtc(SDA, SCL); + +Time t; + +void setup() +{ + // Setup Serial connection + Serial.begin(115200); + + // Initialize the rtc object + rtc.begin(); +} + +void loop() +{ + // Send Current time + Serial.print("Current Time.............................: "); + Serial.print(rtc.getDOWStr()); + Serial.print(" "); + Serial.print(rtc.getDateStr()); + Serial.print(" -- "); + Serial.println(rtc.getTimeStr()); + + // Send Unixtime + // ** Note that there may be a one second difference between the current time ** + // ** and current unixtime show if the second changes between the two calls ** + Serial.print("Current Unixtime.........................: "); + Serial.println(rtc.getUnixTime(rtc.getTime())); + + // Send Unixtime for 00:00:00 on January 1th 2014 + Serial.print("Unixtime for 00:00:00 on January 1th 2014: "); + t.hour = 0; + t.min = 0; + t.sec = 0; + t.year = 2014; + t.mon = 1; + t.date = 1; + Serial.println(rtc.getUnixTime(t)); + + // Wait indefinitely + while (1) {}; +} diff --git a/libs/DS3231/hardware/arm/HW_ARM.h b/libs/DS3231/hardware/arm/HW_ARM.h new file mode 100644 index 0000000..6614ef7 --- /dev/null +++ b/libs/DS3231/hardware/arm/HW_ARM.h @@ -0,0 +1,154 @@ +void DS3231::begin() +{ + _use_hw = false; + if ((_sda_pin == SDA) and (_scl_pin == SCL)) + { + _use_hw = true; + twi = TWI1; + pmc_enable_periph_clk(WIRE_INTERFACE_ID); + PIO_Configure(g_APinDescription[PIN_WIRE_SDA].pPort, g_APinDescription[PIN_WIRE_SDA].ulPinType, g_APinDescription[PIN_WIRE_SDA].ulPin, g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration); + PIO_Configure(g_APinDescription[PIN_WIRE_SCL].pPort, g_APinDescription[PIN_WIRE_SCL].ulPinType, g_APinDescription[PIN_WIRE_SCL].ulPin, g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration); + NVIC_DisableIRQ(TWI1_IRQn); + NVIC_ClearPendingIRQ(TWI1_IRQn); + NVIC_SetPriority(TWI1_IRQn, 0); + NVIC_EnableIRQ(TWI1_IRQn); + + } + else if ((_sda_pin == SDA1) and (_scl_pin == SCL1)) + { + _use_hw = true; + twi = TWI0; + pmc_enable_periph_clk(WIRE1_INTERFACE_ID); + PIO_Configure(g_APinDescription[PIN_WIRE1_SDA].pPort, g_APinDescription[PIN_WIRE1_SDA].ulPinType, g_APinDescription[PIN_WIRE1_SDA].ulPin, g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration); + PIO_Configure(g_APinDescription[PIN_WIRE1_SCL].pPort, g_APinDescription[PIN_WIRE1_SCL].ulPinType, g_APinDescription[PIN_WIRE1_SCL].ulPin, g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration); + NVIC_DisableIRQ(TWI0_IRQn); + NVIC_ClearPendingIRQ(TWI0_IRQn); + NVIC_SetPriority(TWI0_IRQn, 0); + NVIC_EnableIRQ(TWI0_IRQn); + } + + if (_use_hw) + { + // activate internal pullups for twi. + digitalWrite(SDA, 1); + digitalWrite(SCL, 1); + + // Reset the TWI + twi->TWI_CR = TWI_CR_SWRST; + // TWI Slave Mode Disabled, TWI Master Mode Disabled. + twi->TWI_CR = TWI_CR_SVDIS; + twi->TWI_CR = TWI_CR_MSDIS; + // Set TWI Speed + twi->TWI_CWGR = (TWI_DIV << 16) | (TWI_SPEED << 8) | TWI_SPEED; + // Set master mode + twi->TWI_CR = TWI_CR_MSEN; + } + else + { + pinMode(_scl_pin, OUTPUT); + } +} + +void DS3231::_burstRead() +{ + if (_use_hw) + { + // Set slave address and number of internal address bytes. + twi->TWI_MMR = (1 << 8) | TWI_MMR_MREAD | (DS3231_ADDR << 16); + // Set internal address bytes + twi->TWI_IADR = 0; + // Send START condition + twi->TWI_CR = TWI_CR_START; + + for (int i=0; i<6; i++) + { + while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY) + { + }; + _burstArray[i] = twi->TWI_RHR; + } + + twi->TWI_CR = TWI_CR_STOP; + while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY) {}; + _burstArray[6] = twi->TWI_RHR; + while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {}; + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(0); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + + for (int i=0; i<7; i++) + { + _burstArray[i] = _readByte(); + if (i<6) + _sendAck(); + else + _sendNack(); + } + _sendStop(); + } +} + +uint8_t DS3231::_readRegister(uint8_t reg) +{ + uint8_t readValue=0; + + if (_use_hw) + { + // Set slave address and number of internal address bytes. + twi->TWI_MMR = (1 << 8) | TWI_MMR_MREAD | (DS3231_ADDR << 16); + // Set internal address bytes + twi->TWI_IADR = reg; + // Send START and STOP condition to read a single byte + twi->TWI_CR = TWI_CR_START | TWI_CR_STOP; + while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY) {}; + readValue = twi->TWI_RHR; + while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {}; + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + readValue = _readByte(); + _sendNack(); + _sendStop(); + } + return readValue; +} + +void DS3231::_writeRegister(uint8_t reg, uint8_t value) +{ + if (_use_hw) + { + // Set slave address and number of internal address bytes. + twi->TWI_MMR = (1 << 8) | (DS3231_ADDR << 16); + // Set internal address bytes + twi->TWI_IADR = reg; + // Send a single byte to start transfer + twi->TWI_THR = value; + while ((twi->TWI_SR & TWI_SR_TXRDY) != TWI_SR_TXRDY) {}; + // Send STOP condition + twi->TWI_CR = TWI_CR_STOP; + while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {}; + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _writeByte(value); + _waitForAck(); + _sendStop(); + } +} + diff --git a/libs/DS3231/hardware/arm/HW_ARM_defines.h b/libs/DS3231/hardware/arm/HW_ARM_defines.h new file mode 100644 index 0000000..d00ba7d --- /dev/null +++ b/libs/DS3231/hardware/arm/HW_ARM_defines.h @@ -0,0 +1,13 @@ +// *** Hardwarespecific defines *** +#define SDA 20 +#define SCL 21 +#define SDA1 70 +#define SCL1 71 + +#define TWI_SPEED TWI_SPEED_400k // Set default TWI Speed +#define TWI_SPEED_100k 208 +#define TWI_SPEED_400k 101 + +#define TWI_DIV TWI_DIV_400k // Set divider for TWI Speed (must match TWI_SPEED setting) +#define TWI_DIV_100k 1 +#define TWI_DIV_400k 0 diff --git a/libs/DS3231/hardware/avr/HW_AVR.h b/libs/DS3231/hardware/avr/HW_AVR.h new file mode 100644 index 0000000..73583e3 --- /dev/null +++ b/libs/DS3231/hardware/avr/HW_AVR.h @@ -0,0 +1,153 @@ +void DS3231::begin() +{ + if ((_sda_pin == SDA) and (_scl_pin == SCL)) + { + _use_hw = true; + // activate internal pullups for twi. + digitalWrite(SDA, HIGH); + digitalWrite(SCL, HIGH); + //delay(1); // Workaround for a linker bug + + // initialize twi prescaler and bit rate + cbi(TWSR, TWPS0); + cbi(TWSR, TWPS1); + TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; + + // enable twi module, acks, and twi interrupt + TWCR = _BV(TWEN) | _BV(TWIE)/* | _BV(TWEA)*/; + } + else + { + _use_hw = false; + pinMode(_scl_pin, OUTPUT); + } +} + +void DS3231::_burstRead() +{ + if (_use_hw) + { + // Send start address + TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = DS3231_ADDR_W; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = 0; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + + // Read data starting from start address + TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send rep. START + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = DS3231_ADDR_R; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + for (int i=0; i<7; i++) + { + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Send ACK and clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + _burstArray[i] = TWDR; + } + TWCR = _BV(TWEN) | _BV(TWINT); // Send NACK and clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + + TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(0); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + + for (int i=0; i<7; i++) + { + _burstArray[i] = _readByte(); + if (i<6) + _sendAck(); + else + _sendNack(); + } + _sendStop(); + } +} + +uint8_t DS3231::_readRegister(uint8_t reg) +{ + uint8_t readValue=0; + + if (_use_hw) + { + // Send start address + TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = DS3231_ADDR_W; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = reg; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + + // Read data starting from start address + TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send rep. START + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = DS3231_ADDR_R; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Send ACK and clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + readValue = TWDR; + TWCR = _BV(TWEN) | _BV(TWINT); // Send NACK and clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + + TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + readValue = _readByte(); + _sendNack(); + _sendStop(); + } + return readValue; +} + +void DS3231::_writeRegister(uint8_t reg, uint8_t value) +{ + if (_use_hw) + { + // Send start address + TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = DS3231_ADDR_W; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = reg; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + TWDR = value; + TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed + while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready + + TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _writeByte(value); + _waitForAck(); + _sendStop(); + } +} + diff --git a/libs/DS3231/hardware/avr/HW_AVR_defines.h b/libs/DS3231/hardware/avr/HW_AVR_defines.h new file mode 100644 index 0000000..2139d12 --- /dev/null +++ b/libs/DS3231/hardware/avr/HW_AVR_defines.h @@ -0,0 +1,6 @@ +// *** Hardwarespecific defines *** +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) + +#ifndef TWI_FREQ + #define TWI_FREQ 400000L +#endif diff --git a/libs/DS3231/hardware/pic32/HW_PIC32.h b/libs/DS3231/hardware/pic32/HW_PIC32.h new file mode 100644 index 0000000..af81e5f --- /dev/null +++ b/libs/DS3231/hardware/pic32/HW_PIC32.h @@ -0,0 +1,192 @@ +inline void _waitForIdleBus() { while (I2C1CON & 0x1f) {} } + +void DS3231::begin() +{ + if ((_sda_pin == SDA) and (_scl_pin == SCL)) + { + uint32_t tpgd; + + _use_hw = true; + pinMode(SDA, OUTPUT); + digitalWrite(SDA, HIGH); + IFS0CLR = 0xE0000000; // Clear Interrupt Flag + IEC0CLR = 0xE0000000; // Disable Interrupt + I2C1CONCLR = (1 << _I2CCON_ON); // Disable I2C interface + tpgd = ((F_CPU / 8) * 104) / 125000000; + I2C1BRG = (F_CPU / (2 * TWI_FREQ) - tpgd) - 2; // Set I2C Speed + I2C1ADD = DS3231_ADDR; // Set I2C device address + I2C1CONSET = (1 << _I2CCON_ON) | (1 << _I2CCON_STREN); // Enable I2C Interface + } + else + { + _use_hw = false; + pinMode(_scl_pin, OUTPUT); + } +} + +void DS3231::_burstRead() +{ + if (_use_hw) + { + _waitForIdleBus(); // Wait for I2C bus to be Idle before starting + I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition + if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision + while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish + I2C1TRN = (DS3231_ADDR<<1); // Send device Write address + while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision + { + I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag + I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address + } + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + I2C1TRN = 0; // Send the register address + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + _waitForIdleBus(); // Wait for I2C bus to be Idle before starting + I2C1CONSET = (1 << _I2CCON_RSEN); // Send start condition + if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision + while (I2C1CON & (1 << _I2CCON_RSEN)) {} // Wait for start condition to finish + I2C1TRN = (DS3231_ADDR<<1) | 1; // Send device Read address + while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision + { + I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag + I2C1TRN = (DS3231_ADDR<<1) | 1; // Retry send device Read address + } + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + byte dummy = I2C1RCV; // Clear _I2CSTAT_RBF (Receive Buffer Full) + for (int i=0; i<7; i++) + { + _waitForIdleBus(); // Wait for I2C bus to be Idle before continuing + I2C1CONSET = (1 << _I2CCON_RCEN); // Set RCEN to start receive + while (I2C1CON & (1 << _I2CCON_RCEN)) {} // Wait for Receive operation to finish + while (!(I2C1STAT & (1 << _I2CSTAT_RBF))) {} // Wait for Receive Buffer Full + _burstArray[i] = I2C1RCV; // Read data + if (i == 6) + I2C1CONSET = (1 << _I2CCON_ACKDT); // Prepare to send NACK + else + I2C1CONCLR = (1 << _I2CCON_ACKDT); // Prepare to send ACK + I2C1CONSET = (1 << _I2CCON_ACKEN); // Send ACK/NACK + while (I2C1CON & (1 << _I2CCON_ACKEN)) {} // Wait for ACK/NACK send to finish + } + I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition + while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(0); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + + for (int i=0; i<7; i++) + { + _burstArray[i] = _readByte(); + if (i<6) + _sendAck(); + else + _sendNack(); + } + _sendStop(); + } +} + +uint8_t DS3231::_readRegister(uint8_t reg) +{ + uint8_t readValue=0; + + if (_use_hw) + { + _waitForIdleBus(); // Wait for I2C bus to be Idle before starting + I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition + if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return 0xff; }// Check if there is a bus collision + while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish + I2C1TRN = (DS3231_ADDR<<1); // Send device Write address + while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision + { + I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag + I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address + } + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + I2C1TRN = reg; // Send the register address + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + _waitForIdleBus(); // Wait for I2C bus to be Idle before starting + I2C1CONSET = (1 << _I2CCON_RSEN); // Send start condition + if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return 0xff; }// Check if there is a bus collision + while (I2C1CON & (1 << _I2CCON_RSEN)) {} // Wait for start condition to finish + I2C1TRN = (DS3231_ADDR<<1) | 1; // Send device Read address + while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision + { + I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag + I2C1TRN = (DS3231_ADDR<<1) | 1; // Retry send device Read address + } + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + byte dummy = I2C1RCV; // Clear _I2CSTAT_RBF (Receive Buffer Full) + _waitForIdleBus(); // Wait for I2C bus to be Idle before continuing + I2C1CONSET = (1 << _I2CCON_RCEN); // Set RCEN to start receive + while (I2C1CON & (1 << _I2CCON_RCEN)) {} // Wait for Receive operation to finish + while (!(I2C1STAT & (1 << _I2CSTAT_RBF))) {} // Wait for Receive Buffer Full + readValue = I2C1RCV; // Read data + I2C1CONSET = (1 << _I2CCON_ACKDT); // Prepare to send NACK + I2C1CONSET = (1 << _I2CCON_ACKEN); // Send NACK + while (I2C1CON & (1 << _I2CCON_ACKEN)) {} // Wait for NACK send to finish + I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition + while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _sendStart(DS3231_ADDR_R); + _waitForAck(); + readValue = _readByte(); + _sendNack(); + _sendStop(); + } + return readValue; +} + +void DS3231::_writeRegister(uint8_t reg, uint8_t value) +{ + if (_use_hw) + { + _waitForIdleBus(); // Wait for I2C bus to be Idle before starting + I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition + if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision + while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish + I2C1TRN = (DS3231_ADDR<<1); // Send device Write address + while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision + { + I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag + I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address + } + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + I2C1TRN = reg; // Send the 1st data byte + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + I2C1TRN = value; // Send the 2nd data byte + while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish + while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK + I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition + while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish + } + else + { + _sendStart(DS3231_ADDR_W); + _waitForAck(); + _writeByte(reg); + _waitForAck(); + _writeByte(value); + _waitForAck(); + _sendStop(); + } +} diff --git a/libs/DS3231/hardware/pic32/HW_PIC32_defines.h b/libs/DS3231/hardware/pic32/HW_PIC32_defines.h new file mode 100644 index 0000000..3f3e686 --- /dev/null +++ b/libs/DS3231/hardware/pic32/HW_PIC32_defines.h @@ -0,0 +1,21 @@ +// *** Hardwarespecific defines *** + +#if !defined(_UP_MCU_) + #if defined(__32MX320F128H__) + #define SDA 18 // A4 (Remeber to set the jumper correctly) + #define SCL 19 // A5 (Remeber to set the jumper correctly) + #elif defined(__32MX340F512H__) + #define SDA 18 // A4 (Remeber to set the jumper correctly) + #define SCL 19 // A5 (Remeber to set the jumper correctly) + #elif defined(__32MX795F512L__) + #define SDA 20 // Digital 20 + #define SCL 21 // Digital 21 + #else + #error "Unsupported PIC32 MCU!" + #endif +#endif + +#ifndef TWI_FREQ + #define TWI_FREQ 400000L +#endif + diff --git a/libs/DS3231/keywords.txt b/libs/DS3231/keywords.txt new file mode 100644 index 0000000..aae56df --- /dev/null +++ b/libs/DS3231/keywords.txt @@ -0,0 +1,53 @@ +DS3231 KEYWORD1 +Time KEYWORD1 + +begin KEYWORD2 +getTime KEYWORD2 +setTime KEYWORD2 +setDate KEYWORD2 +setDOW KEYWORD2 +getTimeStr KEYWORD2 +getDateStr KEYWORD2 +getDOWStr KEYWORD2 +getMonthStr KEYWORD2 +getUnixTime KEYWORD2 +enable32KHz KEYWORD2 +setOutput KEYWORD2 +setSQWRate KEYWORD2 +getTemp KEYWORD2 + +hour KEYWORD2 +min KEYWORD2 +sec KEYWORD2 +date KEYWORD2 +mon KEYWORD2 +year KEYWORD2 +dow KEYWORD2 + +FORMAT_SHORT LITERAL1 +FORMAT_LONG LITERAL1 + +FORMAT_LITTLEENDIAN LITERAL1 +FORMAT_BIGENDIAN LITERAL1 +FORMAT_MIDDLEENDIAN LITERAL1 + +MONDAY LITERAL1 +TUESDAY LITERAL1 +WEDNESDAY LITERAL1 +THURSDAY LITERAL1 +FRIDAY LITERAL1 +SATURDAY LITERAL1 +SUNDAY LITERAL1 + +SQW_RATE_1 LITERAL1 +SQW_RATE_1K LITERAL1 +SQW_RATE_4K LITERAL1 +SQW_RATE_8K LITERAL1 + +OUTPUT_SQW LITERAL1 +OUTPUT_INT LITERAL1 + +SDA LITERAL1 +SCL LITERAL1 +SDA1 LITERAL1 +SCL1 LITERAL1