Skip to content

Instantly share code, notes, and snippets.

@trozodejamon
Last active March 18, 2024 06:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trozodejamon/014ad06c36f25b99ef275c8c178a4571 to your computer and use it in GitHub Desktop.
Save trozodejamon/014ad06c36f25b99ef275c8c178a4571 to your computer and use it in GitHub Desktop.

STC Micro 8051 Microcontroller Notes

Quick Notes

The critical few things you need to know about it are:

  1. 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.
  2. They have a built-in bootloader that emits a magic number, then listens on UART0 (RxD, TxD) on bootup. The ISP programmer uses this.
  3. 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.
  4. C Compilers - the manufacturer recommends Keil, but that costs real money, alternatively, you can use SDCC, or PlatformIO.
  5. No, you cannot read out the contents of the Flash as far as I know.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.

STC ISP and Bootloader

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.

SDCC Notes

  1. 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.
  2. Include the 8051.h or 8052.h header file as appropriate, though STC's datasheet says you should include the 8051.h file.
  3. 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".
  4. After compiling, SDCC will generate an IHX file. You need to run packihx mycode.ihx >mycode.hex to convert to Intel HEX format.
  5. Example of compiler switches for the STC15W408AS: sdcc -pmcs51 --iram-size 256 --xram-size 256 mycode.c
  6. 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;

STC15W408AS Specific

  1. Compiler switches: sdcc -pmcs51 --iram-size 256 --xram-size 256 --code-size 8192 mycode.c
  2. Uart1 use: Need to define the SFRs for T2H, T2L, AUXR; use Uart1 and configure it to use Timer2 for baud rate generation.
  3. This MCU family doesn't have Timer1 at all.

STC15F2K60S2 Specific

  1. Compiler switches: sdcc -pmcs51 --iram-size 256 --xram-size 1792 --code-size 61440 mycode.c

Directory of resources

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.

  1. STC Micro website
  2. 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.
  3. PlatformIO
  4. stcgal - Original by Grigory Goronzy
  5. stc8prog - By IOSetting, but has less functionality than stcgal
  6. stcgal - Updated, patched version of Grigory's to support STC8G and STC8H
  7. uni-STC - a HAL to support STC MCUs.
  8. EdSim51 - a free 8051 simulator. Runs on multiple platforms via Java
  9. PAULMON2 and many other 8051 resources.
  10. 8052 Workbench Hein Pragt's 8052 Workbench for Windows.
@trozodejamon
Copy link
Author

Initial unstructured notes.

@trozodejamon
Copy link
Author

Changed to Markdown.

@j75
Copy link

j75 commented Mar 17, 2024

Why --iram-size 256 --xram-size 256 for STC15W408AS? It has an internal 512 bytes RAM, so shouldn't it be better --iram-size 512 --xram-size 0?

@trozodejamon
Copy link
Author

trozodejamon commented Mar 18, 2024

It would be nice, but it's not possible because the 4 banks of 128 bytes SRAM in the STC15W40x series are split up as 256 bytes of RAM and 256 bytes of AUX RAM, i.e., the internal RAM, as per 8051/8052 design is only on an 8-bits address bus, even though SDCC will allow you to specify that IRAM is 512 bytes. It is a characteristic of the 8051/8052 series that has existed since 1980. AUX RAM can have up to 16-bits address bus size, which is why you will then need the DPH and DPL registers. AUX RAM has a sort of cheat mode if memory serves where if you are only accessing the bottom 256 bytes, you can address it directly, but I haven't tried this in assembler in awhile, and just leave it to the compiler to figure out. You can check it in the datasheet for the part, it clearly shows it in the STC15W401AS series block diagram. Furthermore, in assembly language, it is necessary to use 2 different instructions to access the two different RAMs, thus the need to specify to the compiler how much of each that you have so it will know. Finally, you will need to use the right memory model, but by default, the compiler will choose something that should work for your program. So you will find that the first byte of internal RAM is at internal address bus 0x00, and the first byte of the AUX RAM is also at address 0x0000, but on a different bus. If you dig further, you will notice that there are actually 3 kinds of overlapped sets of memories in the 8051 series, the internal RAM that goes from addresses 0x00 - 0xFF, then the special function registers, that occupy the same address range but are accessed differently in assembly, and the AUX memory which also occupies 0x0000 and above, but on a different bus and accessed using a different instruction. This means that you can actually write and read back 3 different values at the same address, depending on whether you use direct addressing, register indirect or the MOVX instruction, and they will all exist simultaneously in 3 different physical memories on the MCU. It also means that you cannot allocate a single array larger than 256 bytes on this MCU. You should get an error from the compiler. In SDCC C, to access the AUX RAM, you need to prefix the variable declaration with __xdata and that will ensure that the compiler places it in AUX RAM and that it generates the correct instructions to access and manipulate the memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment