DS3231 и ARDUINO без библиотек.

Пример использования DS3231 в среде ARDUINO без использования библиотек.

Пример кода использования DS3231 в среде ARDUINO. Занимает гораздо меньше чем библиотека и не тормозит!

#include <Wire.h>
#define DS3231_ADDRESS 0x68
hr = 22;  //22:10
m = 10
Wire.begin();

// функции с часами =======================================================================================================
byte decToBcd(byte val) {
  // Convert normal decimal numbers to binary coded decimal
  return ( (val / 10 * 16) + (val % 10) );
}

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



void setMinute(byte min1) {

  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x01); //stop Oscillator
  Wire.write(decToBcd(min1));
  Wire.endTransmission();
}

void setHour(byte hour1) {
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x02); //stop Oscillator
  Wire.write(decToBcd(hour1));
  Wire.endTransmission();
}

void setDay(byte day1) {
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x03); //stop Oscillator
  Wire.write(decToBcd(day1));
  Wire.endTransmission();
}

void setDate(byte date1) {
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x04); //stop Oscillator
  Wire.write(decToBcd(date1));
  Wire.endTransmission();
}

void setMonth(byte month1) {
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x05); //stop Oscillator
  Wire.write(decToBcd(month1));
  Wire.endTransmission();
}

void setYear(byte year1) {
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0x06); //stop Oscillator
  Wire.write(decToBcd(year1));
  Wire.endTransmission();
}

void READ_TIME_RTC() {
  Wire.beginTransmission(DS3231_ADDRESS);        //104 is DS3231 device address
  Wire.write(0x00);                                  //Start at register 0
  Wire.endTransmission();
  Wire.requestFrom(DS3231_ADDRESS, 7);           //Request seven bytes
  if (Wire.available()) {
    s = Wire.read();                           //Get second
    m = Wire.read();                           //Get minute
    h   = Wire.read();                           //Get hour
    day     = Wire.read();
    date    = Wire.read();
    month   = Wire.read();                           //Get month
    year    = Wire.read();

    s = (((s & B11110000) >> 4) * 10 + (s & B00001111)); //Convert BCD to decimal
    m = (((m & B11110000) >> 4) * 10 + (m & B00001111));
    h   = (((h & B00110000) >> 4) * 10 + (h & B00001111));   //Convert BCD to decimal (assume 24 hour mode)
    day     = (day & B00000111); // 1-7 (ВС ПН ВТ СР ЧТ ПТ СУБ)
    date    = (((date & B00110000) >> 4) * 10 + (date & B00001111));     //Convert BCD to decimal  1-31
    month   = (((month & B00010000) >> 4) * 10 + (month & B00001111));   //msb7 is century overflow
    year    = (((year & B11110000) >> 4) * 10 + (year & B00001111));
   
  }
}
//
// ========================================================================================================================
// ========================================================================================================================
void loop() {
//........
  READ_TIME_RTC(); / Запрос времени в основном цикле
//........

//........
setHour(hr); // запись часов
setMinute(m); // запись минут

}

Добавить комментарий

Ваш адрес email не будет опубликован.