It’s great having a LCD clock very accurately controlled via a Real Time Clock (RTC for short).
However setting the hours, minutes, seconds, day, date, month and year can be a bit time consuming, especially if you have more than one that requires setting!
Another problem with non radio controlled clocks is having changing the time twice a year.
This blog will cover solving both of these issues.
The Flowchart and app posted will allow the RTC to be set to current PC local time (Via RS232) at a single click of a button.
Time displayed will also include BST or GMT indication and will automatically adjust the time twice a year, even if the hardware is not powered up at the time of change.
First we need to understand how we can set and read the time of DS3231/DS1307 I²C RTC
I²C (Inter-Integrated Circuit) is a communication protocol in which RTC uses to communicate with microcontroller.
Setting RTC Time
Format for setting the time is:
TransmitByte(1st Register address)
TransmitByte(value 1st register )
TransmitByte(value 2nd register )
TransmitByte(value 3rd register ) etc.
Value 208 is derived from 7bit address for DS1307/Ds3231 – DS3232 = 104 (0x68)
If bit 0 is 1 = Read mode then value becomes 209 (0xD1)
If bit 0 is 0 = Write mode then value becomes 208 (0xD0)
Value sent has to be in BCD format.
To do that:
BCD = (Decimal_Value/ 10 << 4) + (Decimal_Value MOD 10)
With Flowcode to set all registers with Time, Day, Date & Year (Click image):
To Read RTC:
TransmitByte(1st Register address)
TransmitByte(value 1st register )
RecieveByte(value 1st register ) (Last Byte = 0)
RecieveByte(value 2nd register ) (Last Byte = 0)
RecieveByte(value final register ) (Last Byte = 1)
Values retrieved have to be converted from BCD to decimal:
Decmial_Value = (BCD_Value >> 4) * 10 + (BCD_Value & 15)
With Flowcode (Click image):
If you are interested in what the data & clock signals look like:
So the time, date year etc. can be set within 2 seconds I came up wit the idea of using RS232.
The idea being an ASCII Q is sent which tells the hardware to expect data in the form of:
which is 1154pm and 59 seconds on 10th Aug 2015
A visual basic application was created to read PC time and send straight to hardware updating RTC registers within two seconds.
Since I don’t know a great deal about visual basic, I sent all the information to Ben Rowland who kindly created the QuickProg application.
Link for it is in the Equipment Required section.
Before running app, make sure that drivers are installed for USB232 board and com port is correctly shown in the device manager and make a note of the COM port.
When you select correct COM Port (app will remember for next time) then after selecting Open Com Port you should see something like:
Above is displaying RTC data when powered up if registers have not been set or backup battery not fitted or depleted.
Note: If you have any error messages about mscomm32 then scroll to the bottom of this blog for further help.
Also temperature shown is only available is using DS3231 & DS3232 as its built in to RTC IC
You can use DS1307 although it’s no where near as accurate but temperature will not be correctly displayed.
Difference between DS1307 and DS3231 – DS3232 is with DS1307 an external crystal is required.
Crystals accuracy are temperature dependent so can change with temperature fluctuations.
The DS3231 & Ds3232 are much more accurate since there is an inbuilt crystal and temperature sensor.
compensation is built in for temperature variations which means accuracy is vastly improved.
From the datasheet:
Benefits and Features
•Highly Accurate RTC Completely Manages All
• Real-Time Clock Counts Seconds, Minutes, Hours,
Date of the Month, Month, Day of the Week, and
Year, with Leap-Year Compensation Valid Up to 2100
• Accuracy ±2ppm from 0°C to +40°C
• Accuracy ±3.5ppm from -40°C to +85°C
• Digital Temp Sensor Output: ±3°C Accuracy
So the accuracy is ±2ppm in normal room temperature then accuracy (seconds) can be calculated by:
Accuracy(Seconds) = (total seconds * PPM)/1E6
Seconds per year is about 31.558 million
Therefore accuracy at 2ppm = (31.558E6 * 2 )/1E6
=63 seconds per year!
Going back to the characters displayed on TimeConfig App.
If you wondering how the chars are always shown at the same position rather than normal scrolling , it’s because we are using VT52 format for RS232 ASCII characters.
HyperTerminal is also VT52 compatible, so if you load that instead of TimeConfig then all characters will be displayed the same:
How we can use VT52 within Flowcode is simple.
The format is:
Where Y is always sent.
R = Row
C = Column
First we need to determine the row and column where we want first character to be placed.
Then the following chart (link for excel sheet in Equipment required section)
we can obtain the illustration below (Click on it to enlarge):
The time starts at row 6 & column 8
You can see from the above illustration that row 6 = % and column 8 = ‘
So you use:
Then any text sent will start at the correct row and column.
Note when sending to row or column 3 you will notice a speech mark is used.
You can’t use
Instead of just a speech mark you must use a backward slash first.
There is a list of VT functional commands to use.
They are know as escape commands and such examples can be found on the wiki page
For example I used clear the screen escape command which is Esc J
First the cursor has to be sent to the start of the screen (cursor home) which is Esc H
so in Flowcode I just did:
LCD should show the same time as TimeConfig window:
We have two options to set Time, day & date
1) The manual slow method:
Left click on anywhere within main widow, then read Key & Function.
Press 1 to Set Time etc.
2) The super fast method.
Make sure PC is set to correct time (2 seconds fast is ideal).
Select SendTimeStamp and RTC should be updated with time, day & date of your PC:
All settings are usually updated just fine.
on the odd occasion you may spot that day or year may not have been updated correctly, but clicking on SendTimeStamp again will sort that out.
The Day of the week is only stored as a number from 1 to 7 in the RTC register.
So to set the correct number within the day register (0x03),the flowchart has to calculate correct the day of the week
It does this by using the following formula:
The month code is:
Note: If you remove the -1 and +1 then DayNum = 0 to 6 instead of 1 to 7
There will be one difference when running the Flowcode simulator.
Since the day of the week is user defined, then you can choose any number from 1 to 7 to represent the day.
since datasheet stated an example or 1 representing Sunday that is what I went for.
This means when you run the simulation the wrong day will be shown since the original creator of the DS1307 chose a different number to represent Sunday.
It’s not a bug it’s just choice.
so at the time of writing (Sat 26th Sep 2015) when ran simulation I got:
Since this is only with the DS1307 injector added, on hardware the correct Day will be shown:
Also during BST (British Summer time) only you may notice that Time will be an hour different.
This is because the RTC is always GMT (Greenwich mean time), but the display shows correct local time.
So when LCD shows 1am for example the RTC is really at 12am
Therefore display should show correct time with out having to adjust twice a year.
The downside is during BST the day will not change until 1am on LCD.
mscomm32 error when running TimeConfig
If there is a problem with registration of mscomm32 files, or they are not present then when trying to run TimeConfig app, you will something like:
To fix this issue you will need to either just register mscomm32.ocx.
Or if missing, add then register mscomm32.ocx
There are a load of websites that have instructions and include missing file.
Fix mscomm32.ocx error
8,344 total views, 1 views today