我如何在OLED显示屏上显示uint8值

时间:2019-06-25 11:23:08

标签: avr display

我可以编译代码,但没有显示

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");
}

1 个答案:

答案 0 :(得分:0)

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

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