avr - 如何在 oled 显示器上显示 uint8 值
问题描述
我可以编译代码但没有显示
int main(void){
lcd_init(LCD_DISP_ON);
lcd_clrscr();
lcd_set_contrast(0x00);
lcd_gotoxy(0,3);
lcd_puts((char*)&temperature);
lcd_gotoxy(1,2);
lcd_puts((char*)&humidity);
lcd_puts("Hello World");
}
解决方案
您需要先将数值数据(例如uint8_t
)转换为字符串,然后才能显示它。
例如,该uint8_t
值123
是一个字节,但要显示它,必须将其转换为一个三字符/字节的字符串,,,,1
即2
三个3
s char
0x31、0x32、0x33。
为此,您可以使用函数itoa()
(“integer to ascii”)将整数值复制到char
您提供的数组中。请注意,char
数组必须足够大以容纳任何可能的数字字符串,即如果您的值是uint8_t
's(范围 0...255),则数组必须至少为三个字符长。
要将字符数组作为 C(-libraries) 中的字符串处理,您需要一个额外char
的来保存字符串 terminator '\0'
。
例子:
char tempStr[3+1]; // One extra for terminator
// Clear tempStr and make sure there's always a string-terminating `\0` at the end
for ( uint8_t i = 0; i < sizeof(tempStr); i++ ) {
tempStr[i] = '\0';
}
itoa(temperature, tempStr, 10);
// Now we have the string representation of temperature in tempStr, followed by at least one '\0' to make it a valid string.
// For example:
// 1 --> [ '1', '\0', '\0', '\0' ]
// 255 --> [ '2', '5', '5', '\0' ]
推荐阅读
- python - 如何在 python 中使用 kml 在 macOS 桌面上保存文件?
- performance - 为什么在这种情况下广播比 for 循环慢?
- grails - mapWith with hasMany 在运行应用程序时出现错误,Grails v3.3.5 在 Grails v3.2.11 上运行良好
- hadoop - 将小文件合并为hdfs中的单个文件
- java - 数组中的字符一一输入java
- python - 是否可以将 pyModbusTCP 用作 modbus 从站?
- performance - 计算与打印
- node.js - 使用nodejs,expressjs和reactjs在会话超时后重定向到登录页面
- python - OSError: 无法识别图像文件 <_io.BytesIO object at 0x000001C4EA23AE08>
- java - 使用 Eclipse Windows 10 访问 PostgreSQL 时出错