How to set correct time and date into RTC registers ?

Thread Starter

John99407

Joined Jul 12, 2019
35
I have attached document of RTC DS1337 at bottom

How to set correct time and date into RTC registers ?

Set time 01-09-20 (H-M-S)
Set date 13-01-2020 (d-m-y)

Set seconds 01 to registers 0x00(seconds)
Set minutes 09 to registers 0x01(minutes)
Set hours 20 to registers 0x02 (hours)

Set day monday to registers 0x03(days)

Set date 13 to register 0x04 (date)
Set month 1 January to register 0x05 (months)
Set year 2020 to register 0x06 (years)

DS1337 Document

1578859480183.png
 

djsfantasi

Joined Apr 11, 2010
5,849
You didn’t mention the microprocessor. As far as I know, there should be a library that provided functions to do what you want.

At the least, you should be able to use the datasheet to develop your own library.
 

Thread Starter

John99407

Joined Jul 12, 2019
35
You didn’t mention the microprocessor.
I am using P89V51RD2 I think, You did not understand my question. I am not asking about any function or code

I am asking required hex value to set RTC Device

Set time 01-09-20 (H-M-S)
Set date 13-01-2020 (d-m-y)

What would be the hex value of 1 hours ?
What would be the hex value of 9 minutes?
What would be the hex value of 20 seconds ?

What would be the hex value of Date 13 ?
What would be the hex value of month 1 ?
What would be the hex value of year 2020 ?
 

JohnInTX

Joined Jun 26, 2012
3,919
The RTC uses packed BCD numbers so the coding is just those digits in hex:
Hour 1 = 01h
Minutes 20 = 20h
Date 13 = 13h
Year 2020 = 20h only 100 years are encoded: 2021 = 21h, 2022 = 22h etc. A ‘century’ bit in Reg 5 doubles that.

Things like am/pm, 12/24 hours are in extra bits in registers that will change the hex.
For example in 12 hour format 12AM is 12h but for 12 PM you also set bit 5 in register 2 for a value of 32h.
So for each register, determine the hex for the BCD digits then add in the extra control bits as required for each register.

When you read the registers, you’ll have to strip off those bits to get valid BCD back.

Have fun!
 
Last edited:

BobaMosfet

Joined Jul 1, 2009
893
I have attached document of RTC DS1337 at bottom

How to set correct time and date into RTC registers ?

Set time 01-09-20 (H-M-S)
Set date 13-01-2020 (d-m-y)

Set seconds 01 to registers 0x00(seconds)
Set minutes 09 to registers 0x01(minutes)
Set hours 20 to registers 0x02 (hours)

Set day monday to registers 0x03(days)

Set date 13 to register 0x04 (date)
Set month 1 January to register 0x05 (months)
Set year 2020 to register 0x06 (years)

DS1337 Document

View attachment 196762
Pay attention. Look at the table you showed. Each register has 8-bits. Many of the values are BCD, some may not be. Be aware that some of the bits in any given register may be flag-bits. You can't just overwrite them willy-nilly, if they need preserved.
 

djsfantasi

Joined Apr 11, 2010
5,849
I’m sorry for my earlier comments since you weren’t looking for functions or code. Basically, I’ve never attempted to bit manage the interface because it can get confusing. And my thought was why re-invent the wheel when software libraries containing the code to manipulate the RTC, has already been written and tested in detail fbeyond what I could do.

The other posts may be generalized to refer to bit manipulation in a byte (or any other size) value. This is a basic skill required in microprocessor project.

You need to know how to:
  • Set a bit at a specific location in a storage location;
  • Change a bit (and no others) in a storage location;
  • Write a bit within a storage location.

Once you grok this, you need to extend your acknowledge to the following:
  • Set contiguous bits at a specific location in a storage location;
  • Change a set of contiguous bits (and no others) in a storage location;
  • Write a set of contiguwithin a storage location.

And THEN:
  • Set several non-contiguous bits at a specific location in a storage location;
  • Change several non-contiguous bits (and no others) in a storage location;
  • Write several non-contiguous bits within a storage location.
 
Not sure you even need Hex.

Take 59 minutes, for instance.

You have a base address + 01H
there are 5, 10s intervals and 9 one second intervals.

That is really 59h.

Every 4 bits gets converted. e.g.

0000 = 0d = 0h
.
0101 = 5d = 5h
.
1111 = 15d = Fh

For 10's of minutes the high bit of the nibble needs to be 0.

So, let's say you had an unsigned 8 bit number.

You can shift left, or multiply by a power of 2.

You start out as 5, which is 0101 and shift left 4 bits and get 0101 0000

You have 9d or 9h and you leave that alone and you OR it.

0101 0000 16+64d
0000 1001 9=8+1d

0101 1001

basically, to read multiple bits, you AND with a mask and shift.

so,

0101 1001 AND 0101 0000 = 0101 0000

Shift right 4x, 0000 0101 and we get 5.

To check a bit you generally do an AND NOT

e.g. 1001 AND NOT 0001 = 0001

To set bits you do an OR.

Dividing by a power of 2 is like shifting right and multiplying by a power of 2 is shift left.

When you do all of these operations, it's best to operate on unsigned integers otherwise you have the 2's complement stuff to deal with. 16 bit signed has a range of -32768 to +32767 and unsigned from 0-65535. -1 is all bits set to 1. We use two's complement, so that there is not two values for zero.

I think everyone is looking at you funny because you just group binary digits into groups of 4 bits and apply the A-F digits to 10 to15 decimal.

So, unsigned integers
Shifts, (divides or multiplies with power of 2's)
OR's to set bits
AND NOT to clear bits.

Other binary aritmetic concepts are:
2's compliment
Sign extension e.g. make an 8 bit signed to 16 bit signed number.
 
Top