๐ง STM32 CubeIDE ์ค์๊ฐ ์ฐ์ฐ ์ค์
๐ ๊ณผ์ ์ ๋ณด: STM32 ๋ง์ดํฌ๋ก์ปจํธ๋กค๋ฌ์์ ๋ถ๋์์์ (float, double) ์ฐ์ฐ์ ์ํํ๊ธฐ ์ํ ์ค์ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํ ๋ฌธ์์
๋๋ค.
1. ํ๋ก์ ํธ ์ค์
1
ํ๋ก์ ํธ ์์ฑ ์ด๊ธฐ
2
MCU GCC Compiler ์ค์
Other flags์ ๋ค์ ์ต์ ์ถ๊ฐ:
-u _printf_float -u _scanf_float
3
MCU GCC Linker ์ค์
Other flags์ ๋ค์ ์ต์ ์ถ๊ฐ:
-u _printf_float -u _scanf_float
2. FPU(Floating Point Unit) ์ค์
4
FPU ํ์ฑํ (Cortex-M4F/M7 ๊ณ์ด)
| ์ค์ ํญ๋ชฉ | ๊ถ์ฅ ๊ฐ | ์ค๋ช |
|---|---|---|
| Floating-point unit | FPv4-SP-D16 | ๋จ์ ๋ฐ๋ ๋ถ๋์์์ |
| Floating-point ABI | Hard | ํ๋์จ์ด FPU ์ฌ์ฉ |
โ ๏ธ ์ฃผ์์ฌํญ: FPU๊ฐ ์๋ Cortex-M0/M3 ๊ณ์ด์์๋ ์ํํธ์จ์ด ๋ถ๋์์์ ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
3. ์ฝ๋ ์์
๊ธฐ๋ณธ ์ค์ ์ฐ์ฐ
#include "main.h"
#include <stdio.h>
#include <math.h>
int main(void)
{
HAL_Init();
SystemClock_Config();
// ์ค์ ๋ณ์ ์ ์ธ
float a = 3.14159f;
float b = 2.71828f;
double c = 1.414213562373095;
// ๊ธฐ๋ณธ ์ฐ์ฐ
float sum = a + b;
float product = a * b;
float division = a / b;
// ์ํ ํจ์
float sin_value = sinf(a);
float sqrt_value = sqrtf(b);
double pow_value = pow(c, 2.0);
// printf๋ก ์ถ๋ ฅ (UART ์ค์ ํ์)
printf("Sum: %.6f\n", sum);
printf("Product: %.6f\n", product);
printf("Sin(%.6f) = %.6f\n", a, sin_value);
while (1)
{
// ๋ฉ์ธ ๋ฃจํ
}
}
printf/scanf ์ค์ ์ถ๋ ฅ ์ค์
// syscalls.c ํ์ผ์์ UART๋ฅผ ํตํ printf ๊ตฌํ
int _write(int file, char *ptr, int len)
{
HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
return len;
}
// ์ฌ์ฉ ์์
float temperature = 25.67f;
printf("Temperature: %.2fยฐC\n", temperature);
4. ์ฑ๋ฅ ์ต์ ํ
๐ ์ต์ ํ ํ
- ๋จ์ ๋ฐ๋ ์ฌ์ฉ:
floatํ์ ๊ณผf์ ๋ฏธ์ฌ ์ฌ์ฉ (์:3.14f) - ์ํ ํจ์:
sinf(),cosf(),sqrtf()๋ฑ float ๋ฒ์ ์ฌ์ฉ - ์ปดํ์ผ๋ฌ ์ต์ ํ: -O2 ๋๋ -O3 ์ต์ ํ ๋ ๋ฒจ ์ค์
- ์ธ๋ผ์ธ ํจ์: ๊ฐ๋จํ ์ฐ์ฐ์ ์ธ๋ผ์ธ ํจ์๋ก ๊ตฌํ
5. ๋๋ฒ๊น ๋ฐ ๋ฌธ์ ํด๊ฒฐ
5
์ค์๊ฐ ๋๋ฒ๊น
Variables ์ฐฝ์์ ์ค์๊ฐ ํ์ธ:
- Format์ Decimal๋ก ์ค์
- Precision์ 6~8์๋ฆฌ๋ก ์ค์
- Scientific notation ์ฌ์ฉ ๊ฐ๋ฅ
6
๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ํ์ธ
// ๋น๋ ํ ์ฝ์์์ ํ์ธ
text data bss dec hex filename
12345 1234 5678 19257 4b39 project.elf
6. ์ค์ต ๊ณผ์
๐ ๊ณผ์ ์๊ตฌ์ฌํญ
- ADC์์ ์ฝ์ ๊ฐ์ ์ ์์ผ๋ก ๋ณํ (์ค์ ์ฐ์ฐ)
- ์ผ๊ฐํจ์๋ฅผ ์ด์ฉํ PWM ํํ ์์ฑ
- PID ์ ์ด๊ธฐ ๊ตฌํ (์ค์ ์ฐ์ฐ ํ์)
- UART๋ฅผ ํตํ ์ค์๊ฐ ์ถ๋ ฅ
// ADC ์ ์ ๋ณํ ์์
float adc_to_voltage(uint16_t adc_value)
{
const float vref = 3.3f; // ๊ธฐ์ค์ ์
const float adc_resolution = 4095.0f; // 12bit ADC
return (adc_value * vref) / adc_resolution;
}
// PID ์ ์ด๊ธฐ ์์
typedef struct {
float kp, ki, kd;
float prev_error;
float integral;
} PID_Controller;
float pid_compute(PID_Controller *pid, float setpoint, float measured)
{
float error = setpoint - measured;
pid->integral += error;
float derivative = error - pid->prev_error;
float output = pid->kp * error +
pid->ki * pid->integral +
pid->kd * derivative;
pid->prev_error = error;
return output;
}
โ
์ฒดํฌ๋ฆฌ์คํธ:
- โก ํ๋ก์ ํธ ์ค์ ์์ printf float ์ต์ ์ถ๊ฐ
- โก FPU ์ค์ (ํด๋นํ๋ ๊ฒฝ์ฐ)
- โก ์ค์ ์ฐ์ฐ ์ฝ๋ ๊ตฌํ
- โก ๋๋ฒ๊น ์ผ๋ก ๊ฒฐ๊ณผ ํ์ธ
- โก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ฒดํฌ
์์ฑ์ผ: 2025๋ 8์ 22์ผ
STM32 CubeIDE ์ค์๊ฐ ์ฐ์ฐ ์ค์ ๊ฐ์ด๋