The critical few things you need to know about it are:
- They are 1T (single clock cycle) 8051 compatible MCUs (but some instructions may take as many as 5 or 6 clock cycles to complete) and most are capable of operating at up to 35MHz. Check the markings on your part. If it says something like "28I", then it means that your part has been rated to operate at up to 28MHz. Note that rather than use nice round numbers like 12MHz, which is possible, it is more customary to use specific operating frequencies that facilitate the generation of a lower error UART baud rate. I.e., 11.0592MHz, than would otherwise be possible.
- They have a built-in bootloader that emits a magic number, then listens on UART0 (RxD, TxD) on bootup. The ISP programmer uses this.
- There are two main HEX file download tools, the manufacturer's own STCISP which is an all singing and dancing development support tool (it has pinout information, timer configuration and code generation tools, etc.), and then there is the stcgal FOSS tool (Python) developed by Grigory Goronzy, but beware, development stopped in 2021, and a different forked version is now what seems to be more up-to-date. Either way, you really will need STC's own STCISP tool, even if you decide to use a FOSS tool, because of the wealth of information it provides beyond just enabling you to download your code.
- C Compilers - the manufacturer recommends Keil, but that costs real money, alternatively, you can use SDCC, or PlatformIO.
- No, you cannot read out the contents of the Flash as far as I know.
- No I2C hardware peripheral. It seems you will need to bit-bang the I2C protocol in software. This appears to be quite a common thing, however, and there are many examples of such a thing on the Internet.
- By default, the STCISP tool tries to use a range of baud rates ranging from 2,400 bps to 115,200 bps with which to perform the firmware download, however, I found this can fail on some micros. In the case of the STC15W408AS I am using, I have to set both min and max to 2,400 bps and then it works quickly and flawlessly. So if your micro isn't connecting, then try limiting the speed. Since the STC15F2K60S2 has a much larger Flash, I tried using 4800 bps instead and didn't have any problems with that. I have not tried faster speeds yet.
- Still on the topic of serial, check the datasheet for recommended USB-serial adapter connection methods, as they advise adding a series resistor and diode - the STC 8051 micro might be powered parasitically from the TX and RX pins, fouling up the bootloader function. I can now confirm that this works well, exactly as they specify in the datasheet.
- The built-in EEPROM is not a true EEPROM. It is "EEPROM" implemented from Flash, only called "EEPROM" to make you feel good about it. I'll update here as I learn more. It is used/accessed via IAP SFRs. They have a 512 byte page size, which means you must erase an entire page at once.
- There is a useful and free 8051 simulator written by James Rogers that works quite well, but it does not simulate auxillary RAM. See list of resources below.
All STC 8051 micros come with a built-in bootloader that emits a magic number at power up and checks for a response from a waiting ISP software tool running on a host PC. As such, it is necessary to set your ISP tool going first, and then power cycle your board/microcontroller.
- Use the
-pmcs51
switch to tell the compiler to compile code for Intel 8051/8052 and compatibles, although this is actually the default target, so technically speaking, it isn't strictly necessary to specify it. - Include the 8051.h or 8052.h header file as appropriate, though STC's datasheet says you should include the 8051.h file.
- Specify the size of IRAM and XRAM in your MCU by using the
--iram-size
and--xram-size
compiler switches. You can also specify the start addresses of each if needed via more compiler switches. By default, SDCC assumes an IRAM size of 256 bytes anyway, which is what most modern 8051's have. Specify maximum code size with--code-size
. To make sure, check Chapter 3 of the datasheet marked "Memory Organisation and SFRs". - After compiling, SDCC will generate an IHX file. You need to run
packihx mycode.ihx >mycode.hex
to convert to Intel HEX format. - Example of compiler switches for the STC15W408AS:
sdcc -pmcs51 --iram-size 256 --xram-size 256 mycode.c
- SDCC's built-in header files for the 8051 and 8052 are missing a number of SFR definitions for the STC micros. Or stated otherwise, there are no specialised header files for the STC MCUs included in SDCC. You can find the proper way to define these for Keil C51 in the STCISP application - cutting and pasting this code directly into your source and trying to compile it with SDCC will produce errors because Keil and SDCC fundamentally define SFRs in different, but actually similar ways, just edit the style to match the SDCC coding syntax and off you go. For example: change
sfr AUXR = 0x8E;
to__sfr __at (0x8E) AUXR;
- Compiler switches:
sdcc -pmcs51 --iram-size 256 --xram-size 256 --code-size 8192 mycode.c
- Uart1 use: Need to define the SFRs for T2H, T2L, AUXR; use Uart1 and configure it to use Timer2 for baud rate generation.
- This MCU family doesn't have Timer1 at all.
- Compiler switches:
sdcc -pmcs51 --iram-size 256 --xram-size 1792 --code-size 61440 mycode.c
In reality, there are really too many 8051 resources available on the Internet to list here, but perhaps, these are among some of those you should make sure you do visit.
- STC Micro website
- SDCC Compiler In addition to the Intel MCS51 family, this compiler supports many 8-bit microcontrollers and microprocessors, including (but not limited to) HC08, 6502, Z80, Z180, STM8, and Padauk MCUs.
- PlatformIO
- stcgal - Original by Grigory Goronzy
- stc8prog - By IOSetting, but has less functionality than stcgal
- stcgal - Updated, patched version of Grigory's to support STC8G and STC8H
- uni-STC - a HAL to support STC MCUs.
- EdSim51 - a free 8051 simulator. Runs on multiple platforms via Java
- PAULMON2 and many other 8051 resources.
- 8052 Workbench Hein Pragt's 8052 Workbench for Windows.
Initial unstructured notes.