Starting STM8 Microcontrollers

Preparing the Software Tools

Firstly, we need three major documents before starting to program STM8 micros. These are as follows:

  1. STM8 Reference Manual.
  1. Datasheet of the MCU (STM8S003) that we’ll be using.
  2. STM8SVLDiscovery Board User Manual.

These docs will be needed everywhere during learning session. The reference manual states the use and purpose of all the hardware blocks in details. It includes register descriptions, naming conventions, modes of operation of all hardware, etc. However, it does not specify the specifications of a given STM8 micro and that’s because it is a generalized literature for all STM8S and STM8AF micros. As we know even within a family of micros, one MCU differs from another in many aspects. Most commonly this variation comes in the form of memory capacities and I/O pin counts. Sometimes electrical specs also vary and so to know the limits and general specs of our target MCUs we need to checkout their respective datasheets. Lastly the Discovery board user manual is most useful for the hardware schematics, pin assignments and layouts. If you are using some other board then I suggest that you acquire at least its schematic.

Now with Cosmic, STVD and STVP installed our software tool setup is almost ready. There are two approaches to STM8 programming. The first uses the traditional concepts of register-access-based coding, meaning you’ll have to set up every register on your own. The second way utilizes a specialized method of coding by using standard libraries developed by STM that are both universal and platform independent, meaning your C code will be same for any compiler using these libraries. These libraries are called Standard Peripheral Libraries (SPL). With these libraries, no one will ever need to get down to register-level access. The libraries are so coded that a coder will only have to know his/her chips’ hardware specs and some basics of these hardware. On the coding part, he/she will only have to set properties and desired values. The SPL manages the rest. For instance, when setting up a UART, we will only need to set interrupts, IOs and UART properties like baud rate, parity, etc. All of these setups are done with meaningful numbers and texts.

The STMicroelectronics Standard Peripheral Libraries (SPL) for STM8 microcontrollers can be found here:

I wrote this article using SPL since it will be ridiculous to code STM8s using the old-fashioned way of configuring registers one-by-one manually. Thus, it is a mandatory download item. You should preserve and retain the downloaded SPL zip file fully intact. You may need it when things get messy.

Now make two folders and name them “inc” and “src”. The “inc” folder will be filled with all the header files (“.h” extension files) from the extracted zip file. Similarly, the “src” folder will be holding the source files (“.c” extension files). For ease of work, it is better to keep these folders secured just like the SPL zip file because every time when we will be making new projects the files in these folders will be needed. You can copy these files to your project folder or you can keep it centrally somewhere. I prefer the former method as doing so will not have any conflicting issue with other projects needing modifications. However, it will cost hard-drive space. This method is however less confusing and trouble-free for beginners. Extract all the files as shown below:

Source and Header Files

Note that there are more header files than source files. This is because there are two extra header files – stm8s.h and stm8s_conf.h that define processor type and processor properties. To make things work, we will have to comment one line of the stm8s_conf.h. You will find a line at the bottom of this file written as:

#define USE_FULL_ASSERT    (1)

You need to comment or disable this line, otherwise the compiler will throw tons of error messages.  Always check this at the start of a project. Surely, we don’t want to assert our code.

Creating a New Code Project

Assuming that STVD, STVP and Cosmic are properly installed, we will see how to create a new project.

  • Firstly, run STVD.
  • Select File >> New Workspace.

New Workspace

  • Select Create workspace and project.

Create New Workspace

  • Select workspace folder and workspace name.

Workspace Title

  • Set project name and select toolchain STM8 Cosmic. You may need to set the path of your Cosmic compiler’s installation location. In my case, it is:
    C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8

Project Details

  • Type and select target chip part number. Last two or three digits and letters are enough for finding the correct micro.

Chip Selection

  • Now add the source and header files from the previously mentioned SPL folder.

File Inclusion

  • After file inclusions, the workspace tab changes as shown below:


  • Locate and open c file from the source tab, and then type #include “stm8s.h” at the top as shown below:

main file

  • You’ll have to edit the h header file and uncomment the chip number you are going to use as shown below:

STM8S.h Chip Identification

  • Compile the code once using the key combination CTRL+F7 or by pressing the compile button. If everything is okay, there should be no error or warning message. The reason for this blank compilation is to use the compiler’s powerful code assistant feature. With this feature, we can predict or complete a piece of code line by only writing the first few letters and then pressing CTRL + SPACE keys simultaneously.

Code Assistant

During compilation, you may get tons of errors for hardware files that are not available in your target STM8S micro. For instance, CAN hardware is not available in STM8S003K3 and so if you have added CAN source and header files you will get an error for that. Once identified by the error messages, the corresponding header and source files for that particular hardware must be removed.

A wiser approach to avoid this issue is to check device datasheet for available hardware in it. The contents section of datasheet shows this as shown below:


STM8CubeMX can also be used for this purpose but some hardware peripherals which do not have any input-output dependency are not shown in it as previously discussed.

Hardware 2

Similarly, one more caution must be observed. Unless your code is using any interrupt, interrupt source and header files (stm8s_it.h and stm8s_it.c) must be excluded. Sometimes it is better to add only those files that you will need to complete a project. For example, if your project is just using GPIOs, it is better to add GPIO files only along with stm8s.h and stm8s_conf.h. However, I recommend this technique only after you have mastered STM8 coding well because in most cases you will need multiple hardware which have dependencies on each other. As an example, when using SPI, you’ll need both GPIO and SPI modules. If you understand these dependencies, it is okay to select files as per need. You can, then, comment out unnecessary hardware module files specified in the stm8s.h header file and get a faster compilation and build process. After compilation, you should always build/rebuild your project by hitting the Build or Rebuild button. This will generate the final s19 output file in either Debug or Release folder according to the generation mode selected. If things are in order, there should be no error or warning message.

Coding Final

Lastly, I have not found any useful simulation software like Proteus VSM or Electronic Workbench that support STM8 family. Thus, we have to debug our code in real-life with real hardware. It may sound difficult but actually it is not so. We can, however, use such software to make models of STM8 micros and make our PCBs. I don’t like simulations as they are not always accurate and real-world type.

One more advice I would like to give to the readers. Please read the SPL help file. It is located in the SPL zip file under the name stm8s-a_stdperiph_lib_um.chm. It explains each function, definition, data structure, all internal hardware modules and how to use them properly. This is a very important document and your best friend in coding STM8 micros. Apart from this document the reference manual is equally important as it details the capabilities of all internal hardware. I won’t be detailing the internal hardware much as these docs will be doing so.

Check this video out for details:

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

Related Posts


  • Hello!
    And HUGE thanks for a one of the best documenting on “Embedded-lab Starting STM8 Microcontrollers” projects.
    I am trying making SPI demo with MAX7219- based scrolling dot-matrix display and a STM8S003k3, but there is something wrong with my debugging giving an error: “#error clnk Debug\matriisi2.lkf:1 symbol _delay_ms not defined (Debug\main.o Debug\max72xx.o )”

    I`m Not sure if I copied the MAIN.c, MAX72XX.h MAX72xx.h- files accordingly because of copy/paste function from “Code Example section”

    Please help me with the situation. THANKS!
    P.S. PC4 SPI__SS does not work for my STM8CUBEMX and PE5 shows SPI_NSS?!?!?!

    • Seems like software delay library is missing from your project….

      • Hello again and thanks for your reply!
        HMM…Obvioysly software delay library is missing, but…. I´m I totally lost trying to solve the problem by using the section “Creation & Addition of libraries” under “Some Useful Tips”? Please tell me where to find this “stm8s_delay.h”-file. Only I find is a STM8L15x “delay.h” which hardly suitable for my STM8S ?!?!?

        My workaround was to use function “void delay(unsigned int n)” I dont know if the trick is an acceptable programming practice but VOILA!!!! I can see and read the text from my Matrix-LCD.


  • Hello Shawon,

    Thanks for taking effort to put such a nice document on STM8. I would like to have multiple interrupt routines for multiple pins on PORT B. how can i declare multiple routines under PORT B. Here you are showing Port B Pin 7 routine. I would like to have Port B Pin6 also as external interrupt. How can i declare its routine seperatly like EXTI1_IRQHandle.

    • Since there is one interrupt vector for each port, you have to read needed GPIO pins of the respective port once interrupt triggers. In this way you can have multiple external interrupts on same port.

      • Thanks for your response Shawon.
        I have similar question regarding ADC usability. In my application i will be using multiple ADC pins. How can read ADC value of specific pin. I am refering to your ADC continuous conversion example.
        In that exaple you have PB0 as ADC pin and when you call ADC1_startcoversion(); it reads PB0 and you collect result at A0= ADC1_GetConversionValue();
        Now, if i have multiples ADC pin how can i get conversion value from intended pin? for e.g. PB1(AN1) and PB2(AN2)

  • Hi! I’m trying to run the Timer 1 PWM program on my Discovery Board (105C6T6 MCU). I’m testing it on all the channels of Timer 1. I get complementary PWM outputs from channel 2 on my oscilloscope. But for channel 1 and 3, only the complementary pins i.e. CH1N and CH3N pins give outputs while their counterparts only show some noise on the scope. Also channel 4 gives proper output. I’ve also tried with the code example given in the Standard Peripheral Library, but the same thing happens. What could be the reason? Please help!

  • Hi! I’m using your codes on my DISCOVERY BOARD (STM8S105C6T6 MCU) . Every time I try to debug a program on STVD it says “configuration error: detected mcu different from the selected one”. I’ve tried to change the MCU from Project settings => MCU selection, but to no avail. Please help!

  • muharrem çetinkaya

    Hi Shawon Shahryiar,

    I am sorry for my English,
    I’m using translate.

    Did you use software uart with stm8?
    can you share a example study?

  • Hi Shawon Shahryiar

    “stm8s conf.h” not there in stm library pls upload in drive and share the link for it


    Hi Shawon Shahryiar,
    I have debug time base example, but not getting any timer output. Actually i am not able to start any of timer. I have also tried with your example but same result. Am i doing anything wrong or something missing?
    Hardware : STM8S003F3

  • hi Shawon , i tried this for kit stm8s discovery with 105C6T6 and have nothing on the display of LCD . can u help me !!! tks a lot , brate !!!

  • Pingback: آموزش میکروکنترلر STM8 قسمت 21: بررسی اجمالی ارتباطات - سیسوگ - Sisoog

  • Hi, just wanted to ask where did you get the documentation for the SPL. The pic in your GPIO tutorial, where did you get that from ?

  • Hi sir, I’m using stvp + cosmic for almost a year. Recently as I compile there happens errors as follows: flexLM features has expired. I think my cosmic licence has expired. Thus I want to know what can I do? Can I get licence like the first time?

    • Yeah just like first time you have to reacquire a new license from Cosmic and reapply….

      • Excuse me shawon, I’m a bjt confused. I should resend the “CM8_license.txt” file to cosmic and how can I reapply the new license?should I replace it with previous license? I couldn’t find the previous license file. I don’t know where did I copy it…

        • First go to this location of your PC: “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8″…. Run LmregFSE.exe file and fill in the infos…. You send them the generated info via email and they will send you a new license file…. Copy-paste and overwrite the license file they will send here: “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\License”…. Your license would then be renewed for another year…. Simple…. 🙂

          • I sent it for cosmic but the problem is that I don’t remember where I copied license.lic. It’s not in “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\License” I must overwrite the new license. how can I find previous license location?

          • It should be “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\License” if your Windows is 64-bit edition or “C:\Program Files\COSMIC\FSE_Compilers\CXSTM8\License” if your Windows is 32-bit edition….

  • Pingback: آموزش میکروکنترلر STM8 قسمت 20: PWM تایمر 1 - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت 19 : PWM - سیسوگ - Sisoog

  • Shawon I’ve really progressed with your masterly article. I have a question; in “interupt.vector.c” you said we should comment the “@far @interrupt void nonHandledInterrupt(void)” ( in above the interrupt.vextor.c file ) but as I comment this function I got 33 errors for 33 nonHandeledInterupts… so I didn’t do that and I’ve never had an issue yet. I want to know does it make a problem? Is it necessary to comment that function??

  • Hi shawon☺ , I used an External interupt for connecting a switch to micro. The problem is that when I push the button the ISR repeated many times but I want my ISR run only a time. Can you help me please??

    • Firstly did you use pull-up and glitch removal capacitor across the switching point? Secondly, you can remove this repetitive interrupt by disabling external/global interrupt once an interrupt has been detected and reenabling external/global after processing the ISR….

      • Thanks a lot shawon. I’ve tried to achieve real time. For do that I used 11.0592 Mhz crystal and timer2 on prescaler 1024, timer period: 108. With this regulation the interupt must occurs every 1ms. But it doesn’t work probably . In other words it has 1 second difference in a minute. What’s the problem?

  • Pingback: آموزش میکروکنترلر STM8 قسمت 17: تایمر 2 - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت 16: اصول اولیه تایمرها - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت 15: تایمر نگهبان محدوده ای (WWDG) - سیسوگ - Sisoog

  • Hi Gents,

    I’m using the IAR environment and tried to use the delay feature from these tutorials. Unfortunately I
    meet an issue with the dealay function.
    I have imported both .c & .h files to the project and tried to use the delay_ms(); but during compilation I see error like below:
    “Error[Li005]: no definition for “_asm” [referenced from D:\…\Debug\Obj\stm8s_delay.o].

    I have no idea what can be wrong, maybe I missed add some definitions, perhaps someone could explain it to me?

    Thank U in advance

  • Hey guys;
    Could you help me please, I’ve copied the Tmer Interupt(TIM4) project, but when I built it, there was an error:
    “@svlreg missing for function f_TIM4_UPD_IRQHandler”
    I just recomment the top of “interupt_vector.c” because I had 33 errors.

  • Pingback: آموزش میکروکنترلر STM8 قسمت 14: تایمر نگهبان (IWDG) - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت 13: تایمر نگهبان آنالوگ (AWD) - سیسوگ - Sisoog

  • ——- Project stm8s103 – STM8 Cosmic – Configuration Release ————-

    “on release option i encounter this error what is the solution for this
    in debugging mode i don’t have any errors please help”

    Compiling ..\..\main.c…
    cxstm8 +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clRelease\ -coRelease\ ..\..\main.c
    #error cpstm8 ..\..\main.c:29 can’t open stm8s.h
    #error cpstm8 ..\..\main.c:46(12+8) missing )
    #error cpstm8 ..\..\main.c:60(11) missing prototype
    #error cpstm8 ..\..\main.c:60(45) missing prototype
    #error cpstm8 ..\..\main.c:65(21) missing prototype
    #error cpstm8 ..\..\main.c:65(55) missing prototype
    #error cpstm8 ..\..\main.c:66(9) missing prototype
    #error cpstm8 ..\..\main.c:60(45+10) GPIO_PIN_0 undefined
    #error cpstm8 ..\..\main.c:60(12+5) GPIOA undefined
    #error cpstm8 ..\..\main.c:60(60+25) GPIO_MODE_OUT_PP_LOW_FAST undefined
    #error cpstm8 ..\..\main.c:76(11+8) missing )
    #error cpstm8 ..\..\main.c:76(26) old style argument declaration
    #error cpstm8 ..\..\main.c:79(9+6) nCount undefined
    The command: “cxstm8 +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clRelease\ -coRelease\ ..\..\main.c ” has failed, the returned value is: 1
    exit code=1.

    stm8s103.elf – 15 error(s), 0 warning(s)

    • Seems like the compiler doesn’t know the location of the source and header files…. Go to Project >> Settings…. Select Debug/Release and check the debug tab there…. Locations for the header and source files should be shown there or else you’ll get this sort of error….

      • yes, sir as per your video i did the process , but previously i use st’s standard peripheral library “template” to check GPIO ready project. in st’s standard library ” Example project” compiled in debug mode but in release mode it gives above error. then i follow your video process and error gone hex.file generated in debug and release mode too. my request is to make separate page for stm8 and explain the st’s standard peripheral library and your project list for stm8 only with separate section.Thanks.

  • Pingback: آموزش میکروکنترلر STM8 قسمت 12: مبدل آنالوگ به دیجیتال (ADC) - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت دهم: Beeper - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت نهم: وقفه خارجی (EXTI) - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت هشتم : کلاک سیستم (CLK) - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت هفتم: LED چشمک‌زن - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8: چگونه برنامه خود را روی STM8 آپلود کنیم؟ - سیسوگ - Sisoog

  • Pingback: قسمت پنجم: آماده سازی ابزارهای نرم‌افزاری برای STM8 - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت چهارم: STM8CubeMX - سیسوگ - Sisoog

  • Pingback: آموزش میکروکنترلر STM8 قسمت سوم: کامپایلر و پروگرامر - سیسوگ - Sisoog

  • Pingback: قسمت دوم:معرفی بردهای Discovery میکروکنترلر STM8 - سیسوگ - Sisoog

  • Pingback: میکروکنترلر STM8 چیست و از کجا آمده است؟ - سیسوگ - Sisoog


    I have been working through your articles and trying to understand as much as possible. I am currently looking at the AWU,

    I have brought in the project, compiled and built with no errors. 🙂

    However when running the program the CPU goes into the halt mode, and never Auto wakes up! 🙁

    Is there some more work that has to be done in the interrupt side of things? Does the project need the ITC.c and ITC.h files and some adjustments in there?

    Could I also have the delay.c and delay .h files please.


    • By the way, I am using the STM8SVL Discovery with the 003K processor on it also so all hardware is identical.

    • No, nothing needs to be done in the interrupt “itc” files but you need to add some lines in the “it” interrupt files:

      in the “it” header file add the following line:

      @far @interrupt void AWU_trigger_IRQHandler(void);

      and in the “it” source file add the following lines:

      void AWU_trigger_IRQHandler(void)

      Change the interrupt vector file as shown below:

      #include "stm8s_it.h"

      typedef void @far (*interrupt_handler_t)(void);

      struct interrupt_vector {
      unsigned char interrupt_instruction;
      interrupt_handler_t interrupt_handler;

      //@far @interrupt void NonHandledInterrupt (void)
      /* in order to detect unexpected events during development,
      it is recommended to set a breakpoint on the following instruction
      // return;

      extern void _stext(); /* startup routine */

      struct interrupt_vector const _vectab[] = {
      {0x82, (interrupt_handler_t)_stext}, /* reset */
      {0x82, NonHandledInterrupt}, /* trap */
      {0x82, NonHandledInterrupt}, /* irq0 */
      {0x82, AWU_trigger_IRQHandler}, /* irq1 */
      {0x82, NonHandledInterrupt}, /* irq2 */
      {0x82, NonHandledInterrupt}, /* irq3 */
      {0x82, NonHandledInterrupt}, /* irq4 */
      {0x82, NonHandledInterrupt}, /* irq5 */
      {0x82, NonHandledInterrupt}, /* irq6 */
      {0x82, NonHandledInterrupt}, /* irq7 */
      {0x82, NonHandledInterrupt}, /* irq8 */
      {0x82, NonHandledInterrupt}, /* irq9 */
      {0x82, NonHandledInterrupt}, /* irq10 */
      {0x82, NonHandledInterrupt}, /* irq11 */
      {0x82, NonHandledInterrupt}, /* irq12 */
      {0x82, NonHandledInterrupt}, /* irq13 */
      {0x82, NonHandledInterrupt}, /* irq14 */
      {0x82, NonHandledInterrupt}, /* irq15 */
      {0x82, NonHandledInterrupt}, /* irq16 */
      {0x82, NonHandledInterrupt}, /* irq17 */
      {0x82, NonHandledInterrupt}, /* irq18 */
      {0x82, NonHandledInterrupt}, /* irq19 */
      {0x82, NonHandledInterrupt}, /* irq20 */
      {0x82, NonHandledInterrupt}, /* irq21 */
      {0x82, NonHandledInterrupt}, /* irq22 */
      {0x82, NonHandledInterrupt}, /* irq23 */
      {0x82, NonHandledInterrupt}, /* irq24 */
      {0x82, NonHandledInterrupt}, /* irq25 */
      {0x82, NonHandledInterrupt}, /* irq26 */
      {0x82, NonHandledInterrupt}, /* irq27 */
      {0x82, NonHandledInterrupt}, /* irq28 */
      {0x82, NonHandledInterrupt}, /* irq29 */

      Sorry for the trouble…. I probably forgot to state these in the code and explanation segments…. Will be updating the post…. Thanks for this catch…. 🙂

  • Hey,
    Grateful for the tutorial! Nice work.
    I tried this section of your tutorial and got 33 errors after building:
    it was due to commenting of :

    @far @interrupt_handler_t void NonHandledInterrupt (void)

    because these were used in the interrupt vector struct again. I uncommented those lines and got one error:
    “unknown space: interrupt_handler_t”

    what should i do now?
    please help me!
    Thanks alot.

  • Hi,
    Firstly, thanks alot for your tutorials. It helps alot for beginners in STM8S because there are not much documentation for it.
    Secondly, I have been trying to implement the Timer interrupt tutorial(pg. 19) and wrote the code as instructed, however I am getting this error of “invalid pointer initializer” when I added the interrupt
    {0x82, (interrupt_handler_t)TIM2_IRQHandler}, /* irq13 */

    in the interrupt_vector.c file as was instructed in your tutorial. Can you please help me around this issue? What am I doing wrong here? Any suggestions would be of great help.

  • Hi

    Thank you for the post,
    I ve used your code for another sensor and noticed that in the i2c read routine you’ve forgot to send a stop to release the clock line. Modification is as follows



    unsigned int BH1750_read_word(void)
    unsigned long value = 0x0000;
    unsigned char num_of_bytes = 0x02;
    unsigned char bytes[2] = {0x00, 0x00};



    I2C_Send7bitAddress(BH1750_addr, I2C_DIRECTION_RX);

    if(num_of_bytes == 0)

    bytes[(num_of_bytes – 1)] = I2C_ReceiveData();

    /// the following line should be added



  • Hello Sir,
    I am trying to compile the code of led blink. It gives me error like this

    Compiling main.c…
    cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c
    #error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:106(13) missing enumeration member
    #error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:113(13) missing enumeration member
    The command: “cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    • Can you help me?

      • Why are you adding ADC2 header and source files? Are you not using STM8S003?

        • stm8s_conf.h this file i am added from ADC2

          • Add only the hardware files that your target chips supports…. For example, you cannot add ADC2 for STM8S003…. This is so as STM8S003 doesn’t have these hardware blocks…. Likewise if you are using STM8S207, you cannot add UART2 files for the same reason…. If you don’t add required files then you will get errors…. You’ll also get errors if you add files that are not applicable for your target MCU….

  • Hi
    I am using stm8s003f3 pin out board and with this code I am just testing output at micro controller pin 2 i.e. TX
    I am testing this using an oscilloscope.
    but not able to get any output
    Can you please help?


  • SSI_Electronics

    I have followed the code as per the explanation.
    but I’m getting garbage values on serial port.
    Can you suggest any debugging steps??
    Thanks in advanced.

    PS: Great tutorial. It’s helping me a lot.

    • 1. Check wiring for loose connections….
      2. Use a stable and noise-free power source like a battery-bank to power up the board…. Noise from PC port may cause unwanted issues….
      3. Use CP2102 or FT232-based USB-TTL converter…. Don’t use PL2303-based ones because most are clones or fakes….
      4. Use a short USB cable….
      5. Use external pull-up resistors for both TX and RX pins….
      6. Try with another board if these don’t solve the issue…. It should not behave as you are saying because the code has no issues….

  • santosh kharade

    Serial receiving program is not working

  • Hello sir
    Do you tell me how generate pwm in pin no. B5 & B5
    in Stm8 board

    • Which STM8 chip? Because different chips have different pin mapping and sometimes there are alternative pins for such purposes…. Check with STM8CubeMX if that pin is available for that purpose….

  • Santosh Kharade

    i have done code for multi channel adc but its not working ????
    when i use single channel adc its working properly…………

  • Hello guys
    here i’m again for ask your help. I wrote a very easy program but when i try to send to stm8 the light programmer give me this error :

    >Erase not feasible
    Error: Device not recognized !

    some idea ?
    Thank you

  • First thank you so much for this wonderful tutorial, i’m new with STM8 i follow all you steps but i still have a problem and here i’m please can you help me this is the error message.
    Thank you

    ———– Project test_prov – STM8 Cosmic – Configuration Debug ————-

    Compiling main.c…
    cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c
    #error cpstm8 gpio\inc\stm8s.h:74 “Please select first the target STM8S/A device used in your application (in stm8s.h file)”
    The command: “cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    test_prov.elf – 2 error(s), 0 warning(s)

  • Hi Shawon ,
    Excellent article on stms8.
    within 3 days i was able to understand STMS8 very clearly.
    can you send me delay library files please.
    i tried to create my own without any success.
    warm regards

  • hi sir now also getting problem in compilation time …..’

    so plz can u send me the soft copy of software wat properly working at u…??

    my emain id…

    • I’m using the same software you are using – the latest one out there…. and so someone sending it has no meaning…. I suggest that you uninstall it and give it fresh retry, following all the steps carefully…. No one can help you with licensing issue unless to do it correctly….

  • hi geting poroblem while click on programmer ten my softare automatically close tab

  • hi sir…

    i getting problem for license configured……………. at compile time getting error like that…………. plz help out

    #error cpstm8 C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\HSTM8\mods0.h:1 FlexLM Cannot find license file.
    The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    alpha_lcd.elf – 3 error(s), 0 warning(s)

  • helo…we hav stm8svl discovery..i need embedded c program for the glowing of led,connecting ir receiver and also powersupply…for connecting these things i need code can u please guide me

  • Thanks for this article..
    I am looking for microsd card interface with STM8s discovery board. can you please provide the tutorial for microsd card?

  • In relation to disabling unused peripheral clocks as suggested by you:

    I found the following list of peripherals in the STM8S105 Discovery Board: Advanced control timer (TIM1), General-purpose timers (TIM2 and TIM3), Basic timer (TIM4), SPI, I2C, UART2, Window WDG, Independent WDG, ADC1,
    AWU timer, Beeper.

    Is this all?

    I could not find the clock peripheral configuration function for Window WDG, Independent WDG, Beeper in the Standard Peripheral Library.
    Is it not required?

    P.S. I am new to MCU programming with a little experience on Arduino. So please forgive me if the question is foolish.

    • 1. STM8S003K3 Discovery – the one I use for the blog is different from STM8S105…. They have different peripherals and so there will be differences in peripheral clocks…. Please check the device datasheet/STM8CubeMX for available hardware….

      2. WWDG and IWDG are set in option bytes and they don’t use peripheral/CPU clock…. Their oscillator is separate…. Due to these they don’t have clock configurations like other peripherals….

      P.S: No question is a noob question…. every question is allowed no matter how silly it may look….

  • Hi please someone tel me where can i find stm8s_delay.h and stm8s_delay.c

  • Hi , please someone tel me . Where can i find stm8s_delay.h and stm8s_delay.c
    Thanks ..

  • i am using stms003f3p6 controller..i have done as u told exactly but tim1 input capture is not displaying any value …according to your article it should display 10…i dont have any errors everything is fine…pulse on tim2 is ok is coming as u said…but capture not working..please help me …thank you…

  • Hi Sir,
    i have just follwed your instruction provied in yout tutorial in main.c

    #include “stm8s.h”

    while (1);

    This error comes, can you please check why this error come, i am new and learn the controller programming.

    The command: “cxstm8 -i”d:\other datasheet\new folder\lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    main.o – 2 error(s), 0 warning(s)


  • Hi there, i solve the previous ADC problem. Anyway, u make some good tutorial on stm8 chips. Nice work

    • What was causing that issue? How did you solve it?

      • because i put all these

        ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
        ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
        ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
        ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
        ADC_Cmd(ADC1, ENABLE);
        ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);

        in ‘while(1)’. It keeps looping.

        Once i put it before ‘while (1)’, it becomes normal.

  • Hi there, thanks for sharing. I learn alot thru your example, but I have encounter a problem with adc. Im currently using stm8l151k4t6. I wanna use the pin D7 as to read the voltage from my battery and monitor it (display on LCD). Can u please check where it goes wrong on my code?

    ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
    ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
    ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
    ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
    ADC_Cmd(ADC1, ENABLE);
    ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    battery = ADC_GetConversionValue(ADC1);

  • hi shawon great tutorials it would be nice too if you covered the coide compiler for arm it’s a good free compiler

  • hi shawon thanks for your response and support it turns out that my problem was a license issue I stupidly thought i use one license on multiple pc’s I used the license for a particular computer for mine that’s why I’ve been having compilation issues you have to use the license file stm8 sends you that is generated by your own pc else it won’t compile it’ll jus kip throwing you errors

  • It seems that you didn’t follow my instructions completely…. I found some issues:

    1. You have included header and source files of ADC2, CAN, UART2, etc which are unavailable in STM8S103…. Add only files for the peripherals available in your target MCU…. Exclude the rest….

    2. You have not included stm8s_delay header and source files…. Either disable it from stm8s_conf.h header file if you are not going to use it or add both the header and the source files for it in your project….

    Resolve these and you are good to go….


  • I haven’t compiled any of the examples yet I just tried to compile the main source file just for test purpose n it just keeps showing me the same errors

  • yes the comic compiler was installed properly i even received a license file from my mail with the instructions to copy the license file to the license folder in the install directory if you fail to do this it will keep popping up for you to put in the license file. iincluded all the source files and header files as instructed i am working with the cheap stm8s103f i uncommented the stm8s103f in the stm8.h header file as you instructed and just to compile the main file it just throws me errors

  • no I just followed your instructions I just compiled it after setup i did not write any code yet just compiled the default main file

    • Need more details….

      1. The version of STVD and Cosmic
      2. Which example is giving this issue?
      3. The chip you are using if it other than STM8S003
      4. Have you tried to compile something else other than my examples?
      5. Are the paths to libraries and other folders properly added?
      6. Is the Cosmic compiler registered properly?

  • it throws this line The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

  • hi I’m having compilation issues when I compile it just throws me error about comic compiler located in the program file ialso excluded unwanted header files but my problems still weren’t solved

  • hi thanks for the tutorials but i am having compiler error issues even when I remove unnecessary header files it say comic compiler error

  • I am new to STM8S003k3 Discovery board.I have done UART communication using STM standard peripheral library example. Now I want to define any other GPIO pin for UART communication. Is it possible to use d0 and d1 pin for the UART communication? If yes, then please let me know how?

  • thank you very much for this article!

  • Thank you for your time and effort.

  • Good job bro

  • Thank you for this exceptionally comprehensive article, much appreciated


Leave a Reply to Shawon Shahryiar Cancel reply

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