I am useing the DS1302 Real Time Module. I can't find how to set it to 12 hour format. I am using the <DS1302.h> library. Here is my code....I would appreciate it if you can tell me how to fix it.
AM-PM/12-24 MODE
Bit 7 of the hours register is defined as the 12– 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 – 23 hours).
Please note that this library only makes use of the 24-hr format.
I think you have three ways to go.
You can modify the library to use the 12 hr format at the register level.
You can modify your code to test the hour value, and if hour>12 then hour = hour -12 and PM = true
You can modify the library to use the conditional test of #2.
You may find that changing your code is the easier path and will make the code more portable.
If you wish to modify the library as #1, you will have to do two things.
First you will need to set the bit in the hours register has been noted in the previous post. You can use the library function writeRegister() to OR the new bits with the exitising value. Then you will have to modify the code (_decodeH) which reads the hour register to evaluate the 12-24 bit and the am-pm bit and report the hour and am/pm based on that.
You could also modify the library in a less fundamental way (#3), and that is to leave it in the 24 hour mode, and modify the hours section of the getTime() code to add add the conditional test code described.
t.hour = _decodeH(_burstArray[2]);
//add code here to test hour value and subtract 12 if >12 and set pm=true
when I added your code in and up loaded it to my nano i got an error..
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Nano w/ ATmega328"
DS1302_LCD.ino: In function 'void setup()':
DS1302_LCD:19: error: 't' was not declared in this scope
DS1302_LCD:19: error: '_burstArray' was not declared in this scope
DS1302_LCD:19: error: '_decodeH' was not declared in this scope
The code I posted was for modifying the DS1302.h library, not for placement in the sketch.
I think you are better off modifying your sketch code rather than the library.
Take a look at the library example code DS 1302_Serial_Hard. You want to be using the Time data structure t and the methods of that sketch to get access to t.hour. You can then work with an hour value to develop 12 hour format. You want to be working with the hour as a number rather than a character string.
You are making things hard for yourself by using this RTC and library. Why are you using the DS1302 instead of the more common i2c rtcs like the DS 3231 or DS1307. They are better documented and have more examples floating around.
Then use the t.hour & 0x1F. This is to remove the 24 hours and left the 12 hours only. In this way you do not have to do anything to the library. To set the clock for 12 hours you need to set bit 7 of the hour register. Then check bit 5 to see if = 1 then is PM and if bit 5 = 0 is AM. Also you can manually set the reg 3 to 12 hours. If it is 12 PM then you must set the bit 5.
Please correct the 0x3F to 0x1F. What you do it is remove bit 7 and 5. When bit 5 it is set is PM
bit 5 = AM or PM
bit 7 = 12/24
I got it in 12 hour mode now the date it all meat up it is written as 18/10/16 and I would like it to be 10/18/16 I have looked in the library and haven't found how to fix it. It only started doing that after I put it in 12 hour mode.
I got it in 12 hour mode now the date it all meat up it is written as 18/10/16 and I would like it to be 10/18/16 I have looked in the library and haven't found how to fix it. It only started doing that after I put it in 12 hour mode.
There are three ways the library can report the date
day/month/year which the library defines as FORMAT_LITTLEENDIAN
year/month/day which the library defines as FORMAT_BIGENDIAN
month/day/year which the library defines as FORMAT_MIDDLEENDIAN
The default argument for the rtc.getDateStr() function which you use is LITTLEENDIAN which will give you the 18/10/16 you saw. Why you see a / as a divider instead of a . is a mystery, as the default divider appears to be the latter.
Try changing SHORT to LONG which should give 16 and 2016 for the year.
Try the different ENDIAN options to see what they give.
Also try use ':' and '.' as well as '/' to see the possible dividers.
Her is the details on the arguments of the library function get.DateStr()
// function in DS1302.cpp
char *DS1302::getDateStr(uint8_t slformat, uint8_t eformat, char divider)
//showing defaults in DS1302.h
char *getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.');