Getting started with STM8 Development – Part 1 – Blinking a LED

Usually the first step toward learning development on a micro-controller is  simply blinking a LED, as an analog to “Hello, world!” example used on PC programming languages. This time we will have a look into how to start programming and development on STMicroelectronics STM8 series of micro-controllers.

What you need?

In order to start, obviously you need a development board of some sort (ST’s STM8 Discovery evaluation board is a good pick, or you can get away with el-cheapo chinese boards which are going for around 2$ including shipping!) and obviously a programmer to write your compiled code into the micro-controller.

STM8S003F3 Evaluation Board

STM8S003F3 Evaluation Board

For the programmer, you need one that support SWIM (Single Wire Interface Module) mode. You can (recommended) go with the original debugger of STMicroelectronics which is ST-Link V2 (you can get this one second hand as low as 20$) or if you are really want to go economical, you can get away with the fake ones wich cost you under 10$ (please note that these cheap debuggers only support software mode, which works fine, and do not give you full functionality and speed of the genuine debuggers of ST itself).

Fake ST-Link V2

Fake ST-Link V2

One more thing that you need, is the development environment and compiler. If you are on Windows, I recommend to get the code-size limited version of IAR Embedded Workbench. If you are on linux, things are a bit more complex (I will do a separate tutorial for linux as soon as possible).

Start the job!

For this tutorial, I am using an evaluation board which is based on STM8S003F3 micro-controller. This should not make any difference for all other chips in STM8 series, as long as you know which PIN is your LED or LEDS are connected to. In this case, the LED on my board is connected to PortD, Bit 3 which reads PD3 on the PCB.

The first step is to configure the PD3 pin (or whichever pin your LED is connected to).

The code above configure the PD3 pin (Port D, Pin or Bit 3). If your LED is connected to something else, e.g. PC1, then your code should look like this:

Just compare the two code snippets above, I am sure you will figure out how to change it to work on any port and bit that you like!

Next, we need a very simple delay function, two insert a pause between ON and OFF state of our LED!

The above delay, is just a counter which  decrements the value you gave to it until it reaches 0. This is a naive delay, and actual time would be based on clock configuration, and how much time the instruction for decrementing the given value and comparing it to 0 would take. But it will suffice for our work today!

Putting everything together, your main.c should look like this.

You notice the include directive on the first line of the code, in this case, it is provided by IAR STM8. Alternatively you can download the full standard library for STM8 from ST website.

At the end, you should have a blinking led on your board. If you faced any problem, just let me know in the comments and I will try to help you out.

stm8s003f3 LED blinking board

Blinking LED on STM8S003F3 Board

For more in-depth explanation, jump to Part 2 to learn how to setup a pin as input, to be able to detect button press.

Saeid Yazdani

An Electronics Engineer with passion in Embedded Systems, Wireless Sensor Networks, Assembly, Desktop and Mobile Development, Web Development and generally anything that has to do with hardware and software of any kind!

28 Comments:

  1. I get unable to connect to target! 🙁
    I have the same board and a cheap ST-Link2 clone. I got the same message with IAR and ST Visual Develop.
    I have also another board, smaller and blue, same message. It says that the chip not answer.

    ST-Link2 clone works with STM32 boards but I wasn’t able to made it works with STM8.

  2. Mohammad Hemmati

    I did this tutorial successfully and I really enjoyed it–THANK YOU–; the only disturbing thing which is not stated here was to disable the optimizer of the IAR to implement required delays. Because IAR is set to the highest possible optimization grade by default and omits the delay(not useful!) part! To make this not happen, you can do as the following:
    1-Go to : Project > Options > C/C++ Compiler > Optimizations
    2- Set the level to “None” and then press OK.
    3- Rebuild the project
    4- It’s done!
    Good Luck.

  3. Pingback: راه اندازیی برد Stm8s با اردینو

  4. Hi, Yours is the first tutorial I am reading for STM8 programming. Thanks.
    I have a question though. You use IAR (someone mentioned also ST visual develop)

    For other reasons,(I am also working with STM32 nucleos and stuff) I am thinking of installing Eclipse and GCC ARM. So that will take space of my hard disk.

    Do you think that I can program STM8 with eclipse too? (with the appropriate -if they exist- plug ins I suppose)

    Thanks

    • Yes, you can program/debug STM8 using eclipse too. But if there is no ready plugin for eclipse, it is gonna be up to you to configure your project, set the path to headers and include files, tell eclipse which compiler to use and so on. Eclipse is just an (highly customizable) IDE which you can pretty much write any code and compile it, granted you have set the right settings!

      • Thank you for your reply.

        I installed ST Visual develop instead with Cosmic Compiler.

        Is the code you posted here exclusive for IAR?

        • Yes, the code here is for IAR compiler. But the idea is the same, just include the appropriate header files for your chip from cosmic libraries and follow the same principles!

  5. Hello,

    Can you elaborate on the differences between the original ST Link v2 and the clone version, particularly that the clone version only supports software mode… of what etc?

    Thanks, Ian

    • The clone just works…it is however is not reliable for debugging…sometimes it halts the core or misses debug points. If you want 100% reliability you should go for original of course

  6. Link library for STM8S003F3 here https://youtu.be/SJYy8WWx9gE

  7. Why dose initial other register CR1&CR2?
    {
    PD_ODR=0x08;
    while
    {
    PD_ODR&=0x08;
    }
    }

  8. is it possible to compile stm8s003f3 on linux?

  9. is there is a complete tutorial explain ADC,PWM,INTERUPT,

    • There are some tutorials on youtube from another guy. If you want to get going faster, you need to get involved with the datasheet, reference manual and programming manual.

  10. why you set control register 1 and 2 to be 1(push and pull) what will happen if you do not set these register.

  11. Hello
    I know this must be a really stupid question but how do you download your program to the board in IAR EWARM? So far I see a green triangle but this is for debug. I don’t want it to debug, just run for ever
    Thanks

  12. Thanks for kindly answering my question
    I have another one, don’t expect a solution but rather ideas from your own experience.
    I am debugging some code I did not write for STM8 in IAR. When I run it, I get the disassembly window with a green line there (as expected). It is supposed I should also get something similar in the code window in order to debug line by line. but I don’t. I can not set breakpoints either. Has this ever happened to you?
    Any idea why this could be happening?
    Thanks!

  13. I tried to change delay and there is some tricky thing.
    When delay=10000 led blinks fast.
    When delay=50000 led blinks slow.
    When delay=90000 led blinks fast again.
    Why is it so?

  14. hey ,i run the code & face this error:
    “Failed to set ROP flag ”
    what should i do?

  15. Anthony Lauly

    Hello sir, can you explain how is the wiring between stm8s and the st link ?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.