Skip to content

Write and read from Registers#16

Open
AlexAyoub545 wants to merge 1 commit into
Treboada:masterfrom
AlexAyoub545:master
Open

Write and read from Registers#16
AlexAyoub545 wants to merge 1 commit into
Treboada:masterfrom
AlexAyoub545:master

Conversation

@AlexAyoub545
Copy link
Copy Markdown

Added code to write and read from registers from either faulty modules or modules that aren't properly updating. Used on Mega 2560 and ESP32-32X and both worked.

Code I used below for the ESP32

#include <Arduino.h>
#include <Ds1302.h>

#define PIN_ENA 26  
#define PIN_CLK 14  
#define PIN_DAT 27   

Ds1302 rtc(PIN_ENA, PIN_CLK, PIN_DAT);

uint8_t bcdToDec(uint8_t bcd) {
  return ((bcd >> 4) * 10) + (bcd & 0x0F);
}
uint8_t decToBcd(uint8_t dec) {
  return ((dec / 10 * 16) + (dec % 10));
}

void setup() {
  Serial.begin(9600);
  rtc.init();

  rtc.writeRegister(0x8E, 0x00);                 // clear write-protect
  rtc.writeRegister(0x80, decToBcd(0) & 0x7F);   // seconds (bit7=0 ensures oscillator runs)
  rtc.writeRegister(0x82, decToBcd(24));         // minutes
  rtc.writeRegister(0x84, decToBcd(16));         // hours
  rtc.writeRegister(0x86, decToBcd(27));         // day of month
  rtc.writeRegister(0x88, decToBcd(5));          // month
  rtc.writeRegister(0x8A, decToBcd(3));          // day of week 
  rtc.writeRegister(0x8C, decToBcd(26));         // year (00–99)

  Serial.println("RTC registers written directly.");
}

void loop() {
  // Show current time
  uint8_t sec = bcdToDec(rtc.readRegister(0x81));
  uint8_t min = bcdToDec(rtc.readRegister(0x83));
  uint8_t hr  = bcdToDec(rtc.readRegister(0x85));
  uint8_t day = bcdToDec(rtc.readRegister(0x87));
  uint8_t mon = bcdToDec(rtc.readRegister(0x89));
  uint8_t yr  = bcdToDec(rtc.readRegister(0x8D));

  Serial.printf("%02d/%02d/%04d %02d:%02d:%02d\n",
                day, mon, 2000 + yr, hr, min, sec);

  delay(1000);
}```

@rafacouto
Copy link
Copy Markdown
Collaborator

Could you please @AlexAyoub545 explain the detail of changes you are introducing and why are removing the comment headers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants