This is a replacement program for the STC 8051 microcontroller based DIY digital clock kits available from numerous Chinese sources. It is configurable for minor hardware variations, such as MCU pin assignments. The code easily fits into a 4K microcontroller, even with the serial debug feature enabled.
This is a heavily modified fork of R. Sloyer's STC LED Clock project. If my version doesn't suit your needs, try his.
This version was developed using SDCC 4.0. It generates efficient code in most cases, but there are situations where it produces sub-optimal results, or is unexpectedly sensitive to source code style.
The speech feature has been removed, because it would require modifications to fit my other changes, and I have no access to a clock with a sound chip for testing.
This project is licensed under the MIT License; see the license
file for details.
This software and documentation is provided as-is, with no warranty. You may use it at your own risk. See the license for further details.
- Time display (12 and 24 hour modes)
- Alarm and hourly chime
- Date and weekday display
- Ambient Temperature display, in °C or °F
- Configurable automatic brightness
- Temperature offset adjustment
- Auto-increment when setting times/alarms/etc. No need to repeatedly press the same key.
- Configuration menu with text prompts, within the limits of four 7-segment digits.
The switches are assigned the following functions:
- S1: Advance to next item
- S2: Accept or increment
In clock mode, press S2 once to automatically cycle through the following:
- Weekday
- Date
- Temperature
- Seconds
Each of the above items is displayed for a short time, then normal clock display resumes. If the automatic display cycling option is on, then this sequence begins automatically, once per minute.
Press S2 during weekday, date, or temperature display to resume normal clock display immediately.
Press S2 while seconds are displayed, to continue displaying seconds until S2 is pressed again.
From clock mode:
- Press S1 to cycle through available groups of settings.
- Press S2 to select a group of settings.
- Press S2 to change a setting, or S1 to advance to the next setting.
- At the last setting in the group, press S1 to save changes and resume normal clock display.
The settings are:
CL
clock settings- hour
- minute
- seconds (S2 snaps to the nearest minute)
co
daily correction of -9 to +9 seconds
AL
alarm settings- on/off
- hour
- minute
CH
chime settings- on/off
Cb
chime begin hourCE
chime end hour
dAtE
date settingsYr
year (last 2 digits only)- month
- date (day of month)
- weekday
CFg
configuration12H
or24H
dEgC
ordEgF
dE31
(month/day) or31dE
(day/month) formatAC
automatic display cycling on/off
brt
bH
brightness high limitbL
brightness low limit
CAL
temperature calibration- temperature measurement offset
This firmware is applicable to clock kits using an STC15 series microcontroller. If your kit uses a different microcontroller, it's probably best to look for a different project that is applicable to your hardware.
Hopefully, you installed the 4-pin programming header (P1) while building your clock. If not, you may need to either desolder the 7-segment display, or solder a header (perhaps via flywires) to the board surface.
Since the original firmware loaded on the STC processor cannot be copied, you cannot save and restore what you originally received with your clock. If you want to retain this code for any reason, you must leave your original microcontroller unmodified, and purchase a compatible replacement chip. These are readily available on AliExpress and eBay in small quantities.
For connection to the board, use a USB serial adapter with 5V signals. These are commonly based on FT232, PL2303 or CH340 chipsets. It will be connected to the board as follows:
P1 header | Serial adapter |
---|---|
5V | 5V |
GND | GND |
P3.0 | TXD |
P3.1 | RXD |
Simple direct wiring is acceptable. For development work, a more complex cable may be desirable; look in the STC datasheet for "Application Circuit Diagram for ISP".
If you intend to use the serial debug output feature, you will likely want a second USB serial adapter, ideally equipped with mini-grabbers for connecting ground and data pins to the board. Use a multimeter and/or the schematic diagram to locate a convenient ground connection. Connect your serial RXD pin to the microcontroller TXD pin (typically P3.7); see serial.c
and the microcontroller datasheet for details. No connection of serial TXD to microcontroller RXD is necessary, as the code does not support receiving data.
To compile the software, you'll need make
and SDCC.
To configure the processor and flash the firmware, you'll need either STCGAL or STC-ISP (Windows-only).
Configure the software for your hardware and preferences by editing global.h
.
Run make
to compile the software; this produces clock.hex
.
The included program.sh
script provides an example of stcgal
invocation.
The STC-ISP tool is pretty straightforward. The process is:
- Select the correct processor type
- Select COM port
- Open code file (main.hex)
- Select clock speed of 22.1184 MHz on the Hardware Option tab
- Click the Download/Program button
- Briefly interrupt the 5V supply to the STC processor
If you're using simple wiring, then the last step can be accomplished by unplugging the programming cable from the board, then reattaching it. Cycling the USB connection is not recommended.
If you've built the ISP cable described in the STC datasheet, use its power switch.
The test
folder contains a very minimal (incomplete) test suite, which utilizes the ucsim
microcontroller simulation tool that is typically included with SDCC. Simply run make
to run the tests. If you make code changes, then I encourage you to write additional tests.
The code includes a simple serial output mechanism, which can be useful for debugging; see serial.c
and serial.h
. The simple "Print" functions provided here are sufficient for most debugging tasks. SDCC also supports a small-footprint printf_tiny
function that only requires about 400 additional bytes of flash.
Interrupt service routines are sensitive to timing, and should not attempt to produce serial output.
The CPU clock is set to run at 22.1184 MHz. This divides evenly by 192 to produce the standard baud rate of 115200 bps, which is used by the debug serial port.
Timer 0 is configured to tick every 50 µs (20 KHz). This is the display scan rate; see display.h
.
None at this time. If you find a problem, I may be interested in trying to address it. If you want an additional feature, you're welcome to ask, but don't be surprised if I decline. You're welcome to fork the project and modify the code to suit your needs/preferences.
- Graham Knap - Major modifications
- R. Sloyer - Initial work - aFewBits
- zerog2k for his original STC DIY Clock work
- STC and zerog2k for the ADC code used in the LDR and temperature measurement code
- Maxim Integrated for their DS1302 app note and 8051 example code
- All the contributors to the SDCC tool set
Main STC website (in Chinese): http://www.stcmcu.com or http://www.gxwmcu.com
STC15 series English datasheet: http://www.stcmcu.com/datasheet/stc/STC-AD-PDF/STC15-English.pdf
Older STC15F204EA English datasheet: http://gxwmcu.com/datasheet/stc/STC-AD-PDF/STC15F204EA-series-english.pdf
Other STC documents (English): http://stcmicro.com/sjsc.html
SDCC User Guide (PDF): http://sdcc.sourceforge.net/doc/sdccman.pdf
Maxim DS1302 datasheet: http://datasheets.maximintegrated.com/en/ds/DS1302.pdf