ARM:0017 – go to sleep clock

This is a “gamers” clock. It sits near the monitor and shows the real time and time computer is on. This is achieved using USB as power supply. Also, the same USB port is used to set the clock. Battery (in this version- super capacitor) is used only to keep the RTC clock on while it is not displaying time.

All components are easily obtainable and are cheap. I think the box for the clock is the most expensive and labor intensive part.

RTC OLED clock bluepill
There are only few components: Bluepill bloard (STM32F103), small OLED screen on I2C bus (optional bus terminators – 10K resistors if missing on screen PCB), supercapacitor (or 3V backup lithium battery) with charging circuit (diode and current limiting resistor). I am using supercap because batteries are dying, meanwhile this capacitor is forever. Supercaps charge is smaller than battery and it keeps RTC for shorter time length, but this is a frequently used clock and it recharges every time the computer is on. Charging batteries is not recommended.

This clock uses a USB COM (CDC) device for communication with the computer and setting the time. To set time, send a time string to the clock and it will be current settings. To do it automagically use windows CMD script, only change port number:

set /p x=%TIME% \\.\COM12

This script is so strange as there are problems in Windows bat files when port numbers are big. This script eliminates possible problems with port naming, also it does not send extra data (like CRLF) to the clock. Clock’s firmware filters data, but there may be some bugs.
If the time is not set, do like with any other “windows” device- remove it and reattach to USB.

Full source code and compiled binaries for the Bluepill are here.

One notable moment in source code is adjusting RTC clock speed. There is a bug in the current version of the cubeMX software. So there is work around:


/* USER CODE BEGIN PD */
#undef RTC_AUTO_1_SECOND
#define RTC_AUTO_1_SECOND 32767
/* USER CODE END PD */

To keep crystal clock division with a real crystal clock we need to redefine RTC_AUTO_1_SECOND value. Change it to your crystal clock frequency. Fine tuning of the clock is achieved using regular documented ways:

HAL_RTCEx_SetSmoothCalib(&hrtc,0,0,10);

Zeroes are values not used in this MCU. Read manual for further information.

This is the final clock image:
RTC USB OLED clock
Now it is in “alert” mode, indicating that the user is sitting near the computer for too long a time. Time to finish all games.

Close up of the OLED screen:
OLED

About Administrator

I am owner of this site.
This entry was posted in Anything, MCU and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *