Giter Club home page Giter Club logo

ds323x's Introduction

DS323x

Arduino library for DS3231/DS3232 Extremely Accurate I2C-Integrated RTC/TCXO/Crystal

Usage

Time

#include <DS323x.h>
DS323x rtc;

void setup()
{
    Wire.begin();
    rtc.attach(Wire);
    rtc.now(DateTime(2020, 11, 23, 14, 23, 45));
}

void loop()
{
    DateTime now = rtc.now();
    Serial.println(now.timestamp());
    delay(1000);
}

Square Wave

#include <DS323x.h>
DS323x rtc;

void setup()
{
    Wire.begin();
    rtc.attach(Wire);
    rtc.squareWaveFrequency(DS323x::SquareWaveFreq::SQWF_1_HZ);
    rtc.interruptControl(DS323x::InterruptCtrl::SQW); // default is ALRAM
    rtc.trigger(); // first falling edge comes 1sec after this (same as second(0))
}

void loop()
{
}

Alarm

#include <DS323x.h>
DS323x rtc;

void setup()
{
    Wire.begin();
    rtc.attach(Wire);

    // set alarm
    rtc.second(DS323x::AlarmSel::A1, 56);
    rtc.rate(DS323x::A1Rate::MATCH_SECOND);

    // alarm flags must be cleard to get next alarm
    if (rtc.hasAlarmed(DS323x::AlarmSel::A1))
        rtc.clearAlarm(DS323x::AlarmSel::A1);

    // enable alarm
    rtc.interruptControl(DS323x::InterruptCtrl::ALARM);
    rtc.enableAlarm1(true);
}

void loop()
{
}

Configurations

enum class Format : uint8_t { HOUR_24, HOUR_12 };
enum class AMPM : uint8_t { AMPM_AM, AMPM_PM };
enum class DYDT : uint8_t { DYDT_DATE, DYDT_DAY };
enum class A1Rate : uint8_t
{
    EVERY_SECOND,
    MATCH_SECOND,
    MATCH_SECOND_MINUTE,
    MATCH_SECOND_MINUTE_HOUR,
    MATCH_SECOND_MINUTE_HOUR_DATE,
    MATCH_SECOND_MINUTE_HOUR_DAY
};
enum class A2Rate : uint8_t
{
    EVERY_MINUTE,
    MATCH_MINUTE,
    MATCH_MINUTE_HOUR,
    MATCH_MINUTE_HOUR_DATE,
    MATCH_MINUTE_HOUR_DAY
};
enum class SquareWaveFreq : uint8_t
{
    SQWF_1_HZ,
    SQWF_1024_HZ,
    SQWF_4096_HZ,
    SQWF_8192_HZ
};
enum class InterruptCtrl : uint8_t { SQW, ALARM };
enum class AlarmSel : uint8_t { A1, A2 };

APIs

void attach(WireType& w);

// rtc getters
DateTime now() const;
uint8_t second();
uint8_t minute();
uint8_t hour();
uint8_t weekday();
uint8_t day();
uint8_t month();
uint8_t year();
AMPM ampm();
Format format();

// rtc setters
void now(const DateTime& n);
bool second(const uint8_t s);
bool minute(const uint8_t m);
bool hour(const uint8_t h);
bool weekday(const uint8_t w);
bool day(const uint8_t d);
bool month(const uint8_t m);
bool year(const uint8_t y);
bool ampm(const AMPM m);
bool format(const Format f);

// alarm getters
const DateTime& alarm(const AlarmSel a);
uint8_t second(const AlarmSel a);
uint8_t minute(const AlarmSel a);
uint8_t hour(const AlarmSel a);
uint8_t weekday(const AlarmSel a);
uint8_t day(const AlarmSel a);
AMPM ampm(const AlarmSel a);
Format format(const AlarmSel a);
DYDT dydt(const AlarmSel a);
bool a1m1();
bool a1m2();
bool a1m3();
bool a1m4();
A1Rate rateA1();
bool a2m2();
bool a2m3();
bool a2m4();
A2Rate rateA2();

// alarm setters
void alarm(const AlarmSel a, const DateTime& n);
bool second(const AlarmSel a, const uint8_t s);
bool minute(const AlarmSel a, const uint8_t m);
bool hour(const AlarmSel a, const uint8_t h);
bool weekday(const AlarmSel a, const uint8_t w);
bool day(const AlarmSel a, const uint8_t d);
bool ampm(const AlarmSel a, const AMPM m);
bool format(const AlarmSel a, const Format f);
bool dydt(const AlarmSel a, const DYDT d);
bool a1m1(const bool b);
bool a1m2(const bool b);
bool a1m3(const bool b);
bool a1m4(const bool b);
bool rate(const A1Rate a);
bool a2m2(const bool b);
bool a2m3(const bool b);
bool a2m4(const bool b);
bool rate(const A2Rate a);

// Control Registers

// enable oscillator if RTC is powered only by battery power
bool enableOscillator();
bool enableOscillator(const bool b);
// enable square wave even if Vcc < Vpf
bool enableBatteryBackedSquareWave();
bool enableBatteryBackedSquareWave(const bool b);
// enable force temperature sensor to convert the temperature into digital code
bool convertTemperature();
bool convertTemperature(const bool b);
// square wave frequency
SquareWaveFreq squareWaveFrequency();
bool squareWaveFrequency(const SquareWaveFreq f);
// trigger timing of square wave
bool trigger();
// interrupt control
InterruptCtrl interruptControl();
bool interruptControl(const InterruptCtrl i);
// enable alarms
bool enableAlarm1();
bool enableAlarm2();
bool enableAlarm1(const bool b);
bool enableAlarm2(const bool b);

// Status Register

// getters
bool oscillatorStopFlag();
bool enable32kHz();
int8_t agingOffset();
bool busy();
bool hasAlarmed(const AlarmSel a);
float temperature();

// setters
bool oscillatorStopFlag(const bool b);
bool enable32kHz(const bool b);
bool agingOffset(const int8_t o);
bool clearAlarm(const AlarmSel a);

License

MIT

ds323x's People

Contributors

hideakitai avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

ball5656

ds323x's Issues

Issue with reading time after 19:59:59

when I set the time to 19:59:50 in 24 hours format, it works fine until 19:59:59, after that the clock starts to show 00:00:00 and then with time it increases seconds, so technically I am not able to get the time beyon 19:59:59 it resets as if it was 23:59:59

Problem Compiling 'time' example

I have two problems compiling the example 'time' program.

  1. the macro PM is defined in Arduino.h (somewhere). I can work around it by #undef prior to including the header file
  2. There is some sort of assignment error when using DataTime now,
    In file included from C:\Users\00011114\AppData\Local\Temp\arduino_modified_sketch_540583\time.ino:2:0:
    C:\Users\00011114\Documents\Arduino\libraries\DS323x/DS323x.h: In instantiation of 'int8_t arduino::ds323x::DS323x_::readBytes(arduino::ds323x::DS323x_::Reg, uint8_t, const uint8_t*, const uint8_t*) [with WireType = TwoWire; int8_t = signed char; uint8_t = unsigned char]':
    C:\Users\00011114\Documents\Arduino\libraries\DS323x/DS323x.h:472:22: required from 'uint8_t arduino::ds323x::DS323x_::readByte(arduino::ds323x::DS323x_::Reg, uint8_t) [with WireType = TwoWire; uint8_t = unsigned char]'
    C:\Users\00011114\Documents\Arduino\libraries\DS323x/DS323x.h:115:54: required from 'uint8_t arduino::ds323x::DS323x_::month() [with WireType = TwoWire; uint8_t = unsigned char]'
    C:\Users\00011114\Documents\Arduino\libraries\DS323x/DS323x.h:108:55: required from 'DateTime arduino::ds323x::DS323x_::now() [with WireType = TwoWire]'
    C:\Users\00011114\AppData\Local\Temp\arduino_modified_sketch_540583\time.ino:18:28: required from here
    C:\Users\00011114\Documents\Arduino\libraries\DS323x/DS323x.h:510:29: error: assignment of read-only location '*(data + ((sizetype)count))'
    data[count] = wire->read() & mask[count];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.