首页 > 解决方案 > 如何在 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");
}

标签: avrdisplay

解决方案


您需要先将数值数据(例如uint8_t)转换为字符串,然后才能显示它。

例如,该uint8_t123是一个字节,但要显示它,必须将其转换为一个三字符/字节的字符串,,,,12三个3s char0x31、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' ] 

推荐阅读