π Home | π Embedded Systems | π§° Development Toolbox | π Training Courses | π Documents
π― Embedded C programming relies on a variety of data types to efficiently manage memory and interact with hardware. Understanding these data types is essential for writing reliable and portable embedded software.
To design robust embedded software using data types, the following key points should be considered.
Data Size: Always consider the size of each data type, as it directly impacts memory usage and performance. Using a larger data type than necessary can waste valuable resources, while using a smaller type may lead to overflow or data loss.
Naming Convention: Use clear and consistent naming conventions for variables, especially when dealing with hardware registers or specific bit-widths (e.g., uint8_t
, int16_t
). This improves code readability and helps prevent errors related to type mismatches.
Floating Point Data Types: Be cautious when using floating point types (float
, double
) in embedded systems. Many microcontrollers lack hardware support for floating point operations, which can result in slower execution and increased code size. Prefer integer math when possible for better efficiency.
Alignment and Padding: Some microcontrollers require data to be aligned in memory. Misaligned data can lead to inefficient access or even faults. Be aware of how your compiler handles alignment and padding, especially in structs.
Portability: Data type sizes may differ between platforms. Use standard types from stdint.h
(such as uint8_t
, int16_t
) to ensure your code is portable and behaves consistently across different hardware.
Volatile Keyword: Use the volatile
keyword for variables that can change unexpectedly, such as hardware registers or interrupt flags. This prevents the compiler from optimizing out important reads and writes.
Endianness: Be aware of byte order (big-endian vs. little-endian) when handling multi-byte data, especially for communication protocols or data exchange between different systems.
Bit-fields: Bit-fields in structs allow efficient access to individual bits, which is useful for hardware control. However, their behavior can be compiler-dependent, so use them with caution and verify their layout.
π‘ For example, when choosing a data type to store the age of a person, I follow these steps:
unsigned char
.unsigned char
, which is uint8_t
.age_u8
to make its type and purpose clear.π You can use this demo project to gain a practical understanding of data types in embedded software. In this project, I demonstrate:
π Below, you will find examples from the project source code that illustrate the size of C standard data types and common used embedded C data types.
/* C standard data types */
const uint8_t C_DataSizeInBytes_u8[] = {
sizeof(char),
sizeof(unsigned char),
sizeof(int),
sizeof(short),
sizeof(long),
sizeof(float),
sizeof(double),
sizeof(unsigned int)
};
/* Embedded C standard data types */
const uint8_t EmbeddedC_DataSizeInBytes_u8[] = {
sizeof(bool),
sizeof(int8_t),
sizeof(uint8_t),
sizeof(int16_t),
sizeof(uint16_t),
sizeof(int32_t),
sizeof(uint32_t)
};
π Actual data type sizes measured in the STM32F103Cx embedded system:
π My Repositories
If you have any thing would like to discuss or cooperate with me, please donβt hesitate to contact me via:
π Home