In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to declare and define C++ device template". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn how to declare and define C++ device template.
To package the on-chip peripherals, you can use the following three technologies:
Use classes to define peripherals, and then create objects.
POD class implementation
Ordinary class
Static template (all members are static)
The common feature of these two templates is that there is no need to dynamically allocate memory, the difference is that one is static access member, the other is object method access, POD type can not add custom data members.
After declaring all the registers of a MCU, if it is a simple mcu, you can use it directly, for example: PORTA.DDR.v | = 0x01 or USART0.UCSRC.UPM = 0x00; but there are always some repetitive steps and related code that we can encapsulate, such as the configuration steps of asynchronous counters can be reused. C++ can use objects to encapsulate these operations, but unfortunately, compilers like avr do not support the generation and deletion of objects, and after defining classes, they cannot be used, because the delete operator and other basic class libraries are not defined, then you can customize or reference some third-party class libraries to solve these problems, if you do not want to reference third-party class libraries, then using static class templates is also a good choice. As follows:
Templateclass USART {public: struct SerialConfig {word baud = 115200; byte data = USART_DATABIT_8; byte stop = USART_STOP_1; byte parity = USART_PARITY_DISABLE;}; _ _ fills _ void init (const SerialConfig& conf) {.} _ fags _ hword calUBRR (const word baud) {.} / / other operations private:}
Template parameter transfer registers are used to improve code efficiency, base is to reuse templates, and _ _ fallows _ is a macro that defines function members as static. After defining the basic template, you can extend or specialize it. For example, during initialization, in addition to setting the USART function register, you also need to set the rx,tx pin. In this case, you can use the template specialization technology to achieve this:
Templatevoid u0::init (const SerialConfig& conf) {rx::init (INPUT); tx::init (OUTPUT); u0_base::init ((u0 simplified Baserx::init SerialConfig &) conf);} templatevoid u1::init (const SerialConfig& conf) {.} / / can be simplified again by always adding pin definitions to the template parameters.
The initialization functions of usart0h and usart1 can be added to the initialization of pins. U0_base is a reuse template. I don't have to write the basic initialization code of USART again. The method of use is as follows:
Using U0 = USART;u0::SerialConfig sc;sc.baud = 115200 * sc.data = USART_DATABIT_8;sc.parity = USART_PARITY_DISABLE;sc.stop = USART_STOP_1;u0::init (sc); u0::start () Thank you for your reading. the above is the content of "how to declare and define C++ device template". After the study of this article, I believe you have a deeper understanding of how C++ device template is declared and defined. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.