STM8S Tutorials
STM8S Tutorials
As we progress in this tutorial series we will come across almost all the main features of this 8-Bit controller. In this tutorial series
we will focus mainly on STM8S series which is a medium code density member of STM8 family.
ST microelectronics provides rich tech support and evaluation platforms for a quick start. Most easy way to start with STM8
microcontroller is to grab aSTM8S Discovery board which is a low cost development board with all the necessary features to kick-
start application development using STM8 mcu.
STM8S Features
2 KByte RAM
Internal RC Oscillator
1, 8 Bit Timer
This series guides you to get started with your STM8S Discovery board. Subsequent posts on STM8S tutorial series will guide you
on further programming and interfacing with this feature rich 8-Bit controller. This particular post lists the necessary hardware and
software required to start application development.
Software Tools
These are minimum possible software tools required to start with STM8 Controllers in Windows OS. Go to following pages to
download required installation file.
1. STVD [ST Visual Develop IDE] IDE for compilation, debugging and project management
Thats it, thats all you need to start application development using STM8S controller. Install these 3 software in your windows
based computer. [PS - Development tools for Linux based machines will be updated soon].
Once all three software packages are installed, next step is to configure STVP and STVD to work with STM8S Discovery Board.
STVP Configuration
To configure STVP to program STM8S105C6T6 controller on Discovery board go to Menu bar in STVP and choose
STVD Configuration
STVD is a great tool, it is used for efficient project management, debugging and overall IDE for STM8 controllers, but remember
STVD is not a C compiler, to develop application programs using C language we need a C compiler that understands the
language of STM8S internals and does all the cross compilation for STM8S. Cosmic C compiler is one such compiler. Cosmic
compilers size limited evaluation version is free for download at the link given in software tools section of this post. Once both
STVD and Cosmic compiler are installed in system it is necessary to link the path of cosmic C compiler with STVD. So that, STVD
knows where to look at, when it need to compile any C file.
Go to STVD-> Project->Settings
Once configuration of STVP and STVD is done we are now ready to write our first firmware for STM8S105Cx MCU.
STM8 Tutorials #2 Hello World Program
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.
1 #include "stm8s.h"
2 void myDelay(void);
3
4 void myDelay()
5 {
6 int i,j;
7 for(i=0;i<1000;i++)
8 {
9 for(j=0;j<100;j++);
10 }
11 }
12
13 main()
14 {
15 GPIOD->DDR |= 0x01; // PD.0 as Output
16 GPIOD->CR1 |= 0x01; // PD.0 as Push Pull Type Output
17 while (1)
18 {
19 GPIOD->ODR |=1<<0; // PD.o = 1
20 myDelay();
21 GPIOD->ODR &= ~(1<<0); // PD.0 = 0
22 myDelay();
23 }
24 }
After opening the .s19 file, choose Program current tab from STVP Menu options.
STVD after successful device programming
Reading input on any of the STM8S GPIO pin is very straight forward. Lets take this step by step.
One has to declare any GPIO pin as input before any physical signal input can be read from that GPIO pin. To declare the GPIO
pin as Input we again need to set proper bits in DDR Register of corresponding port. Table below shows different combinations of
DDR, CR1 & CR2 registers in order to set any GPIO either as Input or Output and to enable or disable Pull ups and Interrupts.
In this experiment we are going to use GPIO as Input with Pull up but without Interrupts. So, for example if I want to use PE.7 as
input with Pull up enabled and Interrupt disabled. I need to write corresponding bit (bit 7) in DDR register of PORTE as 0.
1 GPIOE->DDR &= ~(1<<7); // Declare PE.7 as Input
1 GPIOE->CR1 |= 1<<7; // PE.7 Pull Up Enabled
Now, once we have set PE.7 as Input with Internal Pull-Up, we are now ready to read the input logic status at PE.7 Pin.
STM8S has ODR register to write on output port and IDR register to read from input port. IDR is a read-only 8-bit register. Logical
value at any bit of IDR register of a port, reflects the actual logical status of that pin of that port. For example, if Pin 7 of
IDR register of GPIOE is 0, that means, PE.7, if configured as input is currently set to logic 0.
Step 3 Schematic
Figure below shows the connections that Ive made to experiment with Inputs. Circuit has a tactile switch connected to PE.7
and 3 LEDs connected to PC.2, PC.4 and PC.6 respectively.
STM8S105_Input_GPIO_Tutorials_Schematic
Step -4 Code
below is main() function for input handling program. Whenever the user presses the switch at PE.7, the input pin
goes low, the code has a software wait for switch debounce. As the user presses the switch, it will glow all the LED
for approximately 1 second otherwise the LEDs remain off forever.
STM8S_input_handling_code