Author | strongerHuang
WeChat Official Account | Embedded Column
Embedded Column
1
I wonder how much everyone understands about procedural programming and object-oriented programming?
C language is a procedural programming language, while C++ is an object-oriented programming language. In the context of this article, procedural programming generates smaller code size (bin files) and has higher execution efficiency compared to object-oriented programming.
Therefore, C language has a smaller code size (bin files) and faster execution speed compared to C++.
Of course, this is in comparison to C++. In fact, assembly language has an even smaller code size and faster execution speed compared to C. You can refer to the article: What Are the Differences Between Writing LED Blinking Programs in Assembly and C Language?
Since the RAM and Flash resources of microcontrollers are relatively small and their execution speed is also lower, you will find that very few people use C++ for project development on microcontrollers.
Actually, with the increase in storage resources and execution speed of microcontrollers, some engineers are starting to use C++ for microcontroller projects.
Embedded Column
2
In the Keil MDK development environment, using C language to develop STM32 is quite common. The steps for installing the development environment, creating projects, etc., will not be discussed here. Please refer to my articles:
This is the basic tutorial for Keil. If there are students who do not understand, you can reply with the keyword “Keil Series Tutorial” in the backend of my official account to read more about using Keil.
Next, you need to understand some basic syntax of C++. The content discussed in this article is quite basic, using fundamental C++ knowledge, such as classes and objects.
If you have never learned C++, that’s okay. As long as you understand C language, you can easily learn basic C++ knowledge online.
Embedded Column
3
In the Keil MDK environment, the ARM compiler (Arm Compiler) is used. Many online tutorials use V6 compilation; in fact, both V5 and V6 versions support C++ programming language.
Add C++ source code to the project (for example: main.cpp).
Using V5 and V6, there are some differences in the project configuration options:
The development library from ST has already provided support, and you will see a piece of code like this:
#ifdef __cplusplus extern "C" {#endif
//C source code here
#ifdef __cplusplus}#endif
What does this mean?
This is a piece of preprocessor code. From the preprocessing code, we can see that the general meaning is: support for mixed programming in C and C++.
Embedded Column
4
This article assumes that everyone has mastered the knowledge of developing STM32 LED blinking with C language, and will directly describe the C++ code content.
This article discusses a very basic example “LED Lighting”. There are many methods to implement it in C++, and here I will present one of the basic methods.
First, create a source code file named main.cpp and define an LED class:
class LED_Class{}
Then define private members (of course, public can also be used):
class LED_Class{private: GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin; uint32_t RCC_APB2Periph;}
Next, the functions used: Initialize GPIO, turn on, and turn off the LED, etc.
class LED_Class{private: GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin; uint32_t RCC_APB2Periph;
public: LED_Class(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin, uint32_t RCC_APB2Periph){ LED_Class::GPIOx = GPIOx; LED_Class::GPIO_Pin = GPIO_Pin; LED_Class::RCC_APB2Periph = RCC_APB2Periph; }
void Init(void){ GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOx, &GPIO_InitStruct); }
void Open(void){ GPIO_SetBits(GPIOx, GPIO_Pin); }
void Close(void){ GPIO_ResetBits(GPIOx, GPIO_Pin); }};
Embedded Column
5
The thought process in C++ is similar to that in C: first initialize, then implement:
int main(void){ LED_Class LED1(GPIOF, GPIO_Pin_7, RCC_APB2Periph_GPIOF); LED_Class LED2(GPIOF, GPIO_Pin_8, RCC_APB2Periph_GPIOF);
LED1.Init(); LED2.Init();
while(1) { LED1.Open(); LED2.Open(); Delay(10);
LED1.Close(); LED2.Close(); Delay(10); }}
There is also another way to initialize:
int main(void){ LED_Class *LED1 = new LED_Class(GPIOF, GPIO_Pin_7, RCC_APB2Periph_GPIOF); LED_Class *LED2 = new LED_Class(GPIOF, GPIO_Pin_8, RCC_APB2Periph_GPIOF);
LED1->Init(); LED2->Init();
while(1) { LED1->Open(); LED2->Open(); Delay(50);
LED1->Close(); LED2->Close(); Delay(50); }}
The compiled code size is relatively larger:
Then, if you have a development board, you can directly download it to run the LED blinking phenomenon.
Note:
There are many methods to implement the LED blinking program using C++. For example, there are more advanced uses like inheritance and polymorphism that can also be introduced. The above is just one approach, guiding beginners in learning C++ programming.
Isn’t it simple? By now, have you learned something?
Follow the official account 【Embedded Column】, reply with ‘Embedded Software Design and Development’ ‘STM32’‘Embedded C Language’ to read more related articles.

You can add WeChat 17775982065 as a friend, indicating: Company + Name, and be added to the RT-Thread official WeChat group!