5 Key Math Principles for Embedded Development

Today, let’s introduce some common basic mathematical algorithms in embedded systems. The core of embedded mathematics lies in transforming theoretical abstractions into efficient implementations within resource-constrained environments.

Application Scenarios: Register configuration, communication protocol parsing, hardware state management

Mathematical Principle: Perform bit-level operations using logical AND (&), OR (|), XOR (^), and shift (<< / >>), which are practical applications of Boolean algebra.

Source Code Example:

// Bit-reversal function
unsigned int reverse_bits(unsigned int num) {
unsigned int numOfBits = sizeof(num) * 8;
unsigned int reverseNum = 0;
for (unsigned int i = 0; i < numOfBits; i++) {
if (num & (1 << i))
reverseNum |= (1 << ((numOfBits - 1) - i));
}
return reverseNum;
}

This function achieves binary reversal by traversing each bit, commonly used in data encoding (such as CRC checks) or special peripherals (like LED matrix drivers).

Application Scenarios: Decimal operations on MCUs without Floating Point Units (FPUs), such as motor control and sensor calibration

Mathematical Principle: Map floating-point numbers to integer space using scaling factors (such as Q-format) to preserve precision, essentially a linear transformation.

Source Code Example:

// Fixed-point multiplication
typedef int16_t fixed_t;
#define FIXED_SHIFT 8
fixed_t fixed_multiply(fixed_t a, fixed_t b) {
return (fixed_t)(((int32_t)a * (int32_t)b) >> FIXED_SHIFT);
}

This code uses a 32-bit intermediate result to prevent overflow, suitable for scenarios requiring efficient computation like temperature compensation and PID controllers.

Application Scenarios: UART data buffering, cyclic task scheduling

Mathematical Principle: Use modulus (%) to implement cyclic indexing and prevent buffer overflows.

Source Code Example:

#define BUFFER_SIZE 64
uint8_t buffer[BUFFER_SIZE];
uint8_t read_ptr = 0, write_ptr = 0;

void push_data(uint8_t data) {
buffer[write_ptr % BUFFER_SIZE] = data;
write_ptr++;
}

uint8_t pop_data() {
uint8_t val = buffer[read_ptr % BUFFER_SIZE];
read_ptr++;
return val;
}

This structure is widely used in UART receive interrupt service routines to ensure data stream continuity.

Application Scenarios: Sensor signal denoising (e.g., accelerometers, gyroscopes)

Mathematical Principle: Moving average filtering, Kalman filtering, etc., are based on matrix operations and statistical principles.

Source Code Example:

// Moving average filter
#define WINDOW_SIZE 5
float moving_average(float new_sample) {
static float samples[WINDOW_SIZE] = {0};
static uint8_t index = 0;
samples[index] = new_sample;
index = (index + 1) % WINDOW_SIZE;

float sum = 0;
for (uint8_t i = 0; i < WINDOW_SIZE; i++)
sum += samples[i];
return sum / WINDOW_SIZE;
}

This algorithm suppresses high-frequency noise by averaging data within a window, commonly used in ADC sampling post-processing.

Application Scenarios: Motor speed regulation, closed-loop temperature control

Mathematical Principle: PID controllers rely on discretized calculations of integration (error accumulation) and differentiation (rate of change of error).

Source Code Example:

typedef struct {
float Kp, Ki, Kd;
float integral;
float prev_error;
} PIDController;

float pid_update(PIDController *pid, float setpoint, float actual) {
float error = setpoint - actual;
pid->integral += error;
float derivative = error - pid->prev_error;
pid->prev_error = error;
return pid->Kp * error + pid->Ki * pid->integral + pid->Kd * derivative;
}

This implementation converts a continuous system into an iterative algorithm executable in embedded environments through discretized formulas.

Related:

  1. Bluetooth Protocols: 1.0 to 6.0 Key Innovations
  2. Why Skip Internet Setup on a New Laptop? Explained
End-of-Yunze-blog

Disclaimer:

  1. This channel does not make any representations or warranties regarding the availability, accuracy, timeliness, effectiveness, or completeness of any information posted. It hereby disclaims any liability or consequences arising from the use of the information.
  2. This channel is non-commercial and non-profit. The re-posted content does not signify endorsement of its views or responsibility for its authenticity. It does not intend to constitute any other guidance. This channel is not liable for any inaccuracies or errors in the re-posted or published information, directly or indirectly.
  3. Some data, materials, text, images, etc., used in this channel are sourced from the internet, and all reposts are duly credited to their sources. If you discover any work that infringes on your intellectual property rights or personal legal interests, please contact us, and we will promptly modify or remove it.

Leave a Reply