Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to write embedded code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use C++ to write embedded code". In daily operation, I believe many people have doubts about how to use C++ to write embedded code. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "how to use C++ to write embedded code"! Next, please follow the small series to learn together!

1. Declaration and definition of registers:

Access to registers in MCU is a basic function developed by mcu. Traditional C programs use macro and AND or NOT operations to access registers. The reason why they do not use bit fields is because:

Code migration (different compilers may produce different endianments) can be avoided if only one compiler is used.

Problems such as non-atomic operations in multitasking, for example, three-step read-rewrite operations performed when assigning bit fields, resulting in data synchronization in multitasking systems; but in traditional code, if REG is used| = (1 0) This form of code causes the same problem.

But there are some benefits to using bitfields:

Simple variations of encapsulated registers, e.g. different memory layouts for the same register structure.

Assignment operations can be accessed without worrying about data size.

Encapsulated bit information, no need to introduce different header files according to chip type when defining templates

Can be passed as a template parameter. For example:

struct PORTA_t { byte reserved_0[57]; byte PIN; //! < Port A Input Pins byte DDR; //! < Port A Data Direction Register byte PORT; //! < Port A Data Register};

Device templates, using bitfields as template parameters. Take port devices as an example:

templateclass Port {public: static void init(const byte mode); static void write(const bit hl); static byte read(); static void toggle();private:};

Template parameters are the type of port register and its instance. After defining the template instance, you can use it, such as:

using led1 = Port;led0::init(PORT_MODE_OUTPUT);while(1) { led1::toggle();}

The template defined in this way

Good reusability, basically no code change in the same series of chips,

Simple to use, understand at a glance, more suitable for me and other fart people to use,

And using static template technology, just like traditional C, do not consider this pointer, there is no process of creating objects.

If efficiency is to be maximized, it is also possible to optimize each device using specialized techniques.

At this point, the study of "how to write embedded code using C++" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report