我可以编译代码,但没有显示
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");
}
答案 0 :(得分:0)
您需要先将数字数据(例如uint8_t
)转换为字符串,然后才能显示它。
例如,uint8_t
值123
是一个字节,但是要显示它,必须将其转换为三个字符/字节的字符串1
,2
,3
,即三个char
的0x31、0x32、0x33。
为此,您可以使用函数itoa()
(“整数到ascii”)将整数值复制到您提供的char
数组中。请注意,char
数组必须足够大以容纳任何可能的数字字符串,即,如果您的值是uint8_t
的值(范围为0 ... 255),则数组的长度必须至少为三个字符
要在C(-libraries)中将字符数组作为字符串处理,则需要附加的char
来容纳字符串终止符'\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' ]