10300 - One Step Up

2011.06.30

One Step Up

There are some benefits in writing a program in assembly language. One of which is to train your logical thinking. But different microcontrollers have different assembly language. For applications, it is better to use a common high level language such as C. It is also easier for another person to read your program.

Writing a program in C involves more than the language itself. The power of the C language is increased by the use of preprocessors, such as the header files etc. Header files are usually required if the program contains many functions. Such functions may include drivers for the peripherals on the microcontroller. As the drivers are usually the same in any application, they can be pre-generated and put in a library. The STM8S105 manufacturer provides such a library, called the firmware library. The firmware library also helps the programmer in writing the program without having to memorize certain details of the peripheral, such as ‘0’ for input direction and ‘1’ for output. Although using the firmware library requires a steeper learning curve, it will improve the program development and knowledge in C language.

Let us move one step up.

To begin with, the following code turns on the on-board LED:

#include "stm8s.h"

int main( void )
{
    GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_SLOW);

    return 0;
}


Firmware Library

It is obvious that the program looks simple because it is making a function call. The function is contained in a file stm8s_gpio.c which has to be included in the project. The use of the function in stm8s_gpio.c is declared in the header file stm8s_gpio.h, which is included in the stm8s_conf.h file, which itself and stm8s_type.h file are included by the stm8s.h header. Thus to use the firmware library for GPIO’s, all these files must be included in the project explicitly or implicitly.

       stm8s.h       --> stm8s_type.h
               --> stm8s_conf.h  --> stm8s_gpio.h  --> stm8s_gpio.c

In addition, the stm8s_conf.h file should be edited to indicate that GPIO or other peripheral functions are to be used. It sounds more complicated than the classical approach. But once the procedure is mastered, it will be easier. At least, it helps to reduce programming errors or omissions.


Special Note

The firmware library does not work directly with the IAR software. The easiest solution is to use those provided by IAR where necessary. Up till now, three files may be necessarily from IAR: stm8s.h, stm8s_gpio.h and stm8s_conf.h.