Ai Ho

Jet Station πŸš€

Menu

🏠 Home | πŸš€ Embedded Systems | 🧰 Development Toolbox | πŸŽ“ Training Courses | πŸ“š Documents

Embedded C Data Types

🎯 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.

Table of Contents

  1. Common Data Types in C
  2. Key Points When Using Data Types in Embedded Systems

Embedded C Data Types

Common Data Types in C

To design robust embedded software using data types, the following key points should be considered.

Key Points When Using Data Types in Embedded Systems

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

Practice

πŸ’‘ For example, when choosing a data type to store the age of a person, I follow these steps:

πŸš€ 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: Data Size in Embedded System

Explore More Topics

|πŸ‘ˆ Previous | Next πŸ‘‰|

Repositories

πŸš€ My Repositories

Contact & Discussion

If you have any thing would like to discuss or cooperate with me, please don’t hesitate to contact me via:

Home Page

🏠 Home