Exploring STC 8051 Microcontrollers – Coding

My Customized BSP

To deal with the vast array of peripherals of STC8A8K64S4A12, I needed something to quickly deploy projects without going through the registers every time. Having gotten the idea of Board Support Package (BSP) while working with Nuvoton N76E003, it was time for me to develop something similar for STC micros here as officially STC does not have such beautiful software implementation for their microcontrollers. However, STC does provide lot of both C and assembly language examples in their reference manuals and programmer GUI unlike other manufacturers. Those examples, though helpful, do not fit in all possible scenarios and often incomplete in terms of meaning. By having a BSP, we can use our micro’s hardware peripherals more efficiently with ease and in a wide variety of ways. We would no longer need to create and call functions for hardware peripherals every time. Nuvoton, TI, STMicroelectronics, Silicon Labs and many other mainstream microcontroller manufacturers are tooling various methods to reduce coding efforts and aid in rapid code development.

It is not an easy task to go through an entire reference manual, reading and trying out everything one-by-one. However, I had to do it no matter how painstaking job it was. This is because firstly, I hate setting registers repetitively every single time when I want to make a new project and secondly, I want to make work easy so that less time, resource and effort are spent. I also have a tendency to forget things quickly. This solution will be as such that it can be modified easily and ported for other STC microcontrollers as well.

I have developed my BSP for STC8A8K64S4A12 in an orderly fashion. There are header files for each hardware peripherals that contain all necessary functions and definitions. An example of STC8A8K64S4A12’s watchdog timer’s header file is shown below:

    Watchdog Overflow Time = ((12 * 32768 * 2^(WDT_PS + 1)) / Sysclk)
#define WDT_div_factor_2                        0x00
#define WDT_div_factor_4                        0x01
#define WDT_div_factor_8                        0x02
#define WDT_div_factor_16                       0x03
#define WDT_div_factor_32                       0x04
#define WDT_div_factor_64                       0x05
#define WDT_div_factor_128                      0x06
#define WDT_div_factor_256                      0x07
#define WDT_set_prescalar(value)                do{ \
                                                    WDT_CONTR &= 0xF8; \
                                                    WDT_CONTR |= value; \
#define WDT_stop_counting_in_idle_mode          0x00
#define WDT_continue_counting_in_idle_mode      0x08
#define WDT_start                               bit_set(WDT_CONTR, 5)
#define WDT_get_overflow_flag                   get_bit(WDT_CONTR, 7)
#define WDT_clear_overflow_flag                 bit_set(WDT_CONTR, 7)
#define WDT_reset                               bit_set(WDT_CONTR, 4)
#define WDT_clear                               do{ \
                                                    WDT_CONTR = 0x00; \
#define WDT_setup(CNT_mode, PS)                 do{ \
                                                    WDT_clear; \
                                                    WDT_CONTR |= CNT_mode; \
                                                    WDT_set_prescalar(PS); \

Now let’s see what the registers and their settings look like:

As we can see there are a whole bunch of settings that need attention while using. This is perhaps the easiest way to show what I wanted to achieve.

Please note that I have tested most of the stuffs that STC8A8K64S4A12 could offer. I tested them rigorously and with confidence I can say all of these functions have been found to be okay. Being the sole developer, I did my checks as much as possible but there could be unforeseen bugs that I may have overlooked. A typical case could be wrong function naming. This is so because during development of these BSP files I made several changes when new issues appeared. Therefore, I would like to request readers to report any issue when discovered and I would also like readers to read the reference manual of STC8 series completely if possible.

The examples presented in this document are based on my custom BSP and I believe that the journey with STC microcontroller supported by my BSP would be a joyful one.

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Related Posts


  • i always get excited when you release new tutorials ,you are really doing a great job i wish i could write code and develop libraries like you.

  • Well, this is very nice and thorough tutorial indeed, many thanks!
    Unfortunately I doubt there is good any reason to learn the STC platform beyond curiosity.
    The STC 8051, although pretty evolved from the original 8051 ISA, does not offer anything crucial to justify the relatively high price of these micros and development tools along with certain cumbersomeness of this ancient platform.
    They simply can not compete even with the legacy Cortex M0 in any way. I am even not aware about any affordable debugger/emulator for them.
    All in all, I would never recommend anybody to start learning/using any 8051 without some very good reason to do so.

Leave a Reply

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