STM8 Tutorials – #2 Hello World Program

Link to Part #1 in this Series

Without much talks, lets create our first “Hello World” application for STM8S Discovery Board. As practiced in embedded world, this time also, LED blinking is our way to say Hello to STM8S world.

STM8S Discovery has an on-board Green Colored user LED connected to PD0. We will blink this led on and off with some delay in between.

STM8S Discovery LED connected to PD0

STM8S Discovery LED connected to PD0

STEP 1: STVD Workspace/Project Creation

Create a new STVD Workspace from File menu

STM8S STVD Project Creation Process

Step -1 Create Workspace

STM8S STVD Workspace Settings

Step -2 Project Name

 

Step -3 Project Toolchain selection as Cosmic STM8

Step -3 Project Toolchain selection as Cosmic STM8

Step -4 Choose STM8S105C6 as Target MCU

Step -4 Choose STM8S105C6 as Target MCU

Step - 5 Review Project File Tree

Step – 5 Review Project File Tree

STEP – 2 Adding STM8S Register Definition header stm8s.h

To compile any C project for STM8S can not be done without mapping register definition into suitable names. This file is very important and it will be there in all most all the upcoming projects. It maps register addresses with suitable names. For example GPIOA base register is at 0×5000, this file has definition to map 0×5000 to a name as “GPIOA”.

DDownload stm8s.h file here.
Add stm8s.h file into your project

Add stm8s.h file into your project

Verify that stm8s.h has been added in your project

Verify that stm8s.h has been added in your project

STEP – 3 Writing the Code

#include "stm8s.h"


void myDelay(void);

void myDelay()
{
   int i,j;
   for(i=0;i<1000;i++)
   {
       for(j=0;j<100;j++);    
       }  
}   

main()   
{            
     GPIOD->DDR |= 0x01;  // PD.0 as Output
     GPIOD->CR1 |= 0x01;  // PD.0 as Push Pull Type Output
     while (1)
     {
            GPIOD->ODR |=1<<0;  // PD.o = 1             
            myDelay();                          
            GPIOD->ODR &= ~(1<<0); // PD.0 = 0
            myDelay();
     }
}

STEP – 4 Compile and Build the Project

Compile and build the necessary machine files by pressing F7. If build process does not produce any errors, our code is successfully cross compiled and machine code is now ready to download on mcu program memory.

STEP – 5 Download the code onto MCU Program Memory

Now, open STVP and open .s19 file from the Debug folder inside project directory.

After opening the .s19 file, choose Program current tab from STVP Menu options.

After opening the .s19 file, choose Program current tab from STVP Menu options.

 

STVD after successful device programming

STVD after successful device programming

LED LD1 on Discovery board should be blinking now with a delay defined by mydelay() function. If you are facing any troubles in achieving desired out put in this case, you can put your issue in the comment section below.

About author

Devesh Samaiya

Chief Tinkerer at Lonely Night Projects

7 Comments

  1. vaibhav June 23, 2015 at 5:28 am

    no 3rd Tutorial :(

  2. Robothito April 14, 2016 at 4:29 am

    Nice tutorial
    but… I have ST Visual Develop and Cosmic but “stm8s.h” does not exist anywhere…

    Where is this file?
    I have completely different definitions on each chip particular h file (like “STM8S903K.h” )

    Can you help me with any comment?

  3. Amita October 5, 2016 at 10:04 am

    Nice. Waiting for your other projects like these.

  4. Rohit December 6, 2016 at 6:54 am

    GPIOD->DDR |= 0×01; // PD.0 as Output
    GPIOD->CR1 |= 0×01; // PD.0 as Push Pull Type Output

    What is the meaning of GPIOD, DDR, CR1, PD.0??
    Any guide for this ? sorry for such silly question but i am a newbie to STM8

    • Devesh Samaiya January 17, 2017 at 5:31 am

      GPIOD is a structure found in STM8S.h, DDR is the data direction register which specifies if the port is going to be used as output or input. CR is the control register which sets various aspects of the port and PD.0 indicates that we are dealing with 0th bit of PORTD. Refer to STM8S reference manual for more details on various registers.

  5. Hyuckjin December 15, 2016 at 1:19 am

    Compilation is fine. But the LED does not work.
    Stm8s.h is the library from st.

Leave a reply

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

− 1 = 1