Wednesday, December 12, 2012

Programming a STC (89C52) microcontroller

After finishing my basics in electronics i wanted to try my hand at programming micro controllers and this post is a direct result of my adventure in learning micro controller programming.

Read on If
  1. You are a newbie to micro controllers and want to learn the easiest way to program i.e. don't want to solder circuits, don't want to learn internals of micro controller(yet) etc., 
  2. You are ready to spend 30 to 40 CAD
  3. If you don't know Chinese(because the micro controller i am using 'STC' is a Chinese made one and has better information in Chinese)
  4. You are ok to do some C coding.
BTW the reason i chose STC was because i did not know a thing about micro controller programming when i started so i bought the cheapest one with the most  functionality i saw.

STC is a Chinese design company which produces improved derivatives of the classic 8051 MCU. You can get more info from stcmcu.com (in English en.stcmcu.com)  or mcu-memory.com. The STC chips seems to be manufactured by a variety of other companies e.g stcmicro.com, stc-51.com, also i see reference to the Taiwanese company tsmc.com, anyway this does not matter to us.

The one i am using is STC89C52RC(why? because that's the one that came with the kit :) )
I think its better to buy a development board with the latest MCU i.e the 90, 10, 11, 12 or 15 series as it has English software and manual.
BTW stc-51.com is selling development boards(starter kit at 25 USD) with the STC12C5A60S2 chip which has the English programming tool(STC ISP V4.86)


Terminology used below:

MCU Microcontroller Unit
IDE Integrate Development Environment A graphical development tool which combines a editor, compiler, linker etc.,
ISP In System Programming A way to load user program into MCU without removing the MCU from the target system
ICSP In Circuit Serial Programming A subset of ISP
IC Integrate Circuit


Things you need:
  1. A MCU development board
  2. A cross compiler
  3. A Programmer
MCU development board:
A development board is a circuit board which holds the MCU and has other components to test the MCU. The components may be other IC  or discrete ones like LEDs, buttons, relays etc., For us this is the target board.
I got a development board (STC Kit) which also supports programming through serial interface.
It cost me about 30 CAD and comes with everything you need to start writing programs for MCU and to test your programs.The only snag here is that everything is in Chinese !!! I had to use Google to translate to English. I have provided the details of all that i have learnt through this process.

STC Development board

Closeup of the MCU (This is a 40 pin DIP - Dual Inline Package)


Cross compiler:
A compiler is a software which convert your human readable program into machine language which the machine(microprocessor within the computer) can understand.
A cross compiler is a compiler which generates the machine code in a format different from the processor it is running on e.g. it runs on your PC and generates code for MCU and the architecture of the microprocessor in you PC(x86) is different from the MCU(8051). The destination architecture is called the target.

One good cross compiler is Keil. You can  download a demo copy of Keil from https://www.keil.com/c51/demo/eval/c51.htm. This has the µVision IDE and C51 compiler for writing and compiling C program. Although the demo has limitation on the size of binary generated etc., this is enough for starter. 
Note: There are other commercial and open source compiler and IDE which you can use for 8051. 
I feel its better to use Keil as i found it to be small, simple and easy to start with albeit some limitations. Go ahead and use the open source ones if you are comfortable setting up the tool-chain(i.e compiler, linker etc)

Keil also provides a lot of sample programs for 8051, refer http://www.keil.com/download/list/c51.htm

You can also use an assembler e.g. A51 from Keil or BASIC e.g BASCOM if you are comfortable or familiar with those.

Programmer:
A tool to transfer the machine code to the MCU
A programmer consist of a  software part and/or a hardware part. The above development boards supports programming using serial port and comes with a USB to serial adapter cable(more on this later)
The accompanying CD comes with a programing software(STC ISP V4.80)
If you are buying a universal programmer make sure it supports the MCU in the development board and also has a good user manual in the language you understand.
BTW most of the MCU nowadays support ISP so you can buy a USB ISP tool and wire it to the ISP pins on the MCU or to the ISP port on the development board.


The Real Stuff:
At this point if you have got the stuff 1 &2 from 'Things you need' you are ready for setup and then to write your first program. Below are the steps you should follow

Step 1: Install the USB to Serial driver
This is needed to download your program into the MCU
 
Install the driver (HL-340) from the CD or google for HL-340 driver



 Click on INSTALL


 After installing the driver connect the USB to Serial cable to your PC using the USB port.



Windows will prompt you to install driver for the new hardware, chose windows to automatically install the driver.



After successful configuration, verify that the driver has been installed properly using the device manager. You should be seeing a new COM port(e.g COM4) assigned. The port assignment is based on the USB port you connect the cable to.


If you unplug the cable, the device manager will refresh as below


How to open device manager in XP ? Simple, type devmgmt.msc in the Run dialog box(Win key + r)


 Closeup of the USB to RS-232 chip inside the cable
Step 2: Install Keil on your PC


After installing Keil you need to configure it for the STC MCU
Keil has a device database which is used to select a specific target MCU for your program. STC is not available by default but fortunately STCMCU has provided the device database file for Keil. Refer to stcmcu.com for getting the CDB file or download it from STC Artifact

To add STC MCU to Keil device DB:
Based on your version of Keil (uv2, uv3 or uv4), copy the corresponding CDB to kiel install and update the tools.ini file as below
For e.g. if you have  µVision 4 installed in c:\keil
1. Copy uv4.cdb to c:\keil\uv4\uv4.stc.cdb
2. Update c:\keil\tools.ini as follows

[uv2]
CDB0=uv4\uv4.stc.cdb ("STC")




 Now when you list the devices available in Keil(File->Device Database) you can see two groups, "Generic CPU" and STC

All the STC MCUs are under the STC grouping

Adding STC specific registers(optional):
STC has its own set of SFR(Special Function Registers), these are defined in the STC header file, refer to STCMCU.com or  STC Artifact
 
Step 3:  Install the program download tool(stc-isp-v4.80)

Unfortunately i am unable to find the English version of the tool which supports STC89C52RC (i guess its better to buy a development board with the latest MCU i.e the 90, 10, 11, 12 or 15 series).

I downloaded a later version (4.83) from stcmcu.com and installed it


Step 4: Setup and test hardware

Now connect the development board to your PC and switch on the power



You should see the factory loaded program running. :) The board is working !!!
Don't worry if nothing happens, power off and recheck the connections. If still nothing happens, maybe there is no pre-installed program, just carry on.

Now to program the MCU you need to connect the serial cable. Power off the board and connect the USB to Serial cable. Switch on the power and you should see the existing program running again.


Hardware setup and testing is complete


Step 5:  Write a simple program in C using Keil
Startup Keil, create a new project and select the target as STC89C52RC(or the MCU you are using). Create a C source file and add it to the project. Google on how to use Keil.

I just copied some code from the CD that came with the board, this displays numbers in the first 7 segment LED display
Compile the program


You should not see any errors in the build output window


If you see some error fix them like you fix any C compiler error

After fixing all the errors now its time to generate .hex file (The hex file is the binary file which is loaded into the MCU, google "hex file format" for more info)

Configure output to generate .hex file






Build target once again to generate the hex file in the project directory





Step 6: Download the hex file into the MCU

Turn off the MCU board

Start the STC-ISP software(double click STC_ISP_V483.exe)
Select the target MCU



Open the hex file


Configure the COM port (1), this is the port displayed in device manager when you connected the USB to Serial cable. Note: The green dot would light up if the correct COM port is selected.

Select download (2)


You should see the programmer waiting (3)

Now power on the development board 

You should see the program getting downloaded


If all goes well you should see the 7 segment LED display light up.


Hurray!!! You have just finished programming a MCU.

Although the above blog helps you in setting up and testing programming a MCU you should read more on
1. Basic Electronics
2. Digital Electronics

3. Writing optimized C code
4. MCU architecture
5. Using the MCU outside a development board.

All the best.

External Links:
Stuff i found related to STC
http://ncrmnt.org/wp/2012/10/01/stcdude-0-1-rc1-is-here/
https://github.com/nekromant/stcdude

If you don't find the STC kit in ebay using the link, search for "STK16 / STK32 STC ATMEL AVR Atmega16 Development Experiment Board MCU AVR"

26 comments:

  1. I finally succeded on programming my board, In windows and in Linux! YOur tutorial was very helpful, though I had to do somethings a little different...

    Rhpecanha - eletrofun.com.br

    ReplyDelete
  2. It was the starting point for me too.
    I'm in the middle of a battle with this microcontroller.
    I hope to win in the end. :)

    Sil
    http://openhardware.ro

    ReplyDelete
  3. please give me this board drive software. EX:HL340

    ReplyDelete
  4. Hi, this info is very useful. Could somebody answer me; how to upload or read or copy program from a STC micro-controller (I am using STC125620AD) to a PC or another mcu. Thanks

    ReplyDelete
  5. Hi, your MCU Dev Bd is no longer available the the given eBay URL.
    Should I be able to program an STC89C54 with most any MCU Dev Bd?
    I'm not finding one like you have. I'm just getting started, any advice appreciated.

    ReplyDelete
    Replies
    1. Yes you should be able to use STC89C54, there are also tonnes of cheap boards available in aliexpress, banggood, dealextreame, dhgate etc.,

      Delete
    2. Looks like I'm using an STC 89C52RC on my dev board. My question is, the 89C52RC is not listed in my STC-ISP V 4.86 (EN) but it is listed in a Chineese 4.80 version that will not operate(open files, etc.). Any thoughts of why Ver 4.86 doesn't have all MCUs listed? How to add them?

      Delete
    3. I have a bit more going now. I found the STC_ISP V4.83 you used, and it contained the 89C52RC MCU. Now when attempting download I'm receiving a Connection Failed after the following: Chinese:????? MCU/??? ???? ...
      Chinese:????,???????:
      1.?????????,?????,???????
      2.????,???? RS-232 ??, ????
      3.?????? P1.0/P1.1 ????
      4.????????
      5.? PLCC?PQFP ???????????????,?
      ????
      6.????????? STC-ISP.exe ??
      7.??????,?? MCU/????? ISP ?????,
      ??????,? MCU ???
      8.??? USB ? RS-232 ?????,????????
      ???,???????????? USB ? RS-232
      ???

      ?????, ?? MCU ??...
      MCU Type is: STC89C52RC
      MCU Firmware Version: 4.3C
      Chinese:MCU ?????: 4.3C
      Double speed / ???: 12T/???
      ???????: full gain
      ????? P1.0/P1.1 ?????
      ????AUX-RAM: ????(????)
      ??????????????Flash???: NO
      ????????????: ??????
      ALE pin ?? ALE
      ??????:12.003264M.
      ??????:12.003264M.
      Chinese:?????? ...
      Connection failed. / ???? (End: 17:55:05)
      Would love to know what's failing.
      Any guidence appreciated.

      Delete
    4. All is working now with STC_ISP V6.60.
      The strange thing is, the STC-ISP is in English, opening with a 12..series MCU, but as soon as I select the 89C52/LE52RC series all the English changes to random senseless characters, probably supposed to be Chineese.

      Delete
    5. Hope this will help you www.otc-ph.webs.com/download

      Delete
  6. is there any Mac substitutes for this project ?

    ReplyDelete
  7. I can download burned hex file from STC MCU?
    Thanks

    ReplyDelete
  8. What would be the software with the same A letter in display for this programmer?
    http://www.dx.com/p/51-avr-mcu-microcontroller-development-board-150862#.U2aRW_mSyoo

    ReplyDelete
  9. Great info, Is there any way to erase the chip and try again if I screw up. My programmer says I can not erase, read verify. So when I screw up, I gotta use another chip, and I'm all out of blank chips........ Any ideas, anyone.
    Thanks

    ReplyDelete
    Replies
    1. Hi,
      Thanks, It has been a long time since I did anything with mcu but as far as I understand the programmer erases and rewrites every time you download a new program into the chip. This can be done usually many number of time. I dont understand "My programmer says I can not erase, read verify. " - pls elaborate.

      Delete
  10. Among those posts I've seen, this is the most particular one, and I think the blogger must have spent lots of time on it, thank you so much!

    electronic display boards india

    ReplyDelete
  11. I also a similar board, but not sure, it just has no response in "checking target MCU", already off the power and on the power (it was working last night...)

    ReplyDelete
  12. Thanks for the tutorial. I'm still working through it but I found what appears to be an English version of STC-ISP that supports the STC89C52 and similar here:

    http://www.stcmcu.com/datasheet/stc/STC-PROGRAMMER-V1.8/STC_Pror_V180_for_89xx_En.exe

    ReplyDelete
    Replies
    1. Hi Martin,

      Thanks for the update, I have been taking a hiatus after my son was born, hoping to get back into MCU soon. Will check out your link.

      Delete
  13. I am exactly in the same situation as you were. I have a board with STC90C516RD+ MCU. Everything is Chinese and my friend bought it in China because he had no idea what to buy. An AVR board would have been a much better alternative. By reading this post I see there is hope.

    ReplyDelete
  14. wat was the best programer for STC 90c52rc

    ReplyDelete
  15. Sir, I have YL-39, a Chinese STC 89c52 MCU board. I could not found the USB drivers. I am using Windows7, 64 bit system. Can you please help in this regard.

    ReplyDelete
  16. Microcontroller is an electronic piece of digital system that is discovered and developed after a computer. It saves a set of commands, Programming that is subject to it before use. Unlike the analogue circuitry that is designed to perform a certain fixed function, and if you want to modify it requires a change in connections and in most cases this also requires changing the components. The microcontroller is used to perform a variety of different functions, and is used in factories and machines that are working on a set of steps and orders or instructions.

    ReplyDelete
  17. Hi, would someone please help me use STC15W202S chip with Keil. I did download STC device database for UV4 and included the ".cdb" file in the "tools.ini" file. But this does not support STC15Wxxxx family. Any help would be appreciated.

    ReplyDelete