DS1302, need spam ch 0 to work, expected as bug

hi, simply, i need to spam
``
byte sec = RTCread(0x81);
RTCwrite(0x80, sec & 0x7F);

to make sure my DS1302 working, if i not spam it in loop, my ds1030 did not work. i expect this as bug, here is my code

``
#include <Arduino.h>

int RTCrstPin = 2;int RTCclkPin = 4;int RTCdatPin = 3;

void RTCwrite(byte address, byte data) {
    address |= 0x01;
    Serial.println("writing with address: " + String(address, BIN) + " And Value: " + String(data, BIN));
    digitalWrite(RTCrstPin, HIGH);
    pinMode(RTCdatPin, OUTPUT);
    for (int i = 0; i < 8; i++) {
      digitalWrite(RTCdatPin, address & 0x01);
      digitalWrite(RTCclkPin, HIGH);
      delayMicroseconds(5);
      if(i < 7) digitalWrite(RTCclkPin, LOW);
      address >>= 1;
      delayMicroseconds(5);
    }
    digitalWrite(RTCclkPin, LOW);
    for (int i = 0; i < 8; i++) {
      digitalWrite(RTCdatPin, data & 0x01);
      digitalWrite(RTCclkPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(RTCclkPin, LOW);
      data >>= 1;
      delayMicroseconds(5);
    }
    delayMicroseconds(5);
    digitalWrite(RTCrstPin, LOW);
}
byte RTCread(byte address) {
  byte result = 0;
  address |= 0x81;
  pinMode(RTCdatPin, OUTPUT);
  digitalWrite(RTCrstPin, HIGH);
  for (int i = 0; i < 8; i++) {
    digitalWrite(RTCdatPin, address & 0x01);
    digitalWrite(RTCclkPin, HIGH);
    delayMicroseconds(5);
    if(i < 7) digitalWrite(RTCclkPin, LOW);
    address >>= 1;
    delayMicroseconds(5);
  }
  digitalWrite(RTCclkPin, HIGH);
  pinMode(RTCdatPin, INPUT);
  for (int i = 0; i < 8; i++) {
    digitalWrite(RTCclkPin, LOW);
    if(digitalRead(RTCdatPin) == HIGH) result |= (0x1 << i);
    digitalWrite(RTCclkPin, HIGH);
  }
  digitalWrite(RTCrstPin, LOW);
  return result;
}

void setup() {
  pinMode(RTCrstPin, OUTPUT);pinMode(RTCclkPin, OUTPUT);pinMode(RTCdatPin, OUTPUT);
  Serial.begin(250000);

  RTCwrite(0x8E, 0x00);
  RTCwrite(0x80, 0x45 & 0x7F);
  
  delay(100); 
  RTCread(0x81); 
}

void loop() {
  byte sec = RTCread(0x81); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  RTCwrite(0x80, sec & 0x7F); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  Serial.println(sec, HEX);
  delay(1000);
}

i think this as a bug and wanna asking why is that happening? if that is how this work sorry for make useless topic, i wanna ask, is this a bug or not?.

Wrong Category, please move, or ask moderator to move

Not sure what, if any, question you’re really asking. Why is what happening? What might the nature be of the bug your referring to?

Does the program compile? If not, what error do you get? If it does, what is Serial Monitor showing?

The two lines you’ve added marking to aren’t exceptional. I don’t know the particular module you’re using.

sorry im new here, i dont know what category to.

the bug is i need to RTCwrite() to RTCread() its should just need RTCread(), and when RTCread() should not return nil if i do it without RTCwrite() before

i did not get any error, i get something unexpected as i expected.

i am using DS1302 and what exceptional you need.

Sorry. Slow down, try complete sentences, it really helps.

If I read you correctly, you’re wondering why you need to do a write before a read. I don’t know that device, but it’s likely the write sets up access to a particular register, and when you then read, you’re reading the correct register. But to confirm that, you’ll likely need to actually read the datasheet for that particular RTC device.

1 Like

It appears you are not using one of the 5 or 6 libraries for this RTC. After a very quick read of the datasheet, it appears there are some bits to be set for two-way communication. It does NOT appear to be normal I2C though.
My brief reading suggests if you are not going to use a library then a deeper understanding of the datasheets may be needed.

If what you don't understand is the need to write to a control register to set up the read, then you really should use a library like 'Rtc by Makuna'

If you don't want to use a library, then studying an existing library might help you understand how the device works.

Your topic does not indicate a problem with the Arduino CLI and hence has been moved to a more suitable location on the forum.

yeah thats the point, i learning the RTC without library, i know its weird, but i just wanna learning flip flop.

Ok, but so far you have not communicated exactly what the problem is. You need to be specific as in the datasheet says this but I am finding that does not work. Provide proof.

Remember, since you are NOT using any library, you must communicate at the bit level and do it effectively. Most people do not realize that superior software skills are built on superior communication skills.