Arduino Forum

Topics => Education and Teaching => Topic started by: Anindya14 on Dec 23, 2017, 04:51 pm

Title: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 23, 2017, 04:51 pm
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? 

Please Help me :)
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: AWOL on Dec 23, 2017, 05:39 pm
It isn't possible to manipulate a register manually; you'll have to write code to do it,  or use a debugger.
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 24, 2017, 03:41 pm
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
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: CrossRoads on Dec 24, 2017, 03:52 pm
That just needs some simple Wire.h commands to read the register, increment the value, and write the register.

Google DS1307 Arduino and you will find tons of links, such as
https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit (https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit)
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: GolamMostafa on Dec 25, 2017, 09:44 am
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.

Good luck! We wish to see your full codes in your next post of this thread.
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 25, 2017, 05:46 pm
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.

Good luck! We wish to see your full codes in your next post of this thread.
Is the code alright? I very much appreciate your help! Thank you so much!
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: AWOL on Dec 25, 2017, 05:51 pm
Quote
Is the code alright?
Does it do reliably and repeatedly what you want it to do?
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 25, 2017, 06:40 pm
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.
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: AWOL on Dec 25, 2017, 07:00 pm
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.
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: GolamMostafa on Dec 26, 2017, 05:58 am
@Anindya14

Quote
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.
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 26, 2017, 04:40 pm
@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 <Wire.h>

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()
{

}
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: GolamMostafa on Dec 26, 2017, 05:25 pm
You could make your codes good looking by using code tags(</>) from Tool bar.

Code: [Select]
#include <Wire.h>

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?
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: Anindya14 on Dec 27, 2017, 02:22 am
You could make your codes good looking by using code tags(</>) from Tool bar.

Code: [Select]
#include <Wire.h>

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?
Code: [Select]

#include <Wire.h>

void setup()
{
   Wire.beginTransmission(0b1101000); //device address is 1101000
   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.beginTransmission(0b1101000); //device address is 1101000
   Wire.write(0x00);
   Wire.write(bcdSec);
   Wire.endTransmission();
   
}

void loop()
{

}


is it alright now?
Title: Re: Manual increment of SEC register of DS-1307 RTC
Post by: GolamMostafa on Dec 27, 2017, 09:50 am
Please test the program using UNO/Serial Monitor/LCD Monitor/7seg Monitor.