EasyManuals Logo

STMicroelectronics STM8 User Manual

STMicroelectronics STM8
126 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #123 background imageLoading...
Page #123 background image
Definitions and constants can be declared as with any C compiler.
Wherever there are flags, you need to be careful. You should check and clear flags even if it is
cleared by hardware. For instance, when reading ADC, the ADC End-Of-Conversion (EOC) flag
is automatically cleared but still in the code you should check and clear it.
ADC1_ClearFlag(ADC1_FLAG_EOC);
Flags are so important that unless you check and clear them appropriately, you may not get
the right result from your code. Personally, I didn’t care much until I got myself into trouble.
You can mix assembly codes with your C code to enhance performance and optimization.
However, you need to have sound knowledge of the assembly instructions. This is a rare
requirement. The delay library, for instance, uses no operation assembly instruction to
achieve delays. This is written as shown:
_asm ("nop");
Empty loops are ignored by the compiler as a part of code optimization.
Long ago, Atmel (now Microchip) published a document regarding ways to efficiently optimize
C coding. This document holds true for most microcontrollers. For example, in that document
it is stated that a decrementing do-while loop is much more faster and code efficient than an
incrementing do-while loop. You can apply the methods presented there and other similar
tricks with STM8 microcontrollers too. The document can be found here:
http://www.atmel.com/images/doc8453.pdf
Though I don’t feel it as a necessity and don’t recommend it, you can avoid using the SPL and
still code by raw register level access. For example, you can blink a LED with the following
code:
#include "stm8s.h"
void main (void)
{
GPIOD->DDR |= 0x01;
GPIOD->CR1 |= 0x01;
for(;;)
{
GPIOD->ODR ^= (1 << 0);
delay_ms(100);
};
}
As you can see it is both tedious and meaningless unless you comment every single line.
Don’t mess with configuration (fuse) bits unless needed. Most of the times you will never have
to deal with them. AFRs are used when remapping is needed or to enable special GPIO
functionality. These will be of most importance.

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the STMicroelectronics STM8 and is the answer not in the manual?

STMicroelectronics STM8 Specifications

General IconGeneral
BrandSTMicroelectronics
ModelSTM8
CategoryMicrocontrollers
LanguageEnglish

Related product manuals