Why C++ is Rarely Used in Microcontroller Development?

Author | strongerHuang

WeChat Official Account | strongerHuang

When it comes to embedded programming, many people’s first impression is C/C++. However, you will find that microcontroller development is mostly done in C, with very few using C++.

So, can C++ be used for microcontroller development? The answer is definitely yes.

Based on Keil and STM32, this article will describe how to light up an LED using C++ programming for beginners, and then you will understand why C++ is rarely used for microcontroller development.

Why C++ is Rarely Used for Microcontroller Development

How well do you understand procedural programming and object-oriented programming?

Why C++ is Rarely Used in Microcontroller Development?C is a procedural 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 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 a smaller code size and faster execution speed compared to C.Since microcontrollers have relatively small RAM and Flash resources, and their operating speed is also relatively low, you will find that very few people use C++ for microcontroller projects.Actually, as the storage resources and operating speeds of microcontrollers have increased, some engineers have started to use C++ for microcontroller projects.

Preparation Work

In the Keil MDK development environment, using C language for STM32 development is quite common. This article will not cover steps like installing the development environment or creating projects; please refer to my articles:Keil Series Tutorial 01: Introduction, Download, Installation, and RegistrationKeil Series Tutorial 02: Creating a Basic Software Project

This is a 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 WeChat account to read more about using Keil.

Next, you need to understand some basic C++ syntax. The content discussed in this article is quite basic and uses fundamental knowledge of C++. For example: classes, objects, and other basic concepts.

If you have not learned C++, it’s okay; as long as you understand C language, you can easily learn the basics of C++ online.

Usage Instructions

In the Keil MDK environment, the ARM compiler (Arm Compiler) is used. Many online tutorials use V6 compilation, but both V5 and V6 versions support C++ programming language.

Add C++ source code to the project (for example: main.cpp)

Why C++ is Rarely Used in Microcontroller Development?

Using V5 and V6, there are some differences in the project configuration options:

Why C++ is Rarely Used in Microcontroller Development?

ST’s development libraries have already provided support, and you will see a piece of code like this:

#ifdef __cplusplusextern "C" {#endif
// C source code here
#ifdef __cplusplus}
#endif

What does this mean?

This is a piece of preprocessor code, which indicates that it supports mixed programming in C and C++.

Defining the LED Class

In this article, it is assumed that everyone has mastered the knowledge of developing a running light with STM32 using C language, so we will directly describe the C++ code content.

This article discusses a very basic example, “LED Lighting”, and there are many methods to implement it using C++. Here, we will present one of the basic methods.

First, create a main.cpp source code file and define an LED class:

class LED_Class {}

Then define private members (public can also be used):

class LED_Class {private:  GPIO_TypeDef *GPIOx; uint16_t GPIO_Pin; uint32_t RCC_APB2Periph;}

Next, we need to define the functions used: initializing GPIO, turning the LED on and off, 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);  }};

This code is quite simple; even if you do not know C++, as long as you understand C language, you should be able to comprehend it.

Running Light Implementation (main function)

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:

Why C++ is Rarely Used in Microcontroller Development?

Then, if you have a development board, you can directly download and run to see the LED blinking phenomenon.

Note:

There are many methods to write a microcontroller running light program in C++, such as using more advanced inheritance, polymorphism, etc. This is just one approach to guide beginners in learning C++ programming.

Isn’t it simple? By now, have you learned something?

END

Why C++ is Rarely Used in Microcontroller Development?Lastly, I would like to mention that the 21ic Forum (bbs.21ic.com) is recruiting original authors, with a maximum reward of 500 yuan for each article. We welcome everyone to actively submit! Why C++ is Rarely Used in Microcontroller Development? Click to learn more about the event.

Previous Highlights:

  • Breaking! BYD Found Guilty of Infringement, Facing Technical Ban

  • Hikvision Banned, Latest Official Response!

  • Huawei Denies Plagiarism, Pangu Employee Reveals Development Secrets Late at Night: Shell Wrapping, Continued Training, Watermark Removal

Why C++ is Rarely Used in Microcontroller Development?Scan the QR code to follow the video account

Please click 【♡】 to give the editor a thumbs up

Why C++ is Rarely Used in Microcontroller Development?

Leave a Comment