Category Archives: ARM Microcontroller

STM8S Discovery LED connected to PD0

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.