ESP8266 - Время DS3231 (DS1307?) - чтение, установка

abdurino

New member
Подключение к пинам SCL и SDA я так понял произвольное и его можно менять.
По умолчанию
VCC - 3.3.V
GND - GND
SCL - 4
SDA - 5
На 2м тестовый лед, чтобы понимать что оно вообще работает )

Очень замечательно что после компиляции Arduino IDE сразу запускает программу и не надо дёргать джампер.
И при открытии com порта ESP не ресетится, так что сразу можно давать команды
show time
set time

Удачи.
Код:
#include <Wire.h>

//#define DS1307_CTRL_ID 0x68
int clockAddress = 0x68;  // This is the I2C address
byte second, minute, hour, dayOfWeek, day, dayOfMonth, month, year;


#define LCDPin 2

#define DS1307_SCL 4
#define DS1307_SDA 5



void setup() {
  // put your setup code here, to run once:
  //wire.begin(SCL, SDA)
  Wire.begin(DS1307_SCL, DS1307_SDA);
  //Wire.begin();
  Serial.begin(9600);
  pinMode(LCDPin, OUTPUT);


}

void loop() {
  digitalWrite(LCDPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(LCDPin, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second


  if (Serial.available() > 0) {
    String str = Serial.readStringUntil('\n');
    if (str == "show time") {
      getDateDs1307(); // update time variables
      String s = getTimeStr();
      Serial.println(s);
    }
    if (str == "set time") {
      time_set();
    }

  }

}



// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
  // Reset the register pointer
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.endTransmission();

  Wire.requestFrom(clockAddress, 7);

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

  // Need to change this if 12 hour am/pm
  hour       = bcdToDec(Wire.read() & 0x3f);
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  day = dayOfMonth;
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());
}
void setDateDs1307()            
{
  // Use of (byte) type casting and ascii math to achieve result.
  /*second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));*/

  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

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

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


String getTimeStr() {
  String str = String(day) + "." + formatDigit(month, 2) + "." + formatDigit(year, 2) + " " + formatDigit(hour, 2) + ":" + formatDigit(minute, 2) + ":" + formatDigit(second, 2);
  return str;
}
String formatDigit(int i, int len) {
  String s = String(i);
  while (s.length() < len) {
    s = "0" + s;
  }
  return (s);
}



void time_set() {
  Serial.println("Enter Year 2 last digits");
  while (Serial.available() <= 0);
  year = Serial.parseInt();
  Serial.println(year);

  Serial.println("Month");
  while (Serial.available() <= 0);
  month = Serial.parseInt();
  Serial.println(month);

  Serial.println("Day");
  while (Serial.available() <= 0);
  day = Serial.parseInt();
  dayOfMonth = day;
  Serial.println(day);

  Serial.println("Day of Week");
  while (Serial.available() <= 0);
  dayOfWeek = Serial.parseInt();
  Serial.println(dayOfWeek);

  Serial.println("Hour");
  while (Serial.available() <= 0);
  hour = Serial.parseInt();
  Serial.println(hour);

  Serial.println("Minute");
  while (Serial.available() <= 0);
  minute = Serial.parseInt();
  Serial.println(minute);

  setDateDs1307();

  Serial.println("Time recvd OK");
}
 
Последнее редактирование:

Mоnk

New member
А что в этом коде надо добавить, чтобы температуру из DS3231 получить и показать?
 

Mоnk

New member
Никому не интересно, похоже...
Отвечу сам себе.
Код:
#include <Wire.h>
#define DS3231_SCL 4
#define DS3231_SDA 5

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, temperature;

void setup() {
  Wire.begin(DS3231_SCL, DS3231_SDA);
  Serial.begin(115200);
}
void loop() {
 
  if (Serial.available() > 0) {
    String str = Serial.readStringUntil('\n');
    if (str == "get") {
      getDateDS3231(); // update time variables
      String s = getTimeStr();     
      Serial.println("-----------show time-----------");
      Serial.println(s);
    }
    if (str == "set") {
      time_set();
    }
  }
}
void getDateDS3231() {
  Wire.beginTransmission(0x68);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7);
  second     = bcdToDec(Wire.read() & 0x7f);
  minute     = bcdToDec(Wire.read());
  hour       = bcdToDec(Wire.read() & 0x3f);
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());

//--------------------------------------------
    Wire.beginTransmission(0x68);
    Wire.write(0x11);
    Wire.endTransmission();
    Wire.requestFrom(0x68, 2);
    temperature = (Wire.read() & B01111111) + ( (Wire.read() >> 6) * 0.25 );
}
void setDateDS3231()           
{
  // Use of (byte) type casting and ascii math to achieve result.
  /*second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));*/
  Wire.beginTransmission(0x68);
  Wire.write(byte(0x00));
  Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
String getTimeStr() {
  String str = String(dayOfMonth) + "." + formatDigit(month, 2) + "." + formatDigit(year, 2) + " " + dayOfWeek + " " + formatDigit(hour, 2) + ":" + formatDigit(minute, 2) + ":" + formatDigit(second, 2) + " temperature " + temperature;
  return str;
}
String formatDigit(int i, int len) {
  String s = String(i);
  while (s.length() < len) {
    s = "0" + s;
  }
  return (s);
}
void time_set() {
  Serial.println("Enter Year 2 last digits");
  while (Serial.available() <= 0);
  year = Serial.parseInt();
  Serial.println(year);
  Serial.println("Month");
  while (Serial.available() <= 0);
  month = Serial.parseInt();
  Serial.println(month);
  Serial.println("dayOfMonth");
  while (Serial.available() <= 0);
  dayOfMonth = Serial.parseInt();
  Serial.println(dayOfMonth);
  Serial.println("Day of Week");
  while (Serial.available() <= 0);
  dayOfWeek = Serial.parseInt();
  Serial.println(dayOfWeek);
  Serial.println("Hour");
  while (Serial.available() <= 0);
  hour = Serial.parseInt();
  Serial.println(hour);
  Serial.println("Minute");
  while (Serial.available() <= 0);
  minute = Serial.parseInt();
  Serial.println(minute);
  Serial.println("Second");
  while (Serial.available() <= 0);
  second = Serial.parseInt();
  Serial.println(second);
 
  setDateDS3231();
  Serial.println("Time recvd OK");
}
 
Сверху Снизу