Embedded development | Can microcontrollers be developed in C++?

Publisher:悠然自在Latest update time:2023-04-06 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Generally speaking, programming on a microcontroller requires either assembly or C language, and few develop in C++.


So can we use C++ to develop microcontrollers? The answer is definitely yes.


The following is based on Keil and STM32, using C++ programming to light up the LED to provide some ideas for beginners.


Why C++ is rarely used to develop microcontrollers

I wonder how much everyone understands process-oriented and object-oriented?


C language is a process-oriented language, and C++ is an object-oriented programming language. Based on this article, process-oriented programming has a smaller amount of generated code (bin file) and higher operating efficiency than object-oriented programming.


Therefore, C language has a smaller code size (bin file) and faster running speed than C++.


Of course, this is C relative to C++. In fact, assembly has a smaller code size and faster running speed than C. (You can refer to the article: What is the difference between using assembly and C language to light up the program?)


Because the RAM and Flash resources of the microcontroller are smaller and the running speed is relatively low, you will find that few people use C++ to develop projects on the microcontroller.


In fact, with the increase in the storage resources and running speed of microcontrollers, some engineers are now beginning to use C++ to develop microcontroller projects.


Preparation

In the Keil MDK development environment, it is more common to use C language to develop STM32. This article does not describe the development environment, installation, project creation and other steps. Please refer to my article: Keil Series Tutorial 01_Keil Introduction, Download, Installation and Registration Keil Series Tutorial 02_New basic software project


This is the basic tutorial of Keil. If you don’t understand it, you can reply to the keyword "Keil series tutorial" in the background of my official account to read more about how to use Keil.


Then you need to understand some basic syntax of C++. The content described in this article is relatively basic and uses very basic knowledge of C++. For example: basic content such as classes and objects.


It doesn’t matter if you have never learned C++. As long as you understand the C language, it is not difficult to learn the basics of C++ online.


Instructions for use

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


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


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

ST's development libraries have already provided support. You will see this piece of code:

#ifdef __cplusplus extern "C" {#endif

//C source code here

#ifdef __cplusplus}#endif


This is a piece of preprocessing, which can be seen from the preprocessing code. It roughly means: supporting mixed programming of C and C++.


Define LED class

In this article, it is assumed that everyone has mastered the knowledge of developing STM32 water lamps in C language and directly describes the C++ code content.


This article talks about a very basic example "LED lighting". There are many ways to implement it using C++ programming. Here is 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 (of course, public can also be used):


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

Again are the functions used: initializing GPIO, turning on and off LEDs, 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); }};

Isn’t this code very simple? Even if you don’t know C++, as long as you understand C language, I believe you can understand it.


Implementation of running water lamp (main function)

The idea of ​​using C++ is similar to that of C language. First initialize and 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 an initialization:


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 amount of compiled code is relatively larger:


Then, if you have a development board, download it directly to run the LED flashing phenomenon.

illustrate:

There are many ways to write a microcontroller flow lamp program using C++. For example, there are some more advanced inheritance, polymorphism and other usages that can also be introduced. The above just provides an idea to guide beginners to learn C++ programming.

Isn’t it very simple? Are you tired of learning after seeing this?


Reference address:Embedded development | Can microcontrollers be developed in C++?

Previous article:Use P89LPC922 chip to realize the design of intelligent instrument front panel
Next article:Common operations of EEPROM driver code

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号