1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-10-28 14:25:17 +01:00

initial commit microcontroller unit (mcu) arduino sketch for the custom bluetooth scale.

This commit is contained in:
OliE
2014-12-13 16:54:05 +01:00
parent 45081b8f09
commit 9711f35e4f
36 changed files with 4875 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
*.zip
*.7z

View File

@@ -0,0 +1,335 @@
/*----------------------------------------------------------------------*
* DS3232RTC.cpp - Arduino library for the Maxim Integrated DS3232 *
* Real-Time Clock. This library is intended for use with the Arduino *
* Time.h library, http://www.arduino.cc/playground/Code/Time *
* *
* This library is a drop-in replacement for the DS1307RTC.h library *
* by Michael Margolis that is supplied with the Arduino Time library *
* above. To change from using a DS1307 RTC to an DS3232 RTC, it is *
* only necessary to change the #include statement to include this *
* library instead of DS1307RTC.h. *
* *
* In addition, this library implements functions to support the *
* additional features of the DS3232. *
* *
* This library will also work with the DS3231 RTC, which has the same *
* features of the DS3232 except: (1) Battery-backed SRAM, (2) Battery- *
* backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), *
* and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in *
* the Control/Status register). *
* *
* Whether used with the DS3232 or DS3231, the user is responsible for *
* ensuring reads and writes do not exceed the device's address space *
* (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking *
* is done by this library. *
* *
* Jack Christensen 06Mar2013 *
* 28Aug2013 Changed the lower level methods to return the status of *
* the I2C communication with the RTC. Thanks to *
* Rob Tillaart for the suggestion. (Fixes issue #1.) *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 444 Castro Street, Suite 900, *
* Mountain View, CA 94041. *
*----------------------------------------------------------------------*/
#include <DS3232RTC.h>
//define release-independent I2C functions
#if defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#include <TinyWireM.h>
#define i2cBegin TinyWireM.begin
#define i2cBeginTransmission TinyWireM.beginTransmission
#define i2cEndTransmission TinyWireM.endTransmission
#define i2cRequestFrom TinyWireM.requestFrom
#define i2cRead TinyWireM.receive
#define i2cWrite TinyWireM.send
#elif ARDUINO >= 100
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.read
#define i2cWrite Wire.write
#else
#include <Wire.h>
#define i2cBegin Wire.begin
#define i2cBeginTransmission Wire.beginTransmission
#define i2cEndTransmission Wire.endTransmission
#define i2cRequestFrom Wire.requestFrom
#define i2cRead Wire.receive
#define i2cWrite Wire.send
#endif
/*----------------------------------------------------------------------*
* Constructor. *
*----------------------------------------------------------------------*/
DS3232RTC::DS3232RTC()
{
i2cBegin();
}
/*----------------------------------------------------------------------*
* Read the current time from the RTC and return it as a time_t value. *
* Returns a zero value if an I2C error occurred (e.g. RTC *
* not present). *
*----------------------------------------------------------------------*/
time_t DS3232RTC::get()
{
tmElements_t tm;
if ( read(tm) ) return 0;
return( makeTime(tm) );
}
/*----------------------------------------------------------------------*
* Set the RTC to the given time_t value. *
* Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::set(time_t t)
{
tmElements_t tm;
breakTime(t, tm);
return ( write(tm) );
}
/*----------------------------------------------------------------------*
* Read the current time from the RTC and return it in a tmElements_t *
* structure. Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::read(tmElements_t &tm)
{
i2cBeginTransmission(RTC_ADDR);
i2cWrite((uint8_t)RTC_SECONDS);
if ( byte e = i2cEndTransmission() ) return e;
//request 7 bytes (secs, min, hr, dow, date, mth, yr)
i2cRequestFrom(RTC_ADDR, tmNbrFields);
tm.Second = bcd2dec(i2cRead() & ~_BV(DS1307_CH));
tm.Minute = bcd2dec(i2cRead());
tm.Hour = bcd2dec(i2cRead() & ~_BV(HR1224)); //assumes 24hr clock
tm.Wday = i2cRead();
tm.Day = bcd2dec(i2cRead());
tm.Month = bcd2dec(i2cRead() & ~_BV(CENTURY)); //don't use the Century bit
tm.Year = y2kYearToTm(bcd2dec(i2cRead()));
return 0;
}
/*----------------------------------------------------------------------*
* Set the RTC's time from a tmElements_t structure. *
* Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::write(tmElements_t &tm)
{
i2cBeginTransmission(RTC_ADDR);
i2cWrite((uint8_t)RTC_SECONDS);
i2cWrite(dec2bcd(tm.Second));
i2cWrite(dec2bcd(tm.Minute));
i2cWrite(dec2bcd(tm.Hour)); //sets 24 hour format (Bit 6 == 0)
i2cWrite(tm.Wday);
i2cWrite(dec2bcd(tm.Day));
i2cWrite(dec2bcd(tm.Month));
i2cWrite(dec2bcd(tmYearToY2k(tm.Year)));
return i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Write multiple bytes to RTC RAM. *
* Valid address range is 0x00 - 0xFF, no checking. *
* Number of bytes (nBytes) must be between 1 and 31 (Wire library *
* limitation). *
* Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::writeRTC(byte addr, byte *values, byte nBytes)
{
i2cBeginTransmission(RTC_ADDR);
i2cWrite(addr);
for (byte i=0; i<nBytes; i++) i2cWrite(values[i]);
return i2cEndTransmission();
}
/*----------------------------------------------------------------------*
* Write a single byte to RTC RAM. *
* Valid address range is 0x00 - 0xFF, no checking. *
* Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::writeRTC(byte addr, byte value)
{
return ( writeRTC(addr, &value, 1) );
}
/*----------------------------------------------------------------------*
* Read multiple bytes from RTC RAM. *
* Valid address range is 0x00 - 0xFF, no checking. *
* Number of bytes (nBytes) must be between 1 and 32 (Wire library *
* limitation). *
* Returns the I2C status (zero if successful). *
*----------------------------------------------------------------------*/
byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes)
{
i2cBeginTransmission(RTC_ADDR);
i2cWrite(addr);
if ( byte e = i2cEndTransmission() ) return e;
i2cRequestFrom( (uint8_t)RTC_ADDR, nBytes );
for (byte i=0; i<nBytes; i++) values[i] = i2cRead();
return 0;
}
/*----------------------------------------------------------------------*
* Read a single byte from RTC RAM. *
* Valid address range is 0x00 - 0xFF, no checking. *
*----------------------------------------------------------------------*/
byte DS3232RTC::readRTC(byte addr)
{
byte b;
readRTC(addr, &b, 1);
return b;
}
/*----------------------------------------------------------------------*
* Set an alarm time. Sets the alarm registers only. To cause the *
* INT pin to be asserted on alarm match, use alarmInterrupt(). *
* This method can set either Alarm 1 or Alarm 2, depending on the *
* value of alarmType (use a value from the ALARM_TYPES_t enumeration). *
* When setting Alarm 2, the seconds value must be supplied but is *
* ignored, recommend using zero. (Alarm 2 has no seconds register.) *
*----------------------------------------------------------------------*/
void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate)
{
uint8_t addr;
seconds = dec2bcd(seconds);
minutes = dec2bcd(minutes);
hours = dec2bcd(hours);
daydate = dec2bcd(daydate);
if (alarmType & 0x01) seconds |= _BV(A1M1);
if (alarmType & 0x02) minutes |= _BV(A1M2);
if (alarmType & 0x04) hours |= _BV(A1M3);
if (alarmType & 0x10) hours |= _BV(DYDT);
if (alarmType & 0x08) daydate |= _BV(A1M4);
if ( !(alarmType & 0x80) ) { //alarm 1
addr = ALM1_SECONDS;
writeRTC(addr++, seconds);
}
else {
addr = ALM2_MINUTES;
}
writeRTC(addr++, minutes);
writeRTC(addr++, hours);
writeRTC(addr++, daydate);
}
/*----------------------------------------------------------------------*
* Set an alarm time. Sets the alarm registers only. To cause the *
* INT pin to be asserted on alarm match, use alarmInterrupt(). *
* This method can set either Alarm 1 or Alarm 2, depending on the *
* value of alarmType (use a value from the ALARM_TYPES_t enumeration). *
* However, when using this method to set Alarm 1, the seconds value *
* is set to zero. (Alarm 2 has no seconds register.) *
*----------------------------------------------------------------------*/
void DS3232RTC::setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate)
{
setAlarm(alarmType, 0, minutes, hours, daydate);
}
/*----------------------------------------------------------------------*
* Enable or disable an alarm "interrupt" which asserts the INT pin *
* on the RTC. *
*----------------------------------------------------------------------*/
void DS3232RTC::alarmInterrupt(byte alarmNumber, boolean interruptEnabled)
{
uint8_t controlReg, mask;
controlReg = readRTC(RTC_CONTROL);
mask = _BV(A1IE) << (alarmNumber - 1);
if (interruptEnabled)
controlReg |= mask;
else
controlReg &= ~mask;
writeRTC(RTC_CONTROL, controlReg);
}
/*----------------------------------------------------------------------*
* Returns true or false depending on whether the given alarm has been *
* triggered, and resets the alarm flag bit. *
*----------------------------------------------------------------------*/
boolean DS3232RTC::alarm(byte alarmNumber)
{
uint8_t statusReg, mask;
statusReg = readRTC(RTC_STATUS);
mask = _BV(A1F) << (alarmNumber - 1);
if (statusReg & mask) {
statusReg &= ~mask;
writeRTC(RTC_STATUS, statusReg);
return true;
}
else {
return false;
}
}
/*----------------------------------------------------------------------*
* Enable or disable the square wave output. *
* Use a value from the SQWAVE_FREQS_t enumeration for the parameter. *
*----------------------------------------------------------------------*/
void DS3232RTC::squareWave(SQWAVE_FREQS_t freq)
{
uint8_t controlReg;
controlReg = readRTC(RTC_CONTROL);
if (freq >= SQWAVE_NONE) {
controlReg |= _BV(INTCN);
}
else {
controlReg = (controlReg & 0xE3) | (freq << RS1);
}
writeRTC(RTC_CONTROL, controlReg);
}
/*----------------------------------------------------------------------*
* Checks the OSF bit in the status register which indicates that the *
* oscillator is or was stopped. *
*----------------------------------------------------------------------*/
boolean DS3232RTC::oscStopped(void)
{
return ( readRTC(RTC_STATUS) & _BV(OSF) );
}
/*----------------------------------------------------------------------*
* Returns the temperature in Celsius times four. *
*----------------------------------------------------------------------*/
int DS3232RTC::temperature(void)
{
union int16_byte {
int i;
byte b[2];
} rtcTemp;
rtcTemp.b[0] = readRTC(TEMP_LSB);
rtcTemp.b[1] = readRTC(TEMP_MSB);
return rtcTemp.i >> 6;
}
/*----------------------------------------------------------------------*
* Decimal-to-BCD conversion *
*----------------------------------------------------------------------*/
uint8_t DS3232RTC::dec2bcd(uint8_t n)
{
return n + 6 * (n / 10);
}
/*----------------------------------------------------------------------*
* BCD-to-Decimal conversion *
*----------------------------------------------------------------------*/
uint8_t __attribute__ ((noinline)) DS3232RTC::bcd2dec(uint8_t n)
{
return n - 6 * (n >> 4);
}
DS3232RTC RTC = DS3232RTC(); //instantiate an RTC object

View File

@@ -0,0 +1,157 @@
/*----------------------------------------------------------------------*
* DS3232RTC.h - Arduino library for the Maxim Integrated DS3232 *
* Real-Time Clock. This library is intended for use with the Arduino *
* Time.h library, http://www.arduino.cc/playground/Code/Time *
* *
* This library is a drop-in replacement for the DS1307RTC.h library *
* by Michael Margolis that is supplied with the Arduino Time library *
* above. To change from using a DS1307 RTC to an DS3232 RTC, it is *
* only necessary to change the #include statement to include this *
* library instead of DS1307RTC.h. *
* *
* In addition, this library implements functions to support the *
* additional features of the DS3232. *
* *
* This library will also work with the DS3231 RTC, which has the same *
* features of the DS3232 except: (1) Battery-backed SRAM, (2) Battery- *
* backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), *
* and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in *
* the Control/Status register). *
* *
* Whether used with the DS3232 or DS3231, the user is responsible for *
* ensuring reads and writes do not exceed the device's address space *
* (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking *
* is done by this library. *
* *
* Jack Christensen 06Mar2013 *
* 28Aug2013 Changed the lower level methods to return the status of *
* the I2C communication with the RTC. Thanks to *
* Rob Tillaart for the suggestion. (Fixes issue #1.) *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 444 Castro Street, Suite 900, *
* Mountain View, CA 94041. *
*----------------------------------------------------------------------*/
#ifndef DS3232RTC_h
#define DS3232RTC_h
#include <Time.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
//DS3232 I2C Address
#define RTC_ADDR 0x68
//DS3232 Register Addresses
#define RTC_SECONDS 0x00
#define RTC_MINUTES 0x01
#define RTC_HOURS 0x02
#define RTC_DAY 0x03
#define RTC_DATE 0x04
#define RTC_MONTH 0x05
#define RTC_YEAR 0x06
#define ALM1_SECONDS 0x07
#define ALM1_MINUTES 0x08
#define ALM1_HOURS 0x09
#define ALM1_DAYDATE 0x0A
#define ALM2_MINUTES 0x0B
#define ALM2_HOURS 0x0C
#define ALM2_DAYDATE 0x0D
#define RTC_CONTROL 0x0E
#define RTC_STATUS 0x0F
#define RTC_AGING 0x10
#define TEMP_MSB 0x11
#define TEMP_LSB 0x12
#define SRAM_START_ADDR 0x14 //first SRAM address
#define SRAM_SIZE 236 //number of bytes of SRAM
//Alarm mask bits
#define A1M1 7
#define A1M2 7
#define A1M3 7
#define A1M4 7
#define A2M2 7
#define A2M3 7
#define A2M4 7
//Control register bits
#define EOSC 7
#define BBSQW 6
#define CONV 5
#define RS2 4
#define RS1 3
#define INTCN 2
#define A2IE 1
#define A1IE 0
//Status register bits
#define OSF 7
#define BB32KHZ 6
#define CRATE1 5
#define CRATE0 4
#define EN32KHZ 3
#define BSY 2
#define A2F 1
#define A1F 0
//Square-wave output frequency (TS2, RS1 bits)
enum SQWAVE_FREQS_t {SQWAVE_1_HZ, SQWAVE_1024_HZ, SQWAVE_4096_HZ, SQWAVE_8192_HZ, SQWAVE_NONE};
//Alarm masks
enum ALARM_TYPES_t {
ALM1_EVERY_SECOND = 0x0F,
ALM1_MATCH_SECONDS = 0x0E,
ALM1_MATCH_MINUTES = 0x0C, //match minutes *and* seconds
ALM1_MATCH_HOURS = 0x08, //match hours *and* minutes, seconds
ALM1_MATCH_DATE = 0x00, //match date *and* hours, minutes, seconds
ALM1_MATCH_DAY = 0x10, //match day *and* hours, minutes, seconds
ALM2_EVERY_MINUTE = 0x8E,
ALM2_MATCH_MINUTES = 0x8C, //match minutes
ALM2_MATCH_HOURS = 0x88, //match hours *and* minutes
ALM2_MATCH_DATE = 0x80, //match date *and* hours, minutes
ALM2_MATCH_DAY = 0x90, //match day *and* hours, minutes
};
#define ALARM_1 1 //constants for calling functions
#define ALARM_2 2
//Other
#define DS1307_CH 7 //for DS1307 compatibility, Clock Halt bit in Seconds register
#define HR1224 6 //Hours register 12 or 24 hour mode (24 hour mode==0)
#define CENTURY 7 //Century bit in Month register
#define DYDT 6 //Day/Date flag bit in alarm Day/Date registers
class DS3232RTC
{
public:
DS3232RTC();
static time_t get(void);
static byte set(time_t t);
static byte read(tmElements_t &tm);
static byte write(tmElements_t &tm);
byte writeRTC(byte addr, byte *values, byte nBytes);
byte writeRTC(byte addr, byte value);
byte readRTC(byte addr, byte *values, byte nBytes);
byte readRTC(byte addr);
void setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate);
void setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate);
void alarmInterrupt(byte alarmNumber, boolean alarmEnabled);
boolean alarm(byte alarmNumber);
void squareWave(SQWAVE_FREQS_t freq);
boolean oscStopped(void);
int temperature(void);
private:
static uint8_t dec2bcd(uint8_t n);
static uint8_t bcd2dec(uint8_t n);
};
extern DS3232RTC RTC;
#endif

View File

@@ -0,0 +1,8 @@
# Arduino DS3232RTC Library v1.0 #
https://github.com/JChristensen/DS3232RTC
LICENSE file
Jack Christensen Mar 2013
![CC BY-SA](http://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-sa.png)
## CC BY-SA ##
"Arduino DS3232RTC Library" by Jack Christensen is licensed under [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/).

View File

@@ -0,0 +1,345 @@
# Arduino DS3232RTC Library v1.0 #
https://github.com/JChristensen/DS3232RTC
ReadMe file
Jack Christensen Mar 2013
![CC BY-SA](http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-sa.png)
## Introduction ##
**DS3232RTC** is an Arduino library that supports the Maxim Integrated DS3232 and DS3231 Real-Time Clocks. This library is intended to be used with the [Arduino Time library](http://www.arduino.cc/playground/Code/Time).
The **DS3232RTC** library is a drop-in replacement for the DS1307RTC.h library by Michael Margolis that is supplied with the Arduino Time library above. To change from using a DS1307 RTC to an DS323x RTC, it is only necessary to use `#include <DS3232RTC.h>` instead of `#include <DS1307RTC.h>`.
**DS3232RTC** also implements functions to support the additional features of the DS3232 and DS3231. The DS3231 has the same features as the DS3232 except: (1) Battery-backed SRAM, (2) Battery-backed 32kHz output (BB32kHz bit in Control/Status register 0x0F), and (3) Adjustable temperature sensor sample rate (CRATE1:0 bits in the Control/Status register).
"Arduino DS3232RTC Library" by Jack Christensen is licensed under CC BY-SA 4.0.
## Installation ##
To use the **DS3232RTC** library:
- Go to https://github.com/JChristensen/DS3232RTC, click the **Download ZIP** button and save the ZIP file to a convenient location on your PC.
- Uncompress the downloaded file. This will result in a folder containing all the files for the library, that has a name that includes the branch name, usually **DS3232RTC-master**.
- Rename the folder to just **DS3232RTC**.
- Copy the renamed folder to the Arduino sketchbook\libraries folder.
## Examples ##
The following example sketches are included with the **DS3232RTC** library:
- **SetSerial:** Set the RTC's date and time from the Arduino serial monitor. Displays date, time and temperature.
- **TimeRTC:** Same as the example of the same name provided with the **Time** library, demonstrating the interchangeability of the **DS3232RTC** library with the **DS1307RTC** library.
- **tiny3232_KnockBang:** Demonstrates interfacing an ATtiny45/85 to a DS3231 or DS3232 RTC.
## Usage notes ##
When using the **DS3232RTC** library, the user is responsible for ensuring that reads and writes do not exceed the device's address space (0x00-0x12 for DS3231, 0x00-0xFF for DS3232); no bounds checking is done by the library.
Similar to the **DS1307RTC** library, the **DS3232RTC** library instantiates an RTC object; the user does not need to do this.
To use the **DS3232RTC** library, the Time and Wire libraries must also be included. For brevity, these includes are not repeated in the examples below:
```c++
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
```
## Enumerations ##
### SQWAVE_FREQS_t
##### Description
Symbolic names used with the squareWave() method (described below).
##### Values
- SQWAVE_NONE
- SQWAVE_1_HZ
- SQWAVE_1024_HZ
- SQWAVE_4096_HZ
- SQWAVE_8192_HZ
### ALARM_TYPES_t
##### Description
Symbolic names used with the setAlarm() method (described below).
##### Values for Alarm 1
- ALM1_EVERY_SECOND -- causes an alarm once per second.
- ALM1_MATCH_SECONDS -- causes an alarm when the seconds match (i.e. once per minute).
- ALM1_MATCH_MINUTES -- causes an alarm when the minutes *and* seconds match.
- ALM1_MATCH_HOURS -- causes an alarm when the hours *and* minutes *and* seconds match.
- ALM1_MATCH_DATE -- causes an alarm when the date of the month *and* hours *and* minutes *and* seconds match.
- ALM1_MATCH_DAY -- causes an alarm when the day of the week *and* hours *and* minutes *and* seconds match.
##### Values for Alarm 2
- ALM2_EVERY_MINUTE -- causes an alarm once per minute.
- ALM2_MATCH_MINUTES -- causes an alarm when the minutes match (i.e. once per hour).
- ALM2_MATCH_HOURS -- causes an alarm when the hours *and* minutes match.
- ALM2_MATCH_DATE -- causes an alarm when the date of the month *and* hours *and* minutes match.
- ALM2_MATCH_DAY -- causes an alarm when the day of the week *and* hours *and* minutes match.
## Methods for setting and reading the time ##
###get(void)
#####Description
Reads the current date and time from the RTC and returns it as a *time_t* value. Returns zero if an I2C error occurs (RTC not present, etc.).
#####Syntax
`RTC.get();`
#####Parameters
None.
#####Returns
Current date and time *(time_t)*
#####Example
```c++
time_t myTime;
myTime = RTC.get();
```
###set(time_t t)
#####Description
Sets the RTC date and time to the given *time_t* value.
#####Syntax
`RTC.set(t);`
#####Parameters
**t:** The date and time to set the RTC to *(time_t)*
#####Returns
I2C status *(byte)*. Returns zero if successful.
#####Example
```c++
//this example first sets the system time (maintained by the Time library) to
//a hard-coded date and time, and then sets the RTC from the system time.
//the setTime() function is part of the Time library.
setTime(23, 31, 30, 13, 2, 2009); //set the system time to 23h31m30s on 13Feb2009
RTC.set(now()); //set the RTC from the system time
```
###read(tmElements_t &tm)
#####Description
Reads the current date and time from the RTC and returns it as a *tmElements_t* structure. See the [Arduino Time library](http://www.arduino.cc/playground/Code/Time) for details on the *tmElements_t* structure.
#####Syntax
`RTC.read(tm);`
#####Parameters
**tm:** Address of a *tmElements_t* structure to which the date and time are returned.
#####Returns
I2C status *(byte)*. Returns zero if successful. The date and time read from the RTC are returned to the **tm** parameter.
#####Example
```c++
tmElements_t tm;
RTC.read(tm);
Serial.print(tm.Hour, DEC);
Serial.print(':');
Serial.print(tm.Minute,DEC);
Serial.print(':');
Serial.println(tm.Second,DEC);
```
###write(tmElements_t &tm)
#####Description
Sets the RTC to the date and time given by a *tmElements_t* structure.
#####Syntax
`RTC.write(tm);`
#####Parameters
**tm:** Address of a *tmElements_t* structure used to set the date and time.
#####Returns
I2C status *(byte)*. Returns zero if successful.
#####Example
```c++
tmElements_t tm;
tm.Hour = 23; //set the tm structure to 23h31m30s on 13Feb2009
tm.Minute = 31;
tm.Minute = 30;
tm.Day = 13;
tm.Month = 2;
tm.Year = 2009 - 1970; //tmElements_t.Year is the offset from 1970
RTC.write(tm); //set the RTC from the tm structure
```
## Methods for reading and writing RTC registers or static RAM (SRAM) for the DS3232 ##
The DS3232RTC.h file defines symbolic names for the timekeeping, alarm, status and control registers. These can be used for the addr argument in the functions below.
###writeRTC(byte addr, byte *values, byte nBytes)
#####Description
Write one or more bytes to RTC memory.
#####Syntax
`RTC.writeRTC(addr, values, nbytes);`
#####Parameters
**addr:** First SRAM address to write *(byte)*. The valid address range is 0x00-0x12 for DS3231, 0x00-0xFF for DS3232. The general-purpose SRAM for the DS3232 begins at address 0x14. Address is not checked for validity by the library.
**values:** An array of values to write _(*byte)_
**nBytes:** Number of bytes to write *(byte)*. Must be between 1 and 31 (Wire library limitation) but is not checked by the library.
#####Returns
I2C status *(byte)*. Returns zero if successful.
#####Example
```c++
//write 1, 2, ..., 8 to the first eight DS3232 SRAM locations
byte buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
RTC.sramWrite(0x14, buf, 8);
```
###writeRTC(byte addr, byte value)
#####Description
Write a single byte to RTC memory.
#####Syntax
`RTC.writeRTC(addr, value);`
#####Parameters
**addr:** SRAM address to write *(byte)*. The valid address range is 0x00-0x12 for DS3231, 0x00-0xFF for DS3232. The general-purpose SRAM for the DS3232 begins at address 0x14. Address is not checked for validity by the library.
**value:** Value to write _(byte)_
#####Returns
I2C status *(byte)*. Returns zero if successful.
#####Example
```c++
RTC.writeRTC(3, 14); //write the value 14 to SRAM address 3
```
###readRTC(byte addr, byte *values, byte nBytes)
#####Description
Read one or more bytes from RTC RAM.
#####Syntax
`RTC.readRTC(addr, values, nbytes);`
#####Parameters
**addr:** First SRAM address to read *(byte)*. The valid address range is 0x00-0x12 for DS3231, 0x00-0xFF for DS3232. The general-purpose SRAM for the DS3232 begins at address 0x14. Address is not checked for validity by the library.
**values:** An array to receive the values read _(*byte)_
**nBytes:** Number of bytes to read *(byte)*. Must be between 1 and 32 (Wire library limitation) but is not checked by the library.
#####Returns
I2C status *(byte)*. Returns zero if successful.
#####Example
```c++
//read the last eight locations of SRAM into buf
byte buf[8];
RTC.sramRead(248, buf, 8);
```
###readRTC(byte addr)
#####Description
Reads a single byte from RTC RAM.
#####Syntax
`RTC.readRTC(addr);`
#####Parameters
**addr:** SRAM address to read *(byte)*. The valid address range is 0x00-0x12 for DS3231, 0x00-0xFF for DS3232. The general-purpose SRAM for the DS3232 begins at address 0x14. Address is not checked for validity by the library.
#####Returns
Value read from the RTC *(byte)*
#####Example
```c++
byte val;
val = RTC.readRTC(3); //read the value from SRAM location 3
```
## Alarm methods ##
The DS3232 and DS3231 have two alarms. Alarm1 can be set to seconds precision; Alarm2 can only be set to minutes precision.
###setAlarm(ALARM_TYPES_t alarmType, byte seconds, byte minutes, byte hours, byte daydate)
#####Description
Set an alarm time. Sets the alarm registers only. To cause the INT pin to be asserted on alarm match, use alarmInterrupt(). This method can set either Alarm 1 or Alarm 2, depending on the value of alarmType (use the ALARM_TYPES_t enumeration above). When setting Alarm 2, the seconds value must be supplied but is ignored, recommend using zero. (Alarm 2 has no seconds register.)
#####Syntax
`RTC.setAlarm(alarmType, seconds, minutes, hours, dayOrDate);`
#####Parameters
**alarmType:** A value from the ALARM_TYPES_t enumeration, above. *(ALARM_TYPES_t)*
**seconds:** The seconds value to set the alarm to. *(byte)*
**minutes:** The minutes value to set the alarm to. *(byte)*
**hours:** The hours value to set the alarm to. *(byte)*
**dayOrDate:** The day of the week or the date of the month. For day of the week, use a value from the Time library timeDayOfWeek_t enumeration, i.e. dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday. *(byte)*
#####Returns
None.
#####Example
```c++
//Set Alarm1 for 12:34:56 on Sunday
RTC.setAlarm(ALM1_MATCH_DAY, 56, 34, 12, dowSunday);
```
###setAlarm(ALARM_TYPES_t alarmType, byte minutes, byte hours, byte daydate)
#####Description
Set an alarm time. Sets the alarm registers only. To cause the INT pin to be asserted on alarm match, use alarmInterrupt(). This method can set either Alarm 1 or Alarm 2, depending on the value of alarmType (use the ALARM_TYPES_t enumeration above). However, when using this method to set Alarm 1, the seconds value is set to zero. (Alarm 2 has no seconds register.)
#####Syntax
`RTC.setAlarm(alarmType, minutes, hours, dayOrDate);`
#####Parameters
**alarmType:** A value from the ALARM_TYPES_t enumeration, above. *(ALARM_TYPES_t)*
**minutes:** The minutes value to set the alarm to. *(byte)*
**hours:** The hours value to set the alarm to. *(byte)*
**dayOrDate:** The day of the week or the date of the month. For day of the week, use a value from the Time library timeDayOfWeek_t enumeration, i.e. dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday. *(byte)*
#####Returns
None.
#####Example
```c++
//Set Alarm2 for 12:34 on the 4th day of the month
RTC.setAlarm(ALM1_MATCH_DATE, 34, 12, 4);
```
###alarmInterrupt(byte alarmNumber, boolean alarmEnabled)
#####Description
Enable or disable an alarm "interrupt". Note that this "interrupt" causes the RTC's INT pin to be asserted. To use this signal as an actual interrupt to a microcontroller, it will need to be connected properly and programmed in the application firmware.
on the RTC.
#####Syntax
`RTC.alarmInterrupt(alarmNumber, enable);`
#####Parameters
**alarmNumber:** The number of the alarm to enable or disable, ALARM_1 or ALARM_2 *(byte)*
**alarmEnabled:** true or false *(boolean)*
#####Returns
None.
#####Example
```c++
RTC.alarmInterrupt(ALARM_1, true); //assert the INT pin when Alarm1 occurs.
RTC.alarmInterrupt(ALARM_2, false); //disable Alarm2
```
###alarm(byte alarmNumber)
#####Description
Tests whether an alarm has been triggered. If the alarm was triggered, returns true and resets the alarm flag in the RTC, else returns false.
#####Syntax
`RTC.alarm(alarmNumber);`
#####Parameters
**alarmNumber:** The number of the alarm to test, ALARM_1 or ALARM_2 *(byte)*
#####Returns
Description *(type)*
#####Example
```c++
if ( RTC.alarm(ALARM_1) ) { //has Alarm1 triggered?
//yes, act on the alarm
}
else {
//no alarm
}
```
## Other methods ##
###temperature(void)
#####Description
Returns the RTC temperature.
#####Syntax
`RTC.temperature();`
#####Parameters
None.
#####Returns
RTC temperature as degrees Celsius times four. *(int)*
#####Example
```c++
int t = RTC.temperature();
float celsius = t / 4.0;
float fahrenheit = celsius * 9.0 / 5.0 + 32.0;
```
###squareWave(SQWAVE_FREQS_t freq)
#####Description
Enables or disables the square wave output.
#####Syntax
`RTC.squareWave(freq);`
#####Parameters
**freq:** a value from the SQWAVE_FREQS_t enumeration above. *(SQWAVE_FREQS_t)*
#####Returns
None.
#####Example
```c++
RTC.squareWave(SQWAVE_1_HZ); //1 Hz square wave
RTC.squareWave(SQWAVE_NONE); //no square wave
```
###oscStopped(void)
#####Description
Check whether the RTC oscillator is or was stopped. This may indicate that the RTC's time is not accurate.
#####Syntax
`RTC.oscStopped();`
#####Parameters
None.
#####Returns
True or false *(boolean)*
#####Example
```c++
if ( RTC.oscStopped() ) { //check the oscillator
//may be trouble
}
else {
//all is well
}
```

View File

@@ -0,0 +1,132 @@
/*----------------------------------------------------------------------*
* Display the date and time from a DS3231 or DS3232 RTC every second. *
* Display the temperature once per minute. (The DS3231 does a *
* temperature conversion once every 64 seconds. This is also the *
* default for the DS3232.) *
* *
* Set the date and time by entering the following on the Arduino *
* serial monitor: *
* year,month,day,hour,minute,second, *
* *
* Where *
* year can be two or four digits, *
* month is 1-12, *
* day is 1-31, *
* hour is 0-23, and *
* minute and second are 0-59. *
* *
* Entering the final comma delimiter (after "second") will avoid a *
* one-second timeout and will allow the RTC to be set more accurately. *
* *
* No validity checking is done, invalid values or incomplete syntax *
* in the input will result in an incorrect RTC setting. *
* *
* Jack Christensen 08Aug2013 *
* *
* Tested with Arduino 1.0.5, Arduino Uno, DS3231/Chronodot, DS3232. *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
void setup(void)
{
Serial.begin(115200);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
}
void loop(void)
{
static time_t tLast;
time_t t;
tmElements_t tm;
//check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12) {
//note that the tmElements_t Year member is an offset from 1970,
//but the RTC wants the last two digits of the calendar year.
//use the convenience macros from Time.h to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!") << endl;
else {
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else //(y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
RTC.set(t); //use the time_t value to ensure correct weekday is set
setTime(t);
Serial << F("RTC set to: ");
printDateTime(t);
Serial << endl;
//dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
if (second(t) == 0) {
float c = RTC.temperature() / 4.;
float f = c * 9. / 5. + 32.;
Serial << F(" ") << c << F(" C ") << f << F(" F");
}
Serial << endl;
}
}
//print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
Serial << ' ';
printTime(t);
}
//print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
//print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}

View File

@@ -0,0 +1,50 @@
/*
* TimeRTC.pde
* Example code illustrating Time library with Real Time Clock.
* This example is identical to the example provided with the Time Library,
* only the #include statement has been changed to include the DS3232RTC library.
*/
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
void setup(void)
{
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop(void)
{
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(void)
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(' ');
Serial.print(day());
Serial.print(' ');
Serial.print(month());
Serial.print(' ');
Serial.print(year());
Serial.println();
}
void printDigits(int digits)
{
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(':');
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

View File

@@ -0,0 +1,88 @@
/*----------------------------------------------------------------------*
* Digital clock display using a DS3231/32 Real-Time Clock *
* and an ATtiny45/85 with a 1MHz system clock. *
* Also seems to work with a DS1307 which is fairly similar but the *
* DS3232RTC library doesn't officially support it. *
* *
* Tested with Arduino 1.0.5. Also Arduino-Tiny Core, TinyISP, and *
* TinyDebugKnockBang from http://code.google.com/p/arduino-tiny/ *
* *
* Run TinyISP on an ATmega microcontroller that does not have an LED *
* connected to pin 13 (SCK). The LED causes problems because the SPI *
* pins are also the I2C pins on the ATtiny. Connect MISO, MOSI, SCK *
* on the ATmega to the corresponding pins on the ATtiny through 220Ω *
* resistors for safety. Use 4.7K pullup resistors on the ATtiny *
* I2C bus. *
* *
* Jack Christensen 21Aug2013 *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <TinyDebugKnockBang.h> //http://code.google.com/p/arduino-tiny/
#include <TinyWireM.h> //http://playground.arduino.cc/Code/USIi2c
void setup(void)
{
Debug.begin(250000);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Debug.print(F("RTC Sync"));
if (timeStatus() != timeSet) Debug.print(F(" FAIL!"));
Debug.println();
}
void loop(void)
{
static time_t tLast;
time_t t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
Debug.println();
}
}
//print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
Debug.print(' ');
printTime(t);
}
//print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
//print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Debug.print(monthShortStr(month(t)));
Debug.print(year(t), DEC);
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Debug.print('0');
Debug.print(val, DEC);;
if (delim > 0) Debug.print(delim);
return;
}

View File

@@ -0,0 +1,13 @@
DS3232RTC KEYWORD1
get KEYWORD2
set KEYWORD2
read KEYWORD2
write KEYWORD2
writeRTC KEYWORD2
readRTC KEYWORD2
setAlarm KEYWORD2
alarmInterrupt KEYWORD2
alarm KEYWORD2
squareWave KEYWORD2
oscStopped KEYWORD2
temperature KEYWORD2