Application of Enumeration in MCU Programming

Publisher:数字梦想Latest update time:2017-08-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When do we need to use enum? When the value of a variable is between several ranges, such as a week variable that can only take values ​​of Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. It is better to use enum in this case. Of course, define can also be used. However, define is more troublesome to maintain and is suitable for when the value range of the variable is small.

1. Using the enum keyword to describe constants (i.e., describing enumeration constants) has the following advantages: 
(1) It makes the program easier to maintain and more intuitive. For example, the enumeration defined below describes the number of transmission bits supported by the UART. From the definition of the enumeration type, we can know the several modes of transmission bits provided by the UART module.

enum
{
          usartDatabits4 = USART_FRAME_DATABITS_FOUR, /* 4 databits (not available for UART). */
          usartDatabits5 = USART_FRAME_DATABITS_FIVE, /**< 5 databits (not available for UART). */
          usartDatabits6 = USART_FRAME_DATABITS_SIX, /**< 6 databits (not available for UART). */
          usartDatabits7 = USART_FRAME_DATABITS_SEVEN, /**< 7 databits (not available for UART). */
          usartDatabits8 = USART_FRAME_DATABITS_EIGHT, /**< 8 databits. */
          usartDatabits9 = USART_FRAME_DATABITS_NINE, /**< 9 databits. */
          usartDatabits10 = USART_FRAME_DATABITS_TEN, /**< 10 databits (not available for UART). */
} USART_Databits_Enum;

(2) Make the program safer 
. For example, when writing the underlying driver, a UART initialization function UartInit(uint32 baudrate, uint32 bit) is provided to the upper layer. When calling this function, the programmer may provide the wrong bit parameter because he does not understand the transmission bit mode supported by the module UART. To avoid this phenomenon, we can of course check the bit range in the UartInit() function. If the parameter values ​​are continuous, it is fine. If they are not continuous, multiple if-else or switch will be used. It will consume a certain amount of ROM and CPU time. 
We can define the function as UartInit(uint32 baudrate, USART_Databits_Enum bit). On the one hand, we can easily know the transmission bit mode supported by UART by checking the USART_Databits_Enum enumeration type. On the other hand, we can avoid passing wrong parameters during compilation.

2. The difference and connection between enumeration and macro definition:

The difference between macro and enumeration lies in the period of action and the form of storage. Macro is replaced in the preprocessing stage, it replaces the text of the code segment, and the macro no longer exists during the program running. Enumeration takes effect after the program is running, and the enumeration constant is stored in the static storage area of ​​the data segment. (In the IAR compilation environment, the enumeration type defaults to character type. In programming under the M3 core, in order to reduce unnecessary character extension instructions, you can use the ?enum_is_int instruction to force all enumeration types to be 4 bytes.)

But we cannot say that macros are better than enumerations. If you need to define a lot of constants, using one enum {…} is obviously clearer than a bunch of defines. Enumerations can also be used to define some special types, such as Bool, such as: type enum {FALSE,TRUE} Bool;


Reference address:Application of Enumeration in MCU Programming

Previous article:My understanding of microcontrollers
Next article:C language pointers and variables in single chip microcomputer

Recommended ReadingLatest update time:2024-11-15 12:06

STM32 MCU--External Interrupt EXTI Learning
Maskable interrupts and non-maskable interrupts:           According to whether they can be masked, interrupts can be divided into two categories: non-maskable interrupts (also called non-maskable interrupts) and maskable interrupts. Once a non-maskable interrupt source makes a request, the CPU must respond unconditio
[Microcontroller]
STM32 MCU--External Interrupt EXTI Learning
The problem of microcontroller serial port setting encountered recently
The recent test involves the modification of the low-level serial port code. After this modification, I suddenly realized that I was not very clear about some characteristics of the serial port before. Some problems encountered first: 1) When using the IO data bits, the number of bits occupied by the check bits is
[Microcontroller]
What is the difference between the microcontroller STC89C52 and STC89C52RC?
STC89C52RC is an enhanced MCS-51 microcontroller from Hongjing Company. Compared with Atmel's AT89C52, it has the following advantages: (1) Supports STC's 2-wire download method, making program downloading more convenient; (2) Supports 6T mode (in 6T mode, 6 clock cycles are one machine cycle) (3) I
[Microcontroller]
[MCU] crc32
// Referenced from http://www.w3.org/TR/PNG/#D-CRCAppendix #ifdef MAKE_CRC32_TABLE static unsigned long crc_table ; static int crc_table_computed = 0; static void make_crc_table(void) {     unsigned long c;     unsigned long n, k;     for (n = 0; n 256; n++) {         c = n;         for (k = 0; k 8; k++) {  
[Microcontroller]
Introduction to 51 MCU storage programs and data
MCS-51 uses Harvard structure, its program space and data space are separately addressed, that is, each has its own address space, and they do not overlap. So even if the address is the same, because it is separately addressed, we still have to say which address is in which space. However, the address space of ARM (ev
[Microcontroller]
Application of single chip microcomputer in low power consumption temperature detection system
1 Introduction In measurement and instrumentation, temperature detection has almost become an indispensable part. Traditional analog temperature sensors are either complicated in peripheral circuits or require the design of A/D conversion and cumbersome operation, which are subject to certain restrictions in use. Th
[Microcontroller]
Application of single chip microcomputer in low power consumption temperature detection system
Research on the application of Proteus virtual simulation in single chip microcomputer experimental teaching
0 Introduction     In the national college electronic design competitions over the years, most of the test questions involve single-chip microcomputer control problems, which shows the importance of single-chip microcomputer course experimental teaching. However, since the design of single-chip microcomputer systems
[Microcontroller]
Research on the application of Proteus virtual simulation in single chip microcomputer experimental teaching
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号