Setting RTC in a flash with Flowcode and E-blocks

Introduction

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.

Equipment Required

Hardware:

Files:

Equipment

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:

Start

TransmitByte(208)

TransmitByte(1st Register address)

TransmitByte(value 1st register )

TransmitByte(value 2nd register )

TransmitByte(value 3rd register ) etc.

Stop

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):

RTC Set Registers

To Read RTC:

Start

TransmitByte(208)

TransmitByte(1st Register address)

TransmitByte(value 1st register )

Restart

TransmitByte(209)

RecieveByte(value 1st register ) (Last Byte = 0)

RecieveByte(value 2nd register ) (Last Byte = 0)

RecieveByte(value final register )  (Last Byte = 1)

Stop

Values retrieved have to be converted from BCD to decimal:

Decmial_Value = (BCD_Value >> 4) * 10 + (BCD_Value  & 15)

With Flowcode (Click image):

RTC Read Registers

If you are interested in what the data & clock signals look like:

RTC I2C waveform

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:

hhmmssddMMyy
eg
Q235459100815
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.

Mine is:

Port Settings

When you select correct COM Port (app will remember for next time) then after selecting Open Com Port you should see something like:

Before time set

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
Timekeeping Functions
• 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:

HyperTerminal RTC

How we can use VT52 within Flowcode is simple.

The format is:

SendChar(27)

SendString”YRC”

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):

Y52 Formatting

The time starts at row 6 &  column 8

You can see from the above illustration that row 6 = % and  column 8  = ‘

So you use:

SendChar(27)

SendString”Y%'”

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

SendString”Y%””

Instead of  just  a speech mark you must use a backward slash first.

E.g.

SendString”Y%\”

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:

YT52 clear screen

 

 

 

LCD should show the same time as TimeConfig window:

LCD RTC Not Set

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.

Or

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.

LCD:

LCD RTC Set

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:

Flowcode DOTW calculation

The month code is:

Monthcode

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:

DOTW

Since this is only with the DS1307 injector added, on hardware the correct Day will be shown:

LCD Correct Day

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:

mscomm32.ocx erorr

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.

For example:

Fix mscomm32.ocx error

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11,718 total views, 2 views today