oled怎么显示浮点小数_LCD浮点数显示函数的探讨
时间: 2024-02-11 07:13:28 浏览: 48
关于 OLED 显示浮点数,一般可以通过将浮点数转换为字符数组,然后将字符数组显示在 OLED 屏幕上来实现。具体实现方法可以参考下面的代码:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// OLED 显示
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t *chr, uint8_t Char_Size)
{
uint8_t c = 0,i = 0,j = 0;
switch(Char_Size)
{
case 16:
{
while (chr[j]!='\0')
{
c = chr[j]-32;
if(x>112){x=0;y+=16;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+8);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
x+=8;
j++;
}
}
break;
case 12:
{
while (chr[j]!='\0')
{
c = chr[j]-32;
if(x>116){x=0;y+=12;}
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
x+=6;
j++;
}
}
break;
}
}
// 浮点数转换为字符数组
void FloatToStr(float f, char *str)
{
int i = 0;
int integer = (int)f; // 取整数部分
float decimal = f - integer; // 取小数部分
if (integer < 0) // 处理负数
{
integer = -integer;
decimal = -decimal;
str[i++] = '-';
}
// 转换整数部分
if (integer == 0)
{
str[i++] = '0';
}
else
{
while (integer > 0)
{
str[i++] = integer % 10 + '0';
integer /= 10;
}
}
// 转换小数部分
str[i++] = '.';
decimal = decimal * 1000000;
integer = (int)decimal;
if (integer == 0)
{
str[i++] = '0';
}
else
{
while (integer > 0)
{
str[i++] = integer % 10 + '0';
integer /= 10;
}
}
str[i] = '\0';
// 反转字符数组
for (int j = 0; j < i / 2; j++)
{
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
}
// 测试函数
void test()
{
float f = 3.1415926;
char str[20];
FloatToStr(f, str);
OLED_ShowString(0, 0, (uint8_t *)str, 16);
}
int main()
{
test();
return 0;
}
```
在上面的代码中,`FloatToStr()` 函数将浮点数转换为字符数组,`OLED_ShowString()` 函数将字符数组显示在 OLED 屏幕上。具体的显示方法可以根据自己的 OLED 屏幕型号和驱动芯片进行调整。