Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to switch between 12/24 hour format RTC DS1307

Status
Not open for further replies.

asking

Full Member level 5
Full Member level 5
Joined
Sep 21, 2010
Messages
279
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Visit site
Activity points
3,377
Hello
I have data available as 24 hour format from ds1307 but I need the same in 12 hour without using if condition and then deduct 12 hour. I tried to read its pdf but how to make high just 6th bit? I tried much but no success. I please an anyone help me. I I need to put 1 switch for 12/24 conversion.. Plz help
 

Did you read the datasheet

The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12-hour or
24-hour mode-select bit
. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with
logic high being PM. In the 24-hour mode, bit 5 is the second 10-hour bit (20 to 23 hours). The hours value must be
re-entered whenever the 12/24-hour mode bit is changed.
 

Hello
I have data available as 24 hour format from ds1307 but I need the same in 12 hour without using if condition and then deduct 12 hour. I tried to read its pdf but how to make high just 6th bit? I tried much but no success. I please an anyone help me. I I need to put 1 switch for 12/24 conversion.. Plz help

you shoud read datasheet of Ds1307, page 8, section " CLOCK AND CALENDAR".
- If you chose bit 6 is high , 12 h is selected
- If you chose bit 6 is low, 24 h is selected
 

There is no way to write just bit 6 , you have to write the time too, if you don't want to change the time then read it and then write it back and set bit6 at the same time.

- - - Updated - - -

If you don't intend to keep the 12/24H setting permanently and you are only doing it to convert the time then you should do this in your code instead of changing the RTC chip format
 
  • Like
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating
0x12 for bcd hours is equal to 10010

AM/PM bit is 5th bit of hour reg. when bit 5 is 1 then it is PM and when bit 5 is 0 it is AM


bit 6 of hour reg is 12/24 hour mode. If 6th bit is 1 then it is 12 hr mode, if 6th bit is 0 then it is 24 hr mode.

So, 01010010 = 12 AM
01110010 = 12 PM
 
  • Like
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating
Code:
hour = BCD2Binary(hour);
                    hour = hour + set;
                    if(hour>=24)
                    hour=0;
                    hour = Binary2BCD(hour);

                    write_ds1307(2, hour); //write hour

now where should i put if else condition for 12/24 hour ? do i need to set that bit during time set ? here's my code for time set please tell me where should i put that condition ?

- - - Updated - - -

this is mikroc compiler code.
 

What is set? From where does set get value?

Code:
do {

  set = 0;
     if(PORTC.F2 == 0)
     {
         delay_ms(70);
         if(PORTC.F2 == 0)
         {
             set_count++;
             if(set_count >= 3)
             {
                set_count = 0;
             }
         }
     }
     if(set_count)
     {
        if(PORTC.F3 == 0)
        {

          if(PORTC.F3 == 0)
              set = 1;
         }
          if(set_count && set)
        {
          switch(set_count)
          {
            case 1:
                    hour = BCD2Binary(hour);
                    hour = hour + set;
                    if(hour>=24)
                    hour=0;
                    hour = Binary2BCD(hour);

                    write_ds1307(2, hour); //write hour
                    break;
            case 2:
                     minute = BCD2Binary(minute);
                     minute = minute + set;
                     if(minute >= 60)
                        minute = 0;
                     if(minute < 0)
                        minute = 59;
                     minute = Binary2BCD(minute);
                     write_ds1307(1, minute); //write min
                     break;
          }
        }
     }

- - - Updated - - -

its just simply the button there are 2 buttons one for selection and one for increment of the selection.
 

That is the function call. I need the function definition.

Code:
write_ds1307(unsigned char address,unsigned char w_data)
{
  Soft_I2C_Start(); // issue I2C start signal
  //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
  Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
  Soft_I2C_Write(address); // send byte (address of DS1307 location)
  Soft_I2C_Write(w_data); // send data (data to be written)
  Soft_I2C_Stop(); // issue I2C stop signal
 

bcd 12 is 10010

you write bcd values to rtc registers

12 AM = 01010010 = 52 BCD
12 PM = 01110010 = 72 BCD

In 24 hour mode

24 hr = 100100 = BCD 24

5th bit is not used for AM/PM

6th bit is used and it is 0 for 24 hr mode and 1 for 12 hr mode

you write for 24 hr, 00100100 = BCD 24 to hour reg.

Yes you have to set bit 5 and 6 during setting the time.
 
Last edited:

SO I HAVE TO SELECT EACH DIGIT IN SUCH A WAY TO DESCRIBE 6TH BIT ACCORDINGLY ? how to calculate for 1 to 12 ? and 1 to 24 ? is there any tool or calculator which will give direct values ?
 

Why do you want all the values. Once you write some value to hour register and then run the clock it will automatically increment. Lets say, it is 12 AM now and you set the value for 12 AM in the hour register then when time becomes 1:00 PM the value in the hour register will be automatically updated. You only have to calculate the value for the hour your are setting.
 

Why do you want all the values. Once you write some value to hour register and then run the clock it will automatically increment. Lets say, it is 12 AM now and you set the value for 12 AM in the hour register then when time becomes 1:00 PM the value in the hour register will be automatically updated. You only have to calculate the value for the hour your are setting.

everytime i wont be setting values bro.... i have two buttons configured....which will select hour and increment....it...in that case ? how ?

- - - Updated - - -

ok i got it...you mean to say...if i set time with 12 hour format further clock will be moving automatically in 12 hour format until i change the time again...to 24 hour format ? right ?
 

Please see mikroC PRO PIC example of RTC DS1307. You can get the examples if you install the demo version of mikroC PRO for PIC. You can also get the code from libstock.com Search for RTC DS1307. The code shows how to write data to ds1307, read ds1307.

- - - Updated - - -

Please see mikroC PRO PIC example of RTC DS1307. You can get the examples if you install the demo version of mikroC PRO for PIC. You can also get the code from libstock.com Search for RTC DS1307. The code shows how to write data to ds1307, read ds1307.

24 hr = 100100. In 24 hr mode you dont use 5th bit. 6th bit is used and is 0, so you pass bcd values 0...24. In 12 hr mode you write 5th bit and 6th bit once only. Are you asking how to pass data in this condition?

After doing dec2bcd(12) you get 10010. add 1000000 or 1100000 to it and then write the value to hour.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top