Getting started with STM8 Development – Part 2 – Input handling

Now that you learned how to configure one of the pins of a STM8 micro-controller as output to blink a LED in the first part of STM8 basic tutorials, it is time to learn how to configure pins as input, so that you can react to things like button press or other input methods!

The learning management for government agencies is becoming increasingly important as the need for effective training and development programs grows. A learning management system (LMS) can provide a centralized platform for managing training content and resources, tracking learner progress, and assessing performance. With a government-focused LMS, agencies can ensure compliance with regulatory requirements and provide their employees with the necessary knowledge and skills to perform their jobs effectively. Additionally, a government-focused LMS can provide customized learning paths and assessments to meet the unique needs of different agencies and departments, helping to ensure that each employee receives the training they need to succeed in their roles.

A bit of facts

Now that the things have gotten a bit serious, I think it is a good time to introduce you to Pin Configuration Tables (taken from STM8S003 datasheet). Generally the GPIO or General Purpose Input Output pins on STM8 have 5 registers:

GPIO Registers
Data Direction Register – DDR
Control Register 1 – C1
Control Register 2 – C2
Output Data Register – ODR
Input Data Register – IDR

Generally the configuration of a GPIO pin involves performing of following steps:

  1. Reset the pin by setting ODR to 0
  2. If you want the pin to act as output, you should set DDR to 1, otherwise to act as input, set to 0
  3. Set the Control Register 1 and Control Register 2 according to the table below, deepening on what you need.
Register Mode Value Description
CR1 Input 0 Floating input
CR1 Input 1 Input with pull-up
CR1 Output 0 Open drain
CR1 Output 1 Push-Pull
CR2 Input 0 Interrupt disabled
CR2 Input 1 Interrupt enabled
CR2 Output 0 Output up to 2 MHz.
CR2 Output 1 Output up to 10 MHz

To get the idea, please check the header file of your STM8 series. In our example we are using STM8S003F3 so the header file is “iostm8s003f3.h”. As an example, the part of code which has the definitions for PortD in this header file is:

Back to handling button press

First thing you need is obviously a button of some sort. For this project I am using a mechanical button that I have salvaged from some old equipment. The important thing is that you need to take the bounce effect into account. The simplest and must naive method of  implementing  de-bounce for the button is to add a 10nF capacitor in parallel with the button. This is in no way perfect, but will let us test the code we are going to write! Please note the polarity of the capacitor which you are going to use (if it is a electrolyte capacitor). In my case, I am using a ceramic capacitor so the polarity does not matter!

button with debounce capacitor

After adding the capacitor in parallel, and soldering some wires and header (if necessary). All is left is to write the code. I just use the code from part 1 of tutorial, but in this case there is no need to have a delay function. Please note that I have connected one pin of the button to PortD, Pin 6 (PD6) and the other pin of the button is connected to ground.

The required explanation is added as comment in the code snippet above. But like always if you had any problem or did not get the idea, just leave a comment!

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!

10 Comments:

  1. Mohammad Hemmati

    This Tutorial is also useful and gives an abstract of GPIO functions
    I appreciate it.

  2. thank you so much for this instruction

  3. Why do not you continue to write tutorials my friend?

  4. For certain reason I should work with STM8 (in Linux) and I find your web very interesting. Congratulations.
    Just in case it is useful for you, I discovered that this guy (https://github.com/gicking) has made a few interesting things:
    -Patch to port the STM8 StdPeriph Libraries to SDCC
    -STM8 USB Serial flashing
    -Template library (not only for SDCC)
    I just tested the patch and it works ok.

  5. hola. mi me cuando pulso el interruptor(cierro el circuito) se apaga como es para que funcione al revés

  6. Can you please help me reading analog values from the sensor which i have.
    Thank you

Leave a Reply to hesan621 Cancel 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.