SFRA_F32/28379d_test_SFRA/sfra_test.h
2026-06-12 16:22:17 +08:00

95 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef _SFRA_TEST_H_
#define _SFRA_TEST_H_
#include "libsfra.h"
#include "driverlib.h"
#include "device.h"
#include "board.h"
void sfra_init(void);
void sfra_task_run(void);
static inline void UARTprintf(const char *pcString)
{
while (*pcString != '\0')
{
SCI_writeCharBlockingFIFO(SCIA_BASE, *pcString++);
}
}
// ==================== 整数转字符串支持负数、32位====================
static inline int int32ToStr(int32_t num, char *str)
{
int i = 0;
int isNegative = 0;
if (num < 0) {
isNegative = 1;
num = -num;
}
// 逆序存储数字
do {
str[i++] = (num % 10) + '0';
num /= 10;
} while (num > 0);
if (isNegative) {
str[i++] = '-';
}
// 反转字符串
int start = 0;
int end = i - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
str[i] = '\0';
return i;
}
// ==================== 浮点数转字符串固定5位小数====================
static inline int floatToStr5(float value, char *buf)
{
int len = 0;
int isNegative = 0;
if (value < 0) {
isNegative = 1;
value = -value;
}
// 乘以 100000 并四舍五入
int64_t scaled = (int64_t)(value * 100000.0f + 0.5f);
int32_t intPart = (int32_t)(scaled / 100000);
int32_t fracPart = (int32_t)(scaled % 100000);
// 负号
if (isNegative) {
buf[len++] = '-';
}
// 整数部分
len += int32ToStr(intPart, buf + len);
buf[len++] = '.';
// 小数部分补零到5位
char fracBuf[6];
int fracLen = int32ToStr(fracPart, fracBuf);
int i;
for (i = 0; i < 5 - fracLen; i++) {
buf[len++] = '0';
}
for (i = 0; i < fracLen; i++) {
buf[len++] = fracBuf[i];
}
buf[len] = '\0';
return len;
}
#endif