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?
Previous article:Use P89LPC922 chip to realize the design of intelligent instrument front panel
Next article:Common operations of EEPROM driver code
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Engineer, if you don't develop from the perspective of engineering or product, how can you be worthy of the word "engineering"...
- Understand the past and present of NFC in one article!
- When power saving is required, how should the LCD screen's SEG pin, COM pin, VLVD1-3, and VLCDH related configurations be configured?
- [New version of Bluesun AB32VG1 RISC-V development board] - 1: From "New" to "0 errors, 0 warnings."
- 1- Design of vending machine based on 51 single chip microcomputer (schematic diagram + PCB + paper + program)
- Advanced C/C++ Compilation Technology
- How to use multiple serial ports on CC2642R? Can 3 serial ports be used?
- Where are PCB professionals heading?
- Ubuntu development environment installation (I) system installation
- Implementation strategies for Industrial IoT networks