极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: hebjean

使用DS3231写入示例程序串口显示时间温度都不正常!已经自己解决了!很简单!哈哈

[复制链接]
发表于 2017-6-6 09:05:20 | 显示全部楼层 |阅读模式
本帖最后由 hebjean 于 2017-6-7 12:40 编辑

很简单事!就是我用的MEGA2560自带SCL和SDA口不用接A4A5哈哈哈!太他么操蛋了!

只是时间设置的不太对小小改下就好!感谢各位积极解惑的朋友


请忽略第一张图!传错了!用DS3231时钟写入示例程序后串口显示都不正常弄不明白了!

我用的EGA2560+DS3231

示例代码
#include <DS3231.h>
#include <Wire.h>


DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;


byte year, month, date, DoW, hour, minute, second;


void setup() {
        // Start the I2C interface
        Wire.begin();
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute
        Clock.setHour(11);  //Set the hour
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(31);  //Set the date of the month
        Clock.setMonth(5);  //Set the month of the year
        Clock.setYear(13);  //Set the year (Last two digits of the year)
        // Start the serial interface
        Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();


  temperature=Clock.getTemperature();


  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature);
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);}
发上库文件和CPP文件!高手们给看看!
针脚接线
vcc---5v
GND--GND
SCL--A5
SDA--A4
32K还有SQW没用!
CPP文件如下!

/*
DS3231.cpp: DS3231 Real-Time Clock library
original code by
Eric Ayars
4/1/11

updated to Arduino 1.0
John Hubert
Feb 7, 2012

Released into the public domain.
*/

#include <DS3231.h>

#define CLOCK_ADDRESS 0x68

// Constructor
DS3231:S3231() {
        // nothing to do for this constructor.
}

/*****************************************
        Public Functions
*****************************************/

void DS3231::getTime(byte& year, byte& month, byte& date, byte& DoW, byte& hour, byte& minute, byte& second) {
        byte tempBuffer;
        bool PM;
        bool h12;

        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x00));
        Wire.endTransmission();
       
        Wire.requestFrom(CLOCK_ADDRESS, 7);

        second = bcdToDec(Wire.read());
        minute = bcdToDec(Wire.read());
        tempBuffer = bcdToDec(Wire.read());
        h12 = tempBuffer & 0b01000000;
        if (h12) {
                PM = tempBuffer & 0b00100000;
                hour = bcdToDec(tempBuffer & 0b00011111);
        } else {
                hour = bcdToDec(tempBuffer & 0b00111111);
        }
        DoW = bcdToDec(Wire.read());
        date = bcdToDec(Wire.read());
        month = bcdToDec(Wire.read() & 0b01111111);
        year = bcdToDec(Wire.read());
}

byte DS3231::getSecond() {
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x00));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return bcdToDec(Wire.read());
}

byte DS3231::getMinute() {
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(0x01);
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return bcdToDec(Wire.read());
}

byte DS3231::getHour(bool& h12, bool& PM) {
        byte temp_buffer;
        byte hour;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(0x02);
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        temp_buffer = Wire.read();
        h12 = temp_buffer & 0b01000000;
        if (h12) {
                PM = temp_buffer & 0b00100000;
                hour = bcdToDec(temp_buffer & 0b00011111);
        } else {
                hour = bcdToDec(temp_buffer & 0b00111111);
        }
        return hour;
}

byte DS3231::getDoW() {
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x03));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return bcdToDec(Wire.read());
}

byte DS3231::getDate() {
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x04));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return bcdToDec(Wire.read());
}

byte DS3231::getMonth(bool& Century) {
        byte temp_buffer;
        byte hour;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x05));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        temp_buffer = Wire.read();
        Century = temp_buffer & 0b10000000;
        return (bcdToDec(temp_buffer & 0b01111111)) ;
}

byte DS3231::getYear() {
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x06));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return bcdToDec(Wire.read());
}

void DS3231::setSecond(byte Second) {
        // Sets the seconds
        // This function also resets the Oscillator Stop Flag, which is set
        // whenever power is interrupted.
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x00));
        Wire.write(decToBcd(Second));       
        Wire.endTransmission();
        // Clear OSF flag
        byte temp_buffer = readControlByte(1);
        writeControlByte((temp_buffer & 0b01111111), 1);
}

void DS3231::setMinute(byte Minute) {
        // Sets the minutes
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x01));
        Wire.write(decToBcd(Minute));       
        Wire.endTransmission();
}

void DS3231::setHour(byte Hour) {
        // Sets the hour, without changing 12/24h mode.
        // The hour must be in 24h format.

        bool h12;

        // Start by figuring out what the 12/24 mode is
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x02));
        Wire.endTransmission();
        Wire.requestFrom(CLOCK_ADDRESS, 1);
        h12 = (Wire.read() & 0b01000000);
        // if h12 is true, it's 12h mode; false is 24h.

        if (h12) {
                // 12 hour
                if (Hour > 12) {
                        Hour = decToBcd(Hour-12) | 0b01100000;
                } else {
                        Hour = decToBcd(Hour) & 0b11011111;
                }
        } else {
                // 24 hour
                Hour = decToBcd(Hour) & 0b10111111;
        }

        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x02));
        Wire.write(Hour);
        Wire.endTransmission();
}

void DS3231::setDoW(byte DoW) {
        // Sets the Day of Week
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x03));
        Wire.write(decToBcd(DoW));       
        Wire.endTransmission();
}

void DS3231::setDate(byte Date) {
        // Sets the Date
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x04));
        Wire.write(decToBcd(Date));       
        Wire.endTransmission();
}

void DS3231::setMonth(byte Month) {
        // Sets the month
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x05));
        Wire.write(decToBcd(Month));       
        Wire.endTransmission();
}

void DS3231::setYear(byte Year) {
        // Sets the year
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x06));
        Wire.write(decToBcd(Year));       
        Wire.endTransmission();
}

void DS3231::setClockMode(bool h12) {
        // sets the mode to 12-hour (true) or 24-hour (false).
        // One thing that bothers me about how I've written this is that
        // if the read and right happen at the right hourly millisecnd,
        // the clock will be set back an hour. Not sure how to do it better,
        // though, and as long as one doesn't set the mode frequently it's
        // a very minimal risk.
        // It's zero risk if you call this BEFORE setting the hour, since
        // the setHour() function doesn't change this mode.
       
        byte temp_buffer;

        // Start by reading byte 0x02.
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x02));
        Wire.endTransmission();
        Wire.requestFrom(CLOCK_ADDRESS, 1);
        temp_buffer = Wire.read();

        // Set the flag to the requested value:
        if (h12) {
                temp_buffer = temp_buffer | 0b01000000;
        } else {
                temp_buffer = temp_buffer & 0b10111111;
        }

        // Write the byte
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x02));
        Wire.write(temp_buffer);
        Wire.endTransmission();
}

float DS3231::getTemperature() {
        // Checks the internal thermometer on the DS3231 and returns the
        // temperature as a floating-point value.
        byte temp;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x11));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 2);
        temp = Wire.read();        // Here's the MSB
        return float(temp) + 0.25*(Wire.read()>>6);
}

void DS3231::getA1Time(byte& A1Day, byte& A1Hour, byte& A1Minute, byte& A1Second, byte& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM) {
        byte temp_buffer;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x07));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 4);

        temp_buffer        = Wire.read();        // Get A1M1 and A1 Seconds
        A1Second        = bcdToDec(temp_buffer & 0b01111111);
        // put A1M1 bit in position 0 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>7;

        temp_buffer                = Wire.read();        // Get A1M2 and A1 minutes
        A1Minute        = bcdToDec(temp_buffer & 0b01111111);
        // put A1M2 bit in position 1 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>6;

        temp_buffer        = Wire.read();        // Get A1M3 and A1 Hour
        // put A1M3 bit in position 2 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>5;
        // determine A1 12/24 mode
        A1h12                = temp_buffer & 0b01000000;
        if (A1h12) {
                A1PM        = temp_buffer & 0b00100000;                        // determine am/pm
                A1Hour        = bcdToDec(temp_buffer & 0b00011111);        // 12-hour
        } else {
                A1Hour        = bcdToDec(temp_buffer & 0b00111111);        // 24-hour
        }

        temp_buffer        = Wire.read();        // Get A1M4 and A1 Day/Date
        // put A1M3 bit in position 3 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>4;
        // determine A1 day or date flag
        A1Dy                = (temp_buffer & 0b01000000)>>6;
        if (A1Dy) {
                // alarm is by day of week, not date.
                A1Day        = bcdToDec(temp_buffer & 0b00001111);
        } else {
                // alarm is by date, not day of week.
                A1Day        = bcdToDec(temp_buffer & 0b00111111);
        }
}

void DS3231::getA2Time(byte& A2Day, byte& A2Hour, byte& A2Minute, byte& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM) {
        byte temp_buffer;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x0b));
        Wire.endTransmission();

        Wire.requestFrom(CLOCK_ADDRESS, 3);
        temp_buffer        = Wire.read();        // Get A2M2 and A2 Minutes
        A2Minute        = bcdToDec(temp_buffer & 0b01111111);
        // put A2M2 bit in position 4 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>3;

        temp_buffer        = Wire.read();        // Get A2M3 and A2 Hour
        // put A2M3 bit in position 5 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>2;
        // determine A2 12/24 mode
        A2h12                = temp_buffer & 0b01000000;
        if (A2h12) {
                A2PM        = temp_buffer & 0b00100000;                        // determine am/pm
                A2Hour        = bcdToDec(temp_buffer & 0b00011111);        // 12-hour
        } else {
                A2Hour        = bcdToDec(temp_buffer & 0b00111111);        // 24-hour
        }

        temp_buffer        = Wire.read();        // Get A2M4 and A1 Day/Date
        // put A2M4 bit in position 6 of DS3231_AlarmBits.
        AlarmBits        = AlarmBits | (temp_buffer & 0b10000000)>>1;
        // determine A2 day or date flag
        A2Dy                = (temp_buffer & 0b01000000)>>6;
        if (A2Dy) {
                // alarm is by day of week, not date.
                A2Day        = bcdToDec(temp_buffer & 0b00001111);
        } else {
                // alarm is by date, not day of week.
                A2Day        = bcdToDec(temp_buffer & 0b00111111);
        }
}

void DS3231::setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM) {
        //        Sets the alarm-1 date and time on the DS3231, using A1* information
        byte temp_buffer;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x07));        // A1 starts at 07h
        // Send A1 second and A1M1
        Wire.write(decToBcd(A1Second) | ((AlarmBits & 0b00000001) << 7));
        // Send A1 Minute and A1M2
        Wire.write(decToBcd(A1Minute) | ((AlarmBits & 0b00000010) << 6));
        // Figure out A1 hour
        if (A1h12) {
                // Start by converting existing time to h12 if it was given in 24h.
                if (A1Hour > 12) {
                        // well, then, this obviously isn't a h12 time, is it?
                        A1Hour = A1Hour - 12;
                        A1PM = true;
                }
                if (A1PM) {
                        // Afternoon
                        // Convert the hour to BCD and add appropriate flags.
                        temp_buffer = decToBcd(A1Hour) | 0b01100000;
                } else {
                        // Morning
                        // Convert the hour to BCD and add appropriate flags.
                        temp_buffer = decToBcd(A1Hour) | 0b01000000;
                }
        } else {
                // Now for 24h
                temp_buffer = decToBcd(A1Hour);
        }
        temp_buffer = temp_buffer | ((AlarmBits & 0b00000100)<<5);
        // A1 hour is figured out, send it
        Wire.write(temp_buffer);
        // Figure out A1 day/date and A1M4
        temp_buffer = ((AlarmBits & 0b00001000)<<4) | decToBcd(A1Day);
        if (A1Dy) {
                // Set A1 Day/Date flag (Otherwise it's zero)
                temp_buffer = temp_buffer | 0b01000000;
        }
        Wire.write(temp_buffer);
        // All done!
        Wire.endTransmission();
}

void DS3231::setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM) {
        //        Sets the alarm-2 date and time on the DS3231, using A2* information
        byte temp_buffer;
        Wire.beginTransmission(CLOCK_ADDRESS);
        Wire.write(uint8_t(0x0b));        // A1 starts at 0bh
        // Send A2 Minute and A2M2
        Wire.write(decToBcd(A2Minute) | ((AlarmBits & 0b00010000) << 3));
        // Figure out A2 hour
        if (A2h12) {
                // Start by converting existing time to h12 if it was given in 24h.
                if (A2Hour > 12) {
                        // well, then, this obviously isn't a h12 time, is it?
                        A2Hour = A2Hour - 12;
                        A2PM = true;
                }
                if (A2PM) {
                        // Afternoon
                        // Convert the hour to BCD and add appropriate flags.
                        temp_buffer = decToBcd(A2Hour) | 0b01100000;
                } else {
                        // Morning
                        // Convert the hour to BCD and add appropriate flags.
                        temp_buffer = decToBcd(A2Hour) | 0b01000000;
                }
        } else {
                // Now for 24h
                temp_buffer = decToBcd(A2Hour);
        }
        // add in A2M3 bit
        temp_buffer = temp_buffer | ((AlarmBits & 0b00100000)<<2);
        // A2 hour is figured out, send it
        Wire.write(temp_buffer);
        // Figure out A2 day/date and A2M4
        temp_buffer = ((AlarmBits & 0b01000000)<<1) | decToBcd(A2Day);
        if (A2Dy) {
                // Set A2 Day/Date flag (Otherwise it's zero)
                temp_buffer = temp_buffer | 0b01000000;
        }
        Wire.write(temp_buffer);
        // All done!
        Wire.endTransmission();
}

void DS3231::turnOnAlarm(byte Alarm) {
        // turns on alarm number "Alarm". Defaults to 2 if Alarm is not 1.
        byte temp_buffer = readControlByte(0);
        // modify control byte
        if (Alarm == 1) {
                temp_buffer = temp_buffer | 0b00000101;
        } else {
                temp_buffer = temp_buffer | 0b00000110;
        }
        writeControlByte(temp_buffer, 0);
}

void DS3231::turnOffAlarm(byte Alarm) {
        // turns off alarm number "Alarm". Defaults to 2 if Alarm is not 1.
        // Leaves interrupt pin alone.
        byte temp_buffer = readControlByte(0);
        // modify control byte
        if (Alarm == 1) {
                temp_buffer = temp_buffer & 0b11111110;
        } else {
                temp_buffer = temp_buffer & 0b11111101;
        }
        writeControlByte(temp_buffer, 0);
}

bool DS3231::checkAlarmEnabled(byte Alarm) {
        // Checks whether the given alarm is enabled.
        byte result = 0x0;
        byte temp_buffer = readControlByte(0);
        if (Alarm == 1) {
                result = temp_buffer & 0b00000001;
        } else {
                result = temp_buffer & 0b00000010;
        }
        return result;
}

bool DS3231::checkIfAlarm(byte Alarm) {
        // Checks whether alarm 1 or alarm 2 flag is on, returns T/F accordingly.
        // Turns flag off, also.
        // defaults to checking alarm 2, unless Alarm == 1.
        byte result;
        byte temp_buffer = readControlByte(1);
        if (Alarm == 1) {
                // Did alarm 1 go off?
                result = temp_buffer & 0b00000001;
                // clear flag
                temp_buffer = temp_buffer & 0b11111110;
        } else {
                // Did alarm 2 go off?
                result = temp_buffer & 0b00000010;
                // clear flag
                temp_buffer = temp_buffer & 0b11111101;
        }
        writeControlByte(temp_buffer, 1);
        return result;
}

void DS3231::enableOscillator(bool TF, bool battery, byte frequency) {
        // turns oscillator on or off. True is on, false is off.
        // if battery is true, turns on even for battery-only operation,
        // otherwise turns off if Vcc is off.
        // frequency must be 0, 1, 2, or 3.
        // 0 = 1 Hz
        // 1 = 1.024 kHz
        // 2 = 4.096 kHz
        // 3 = 8.192 kHz (Default if frequency byte is out of range)
        if (frequency > 3) frequency = 3;
        // read control byte in, but zero out current state of RS2 and RS1.
        byte temp_buffer = readControlByte(0) & 0b11100111;
        if (battery) {
                // turn on BBSQW flag
                temp_buffer = temp_buffer | 0b01000000;
        } else {
                // turn off BBSQW flag
                temp_buffer = temp_buffer & 0b10111111;
        }
        if (TF) {
                // set ~EOSC to 0 and INTCN to zero.
                temp_buffer = temp_buffer & 0b01111011;
        } else {
                // set ~EOSC to 1, leave INTCN as is.
                temp_buffer = temp_buffer | 0b10000100;
        }
        // shift frequency into bits 3 and 4 and set.
        frequency = frequency << 3;
        temp_buffer = temp_buffer | frequency;
        // And write the control bits
        writeControlByte(temp_buffer, 0);
}

void DS3231::enable32kHz(bool TF) {
        // turn 32kHz pin on or off
        byte temp_buffer = readControlByte(1);
        if (TF) {
                // turn on 32kHz pin
                temp_buffer = temp_buffer | 0b00001000;
        } else {
                // turn off 32kHz pin
                temp_buffer = temp_buffer & 0b11110111;
        }
        writeControlByte(temp_buffer, 1);
}

bool DS3231:scillatorCheck() {
        // Returns false if the oscillator has been off for some reason.
        // If this is the case, the time is probably not correct.
        byte temp_buffer = readControlByte(1);
        bool result = true;
        if (temp_buffer & 0b10000000) {
                // Oscillator Stop Flag (OSF) is set, so return false.
                result = false;
        }
        return result;
}

/*****************************************
        Private Functions
*****************************************/

byte DS3231::decToBcd(byte val) {
// Convert normal decimal numbers to binary coded decimal
        return ( (val/10*16) + (val%10) );
}

byte DS3231::bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
        return ( (val/16*10) + (val%16) );
}

byte DS3231::readControlByte(bool which) {
        // Read selected control byte
        // first byte (0) is 0x0e, second (1) is 0x0f
        Wire.beginTransmission(CLOCK_ADDRESS);
        if (which) {
                // second control byte
                Wire.write(uint8_t(0x0f));
        } else {
                // first control byte
                Wire.write(uint8_t(0x0e));
        }
        Wire.endTransmission();
        Wire.requestFrom(CLOCK_ADDRESS, 1);
        return Wire.read();       
}

void DS3231::writeControlByte(byte control, bool which) {
        // Write the selected control byte.
        // which=false -> 0x0e, true->0x0f.
        Wire.beginTransmission(CLOCK_ADDRESS);
        if (which) {
                Wire.write(uint8_t(0x0f));
        } else {
                Wire.write(uint8_t(0x0e));
        }
        Wire.write(control);
        Wire.endTransmission();
}
3231库文件如下
/*
* DS3231.h
*
* Arduino Library for the DS3231 Real-Time Clock chip
*
* (c) Eric Ayars
* 4/1/11
* Updated to Arduino 1.0 By john Hubert
* Feb 7 2012
* released into the public domain. If you use this, please let me know
* (just out of pure curiosity!) by sending me an email:
* [email protected]
*
*/

#ifndef DS3231_h
#define DS3231_h

#include <Arduino.h>
#include <Wire.h>

class DS3231 {
        public:
                       
                //Constructor
                DS3231();

                // Time-retrieval functions
       
                // the get*() functions retrieve current values of the registers.
                // If you only need one element, use that one for simplicity; but
                // if you need the whole passel then use getTime() to avoid
                // the chance of rollover between reads of the different components.
                void getTime(byte& year, byte& month, byte& date, byte& DoW, byte& hour, byte& minute, byte& second);
                byte getSecond();
                byte getMinute();
                byte getHour(bool& h12, bool& PM);
                        // In addition to returning the hour register, this function
                        // returns the values of the 12/24-hour flag and the AM/PM flag.
                byte getDoW();
                byte getDate();
                byte getMonth(bool& Century);
                        // Also sets the flag indicating century roll-over.
                byte getYear();
                        // Last 2 digits only

                // Time-setting functions
                // Note that none of these check for sensibility: You can set the
                // date to July 42nd and strange things will probably result.
               
                void setSecond(byte Second);
                        // In addition to setting the seconds, this clears the
                        // "Oscillator Stop Flag".
                void setMinute(byte Minute);
                        // Sets the minute
                void setHour(byte Hour);
                        // Sets the hour
                void setDoW(byte DoW);
                        // Sets the Day of the Week (1-7);
                void setDate(byte Date);
                        // Sets the Date of the Month
                void setMonth(byte Month);
                        // Sets the Month of the year
                void setYear(byte Year);
                        // Last two digits of the year
                void setClockMode(bool h12);
                        // Set 12/24h mode. True is 12-h, false is 24-hour.

                // Temperature function

                float getTemperature();

                // Alarm functions
               
                void getA1Time(byte& A1Day, byte& A1Hour, byte& A1Minute, byte& A1Second, byte& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM);
/* Retrieves everything you could want to know about alarm
* one.
* A1Dy true makes the alarm go on A1Day = Day of Week,
* A1Dy false makes the alarm go on A1Day = Date of month.
*
* byte AlarmBits sets the behavior of the alarms:
*        Dy        A1M4        A1M3        A1M2        A1M1        Rate
*        X        1                1                1                1                Once per second
*        X        1                1                1                0                Alarm when seconds match
*        X        1                1                0                0                Alarm when min, sec match
*        X        1                0                0                0                Alarm when hour, min, sec match
*        0        0                0                0                0                Alarm when date, h, m, s match
*        1        0                0                0                0                Alarm when DoW, h, m, s match
*
*        Dy        A2M4        A2M3        A2M2        Rate
*        X        1                1                1                Once per minute (at seconds = 00)
*        X        1                1                0                Alarm when minutes match
*        X        1                0                0                Alarm when hours and minutes match
*        0        0                0                0                Alarm when date, hour, min match
*        1        0                0                0                Alarm when DoW, hour, min match
*/
                void getA2Time(byte& A2Day, byte& A2Hour, byte& A2Minute, byte& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM);
                        // Same as getA1Time();, but A2 only goes on seconds == 00.
                void setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM);
                        // Set the details for Alarm 1
                void setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM);
                        // Set the details for Alarm 2
                void turnOnAlarm(byte Alarm);
                        // Enables alarm 1 or 2 and the external interrupt pin.
                        // If Alarm != 1, it assumes Alarm == 2.
                void turnOffAlarm(byte Alarm);
                        // Disables alarm 1 or 2 (default is 2 if Alarm != 1);
                        // and leaves the interrupt pin alone.
                bool checkAlarmEnabled(byte Alarm);
                        // Returns T/F to indicate whether the requested alarm is
                        // enabled. Defaults to 2 if Alarm != 1.
                bool checkIfAlarm(byte Alarm);
                        // Checks whether the indicated alarm (1 or 2, 2 default);
                        // has been activated.

                // Oscillator functions

                void enableOscillator(bool TF, bool battery, byte frequency);
                        // turns oscillator on or off. True is on, false is off.
                        // if battery is true, turns on even for battery-only operation,
                        // otherwise turns off if Vcc is off.
                        // frequency must be 0, 1, 2, or 3.
                        // 0 = 1 Hz
                        // 1 = 1.024 kHz
                        // 2 = 4.096 kHz
                        // 3 = 8.192 kHz (Default if frequency byte is out of range);
                void enable32kHz(bool TF);
                        // Turns the 32kHz output pin on (true); or off (false).
                bool oscillatorCheck();
                        // Checks the status of the OSF (Oscillator Stop Flag);.
                        // If this returns false, then the clock is probably not
                        // giving you the correct time.
                        // The OSF is cleared by function setSecond();.

        private:

                byte decToBcd(byte val);
                        // Convert normal decimal numbers to binary coded decimal
                byte bcdToDec(byte val);
                        // Convert binary coded decimal to normal decimal numbers
                byte readControlByte(bool which);
                        // Read selected control byte: (0); reads 0x0e, (1) reads 0x0f
                void writeControlByte(byte control, bool which);
                        // Write the selected control byte.
                        // which == false -> 0x0e, true->0x0f.

};

#endif

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2017-6-6 10:19:52 | 显示全部楼层
从哪整来的库
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-6 10:23:02 | 显示全部楼层

官网,是不是3231.cpp错了?
回复 支持 反对

使用道具 举报

发表于 2017-6-6 18:00:02 | 显示全部楼层
hebjean 发表于 2017-6-6 10:23
官网,是不是3231.cpp错了?

看这架势好像不太匹配
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-6 20:00:49 | 显示全部楼层
zjz5717 发表于 2017-6-6 18:00
看这架势好像不太匹配

那该怎么办?
回复 支持 反对

使用道具 举报

发表于 2017-6-6 20:49:02 | 显示全部楼层

您先把库发一下。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-6 21:08:59 | 显示全部楼层
zjz5717 发表于 2017-6-6 20:49
您先把库发一下。。。

贴到帖子里了。给看看吧!谢谢
回复 支持 反对

使用道具 举报

发表于 2017-6-6 22:50:53 | 显示全部楼层
你贴你的代码啊,图发错了可以重新编辑帖子删掉它。。。
回复 支持 反对

使用道具 举报

发表于 2017-6-6 23:14:31 | 显示全部楼层
看代码头大,直接发一个实测可用的代码,先跑起来看看硬件有没有问题再来说学习理解改进
注意是 IIC的1602


#include <DS3231.h>                           // I2C地址为0x27 CLOCK_ADDRESS 0x68 ,  
#include <Wire.h>
#include <LiquidCrystal_I2C.h>                // 1602 库

DS3231 Clock;
LiquidCrystal_I2C lcd(0x27,16,2);        // 设置LCD1602的I2C地址为0x27,LCD1602为两行,每行16个字符的液晶显示器

bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

int year, month, date, DoW,week , hour, minute, second,temperature;
String comdata = "";
int numdata[7] = {0},  mark = 0;
char dis1[16]={0},dis2[16]={0};





void setup (void)
{  
  lcd.init(); // 给LCD的I2C通讯初始化,需要执行两次
  delay(20);
  lcd.init(); // 给LCD的I2C通讯初始化,需要执行两次
  delay(20);
  lcd.backlight();//点亮LCD背光灯

  Serial.begin(9600);                // 串口显示时间设置方式
  Serial.println("set_time :");
  Serial.println("year month day week hour minute second");
  Serial.println();
  Serial.println("week : 1 -> Sunday; 2 -> Monday; 3 -> Tuesday:  ....7 -> Saturday");
  Serial.println();
  Serial.println("for example :2014-5-20 Tue 0:33:30  ");
  Serial.println("set_time :");
  Serial.println("14 5 20 3 0 33 30");
  Serial.println();
}




void ReadDS3231()          //读取DS3231 参数
{
  Wire.begin();
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12,PM);
  week=Clock.getDoW();   
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  temperature=Clock.getTemperature();
}

void get_dis()          //1602液晶上每一位上显示的数据
{
  ReadDS3231();
  dis1[0]='2';
  dis1[1]='0';
  dis1[2]=0x30+year/10;
  dis1[3]=0x30+year%10;
  dis1[4]='-';
  dis1[5]=0x30+month/10;
  dis1[6]=0x30+month%10;
  dis1[7]='-';
  dis1[8]=0x30+date/10;
  dis1[9]=0x30+date%10;
  dis1[10]=' ';
  dis1[11]=' ';
  dis1[12]=' ';
  switch(week)
  {
        case 2: {
                          dis1[13]='M';
                          dis1[14]='o';
                          dis1[15]='n';
                        }
                        break;
        case 3: {
                          dis1[13]='T';
                          dis1[14]='u';
                          dis1[15]='e';
                        }
                        break;
        case 4: {
                          dis1[13]='W';
                          dis1[14]='e';
                          dis1[15]='d';
                        }
                        break;
        case 5: {
                          dis1[13]='T';
                          dis1[14]='h';
                          dis1[15]='u';
                        }
                        break;
        case 6: {
                          dis1[13]='F';
                          dis1[14]='r';
                          dis1[15]='i';
                        }
                        break;
        case 7: {
                          dis1[13]='S';
                          dis1[14]='a';
                          dis1[15]='t';
                        }
                        break;
        case 1: {
                          dis1[13]='S';
                          dis1[14]='u';
                          dis1[15]='n';
                        }
                        break;
  }
  dis2[0]=' ';
  dis2[1]=0x30+hour/10;
  dis2[2]=0x30+hour%10;
  dis2[3]=':';
  dis2[4]=0x30+minute/10;
  dis2[5]=0x30+minute%10;
  dis2[6]=':';
  dis2[7]=0x30+second/10;
  dis2[8]=0x30+second%10;
  dis2[9]=' ';
  dis2[10]=' ';
  dis2[11]=' ';
  dis2[12]=0x30+temperature/10;
  dis2[13]=0x30+temperature%10;
  dis2[14]='.';
  dis2[15]=0x30+0;
}


void play()                                  //1602显示完整时间
{
       get_dis();
            int k;      
            lcd.clear();
            lcd.setCursor(0, 0);             // 注意列在前,行在后
            for(k=0;k<16;k++)
            {
            lcd.print(dis1[k]);                // Serial.print(dis1[k]);   不接LCD时,串口输出日期,检测DS3231是否工作正常
            }
            lcd.setCursor(0, 1);             // 注意列在前,行在后
            for(k=0;k<16;k++)
            {
            lcd.print(dis2[k]);                // Serial.print(dis2[k]);  不接LCD时,串口输出时间,检测DS3231是否工作正常            
            }
}

void set_time()                               //DS3231设置时间
{
Wire.begin();
Clock.setSecond(numdata[6]);                 //秒
Clock.setMinute(numdata[5]);                 //分
Clock.setHour(numdata[4]);                   //时
Clock.setDoW(numdata[3]);                    //周
Clock.setDate(numdata[2]);                   //日
Clock.setMonth(numdata[1]);                  //月
Clock.setYear(numdata[0]);                   //年
}

void loop (void)
{
  int j = 0;
  while (Serial.available() > 0)            //检测串口是否有数据
  {
    comdata += char(Serial.read());
    delay(2);
    mark = 1;
    play();
  }

  if(mark == 1)  
  {
    Serial.println(comdata);             //串口打印检测到的数据
    for(int i = 0; i < comdata.length() ; i++)
    {
      if(comdata[i] == ' ')
      {
        j++;
      }
      else
      {
        numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
      }
    }
    comdata = String("");
    Serial.print("set_time... ");
    set_time();
    Serial.println(" OK ");
    for(int i = 0; i < 7; i++)
    {
    numdata[i] = 0;
    }
    mark = 0;
  }
    play();
    delay(1000);
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-7 00:01:15 | 显示全部楼层
darkorigin 发表于 2017-6-6 22:50
你贴你的代码啊,图发错了可以重新编辑帖子删掉它。。。

贴上了。给帮忙看看吧。整不明白了。新手!
回复 支持 反对

使用道具 举报

发表于 2017-6-7 00:15:55 | 显示全部楼层
hebjean 发表于 2017-6-7 00:01
贴上了。给帮忙看看吧。整不明白了。新手!

楼上有人给你贴了可用的代码,你试试看
还有就是个人的习惯是
新硬件到手先尝试测试硬件好坏,再考虑项目实施。
也就是拿例程跑一遍,这样能及时分清是硬件问题还是逻辑问题。毕竟单片机和传感器等不能和计算机硬件一样都保证完好,有些硬件本身设计或者制造不规范。。。
我就曾经遇到一个28j60模块总是莫名其妙的掉线,后来用替换法发现是质量不好。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-7 01:10:23 | 显示全部楼层
kpj001 发表于 2017-6-6 23:14
看代码头大,直接发一个实测可用的代码,先跑起来看看硬件有没有问题再来说学习理解改进
注意是 IIC的1602
...

手上没有1602屏只有个USART HMI 屏。
用你的代码跑了!串口显示如下
set_time :
year month day week hour minute second

week : 1 -> Sunday; 2 -> Monday; 3 -> Tuesday:  ....7 -> Saturday

for example :2014-5-20 Tue 0:33:30  
set_time :
14 5 20 3 0 33 30
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-7 09:28:22 | 显示全部楼层
darkorigin 发表于 2017-6-7 00:15
楼上有人给你贴了可用的代码,你试试看
还有就是个人的习惯是
新硬件到手先尝试测试硬件好坏,再考虑项 ...

手上没有1602,只是跑了下程序。串口显示为
set_time :
year month day week hour minute second

week : 1 -> Sunday; 2 -> Monday; 3 -> Tuesday:  ....7 -> Saturday

for example :2014-5-20 Tue 0:33:30  
set_time :
14 5 20 3 0 33 30

不知道正常不?
回复 支持 反对

使用道具 举报

发表于 2017-7-11 20:49:23 | 显示全部楼层
怎么修改时间?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 00:08 , Processed in 0.046010 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表