# Manual increment of SEC register of DS-1307 RTC

I have an assignment to submit on 27th December. The problem is ,

I have to manually increment the SEC register of a DS-1307 RTC IC. Assume that present condition is 19(bcd). I want to increment it by 1 to get 20. And write it back to the SEC register. How to do it?

It isn't possible to manipulate a register manually; you'll have to write code to do it, or use a debugger.

AWOL: It isn't possible to manipulate a register manually; you'll have to write code to do it, or use a debugger.

I know and i am asking for the code

That just needs some simple Wire.h commands to read the register, increment the value, and write the register.

As suggested by @CrossRoads, you can use standard TWI Bus Commands (Wire.beginTransmission(deviceAddress);, Wire.write(arg);, Wire.endTransmission();, Wire.requestFrom(deviceAddress, n);, Wire.read();) of the Wire.h Library to read/write the BCD formatted content of the SEC Register of DS1307 RTC. Add 01 (another BCD number) with the content of SEC Register and write it back into SEC Register. (Be sure you are writing BCD formatted value of 20 into SEC Register.) Few hints for you:

1. Check that DS1307 is present and set the address of SEC Register. 2. Read the content of SEC Register. (You have said that it is 19=00011001.) 3. Add 01 with 19; the MCU will produce 1A (0001 1010) and not 0010 0000 (20 BCD). 4. Adjust the incorrect BCD of Step-3 into correct BCD and write it into SEC Register. (If you don't know how to do it, you may take the following alternative steps.)

3. Convert the BCD content of SEC Register into natural binary (normal decimal) just by adding the positional weights of the BCD digits. 4. Add 0x01 with the value of Step-3. 5. Convert the value of Step-4 into BCD value using % (modulus) and / (division) operators. 6. Write the value of Step-5 into SEC Register.

GolamMostafa:
As suggested by @CrossRoads, you can use standard TWI Bus Commands (Wire.beginTransmission(deviceAddress);, Wire.write(arg);, Wire.endTransmission();, Wire.requestFrom(deviceAddress, n);, Wire.read() of the Wire.h Library to read/write the BCD formatted content of the SEC Register of DS1307 RTC. Add 01 (another BCD number) with the content of SEC Register and write it back into SEC Register. (Be sure you are writing BCD formatted value of 20 into SEC Register.) Few hints for you:

1. Check that DS1307 is present and set the address of SEC Register.
2. Read the content of SEC Register. (You have said that it is 19=00011001.)
3. Add 01 with 19; the MCU will produce 1A (0001 1010) and not 0010 0000 (20 BCD).
4. Adjust the incorrect BCD of Step-3 into correct BCD and write it into SEC Register. (If you don’t know how to do it, you may take the following alternative steps.)

3. Convert the BCD content of SEC Register into natural binary (normal decimal) just by adding the positional weights of the BCD digits.
4. Add 0x01 with the value of Step-3.
5. Convert the value of Step-4 into BCD value using % (modulus) and / (division) operators.
6. Write the value of Step-5 into SEC Register.

Is the code alright? I very much appreciate your help! Thank you so much!

Ds1307.ino (684 Bytes)

Is the code alright?

Does it do reliably and repeatedly what you want it to do?

AWOL: Does it do reliably and repeatedly what you want it to do?

It does. But i want to know if the code is efficient or not and any suggestions for better efficiency.

Anindya14: It does. But i want to know if the code is efficient or not and any suggestions for better efficiency.

I don't know; I can't see your code.

@Anindya14

I don't know; I can't see your code.

In respect of the above quote of @AWOL and also mine, please post your program in this window using code tags () in order to allow us seeing your codes online rather than downloading and opening.

GolamMostafa: @Anindya14

In respect of the above quote of @AWOL and also mine, please post your program in this window using code tags () in order to allow us seeing your codes online rather than downloading and opening.

Here it is:

# include

void setup() { Wire.beginTransmission(0b1101000); //device address is 1101000 Wire.write(0x00); Wire.endTransmission();

byte bcdSec=Wire.read(); //reading the SEC register of DS-1307. Assume the value is 19(bcd)(00011001) byte binSec=(((bcdSec>>4)*10)+(bcdSec & 0b00001111)); // Converting the bcd value to binary value binSec++; //incrementing the binary value by 1 byte bcddigit_zero=(binSec%10); byte bcddigit_one =(binSec/10); bcdSec= (bcddigit_zero + bcddigit_one);

Wire.beginTransmission(0b1101000); //device address is 1101000 Wire.write(0x00); Wire.write(bcdSec); Wire.endTransmission();

}

void loop() {

}

You could make your codes good looking by using code tags() from Tool bar.

``````#include

void setup()
{
Wire.write(0x00);
Wire.endTransmission();

byte bcdSec=Wire.read(); //reading the SEC register of DS-1307. Assume the value is 19(bcd)(00011001)
byte binSec=(((bcdSec>>4)*10)+(bcdSec & 0b00001111)); // Converting the bcd value to binary value
binSec++; //incrementing the binary value by 1
byte bcddigit_zero=(binSec%10);
byte bcddigit_one =(binSec/10);
bcdSec= (bcddigit_zero + bcddigit_one);

Wire.write(0x00);
Wire.write(bcdSec);
Wire.endTransmission();

}

void loop()
{

}
``````

You have claimed that your codes are working! How is it possible when your program is missing 'the complete coding' of Step-2 of Post#4?

GolamMostafa: You could make your codes good looking by using code tags() from Tool bar.

``````#include
``````

void setup() {   Wire.beginTransmission(0b1101000); //device address is 1101000   Wire.write(0x00);   Wire.endTransmission();

byte bcdSec=Wire.read(); //reading the SEC register of DS-1307. Assume the value is 19(bcd)(00011001)   byte binSec=(((bcdSec>>4)*10)+(bcdSec & 0b00001111)); // Converting the bcd value to binary value   binSec++; //incrementing the binary value by 1   byte bcddigit_zero=(binSec%10);   byte bcddigit_one =(binSec/10);   bcdSec= (bcddigit_zero + bcddigit_one);

Wire.beginTransmission(0b1101000); //device address is 1101000   Wire.write(0x00);   Wire.write(bcdSec);   Wire.endTransmission();   }

void loop() {

}

``````

You have claimed that your codes are working! How is it possible when your program is missing 'the complete coding' of Step-2 of Post#4?
``````
``````#include

void setup()
{
Wire.write(0x00);
Wire.endTransmission();

Wire.requestFrom(0b1101000,1);
byte bcdSec=Wire.read(); //reading the SEC register of DS-1307. Assume the value is 19(bcd)(00011001)
byte binSec=(((bcdSec>>4)*10)+(bcdSec & 0b00001111)); // Converting the bcd value to binary value
binSec++; //incrementing the binary value by 1
byte bcddigit_zero=(binSec%10);
byte bcddigit_one =(binSec/10);
bcdSec= (bcddigit_zero + bcddigit_one);

Wire.write(0x00);
Wire.write(bcdSec);
Wire.endTransmission();

}

void loop()
{

}
``````

is it alright now?

Please test the program using UNO/Serial Monitor/LCD Monitor/7seg Monitor.