cstm8

How to Convert the last digit of a number to a char and display it on LCD?


I recently got a STM8 MCU and it has the built in function LCD_GLASS_DisplayString("STRING")

The problem with that function is, as you can see below, that I cannot directly display an integer on it:

void LCD_GLASS_DisplayString(uint8_t* ptr)
{
  uint8_t i = 0x01;

    LCD_GLASS_Clear();
  /* Send the string character by character on lCD */
  while ((*ptr != 0) & (i < 8))
  {
    /* Display one character on LCD */
    LCD_GLASS_WriteChar(ptr, FALSE, FALSE, i);

    /* Point on the next character */
    ptr++;

    /* Increment the character counter */
    i++;
  }
}

How could I modify it so I could send integers directly? Also, I'm not sure I can use any libraries, so just pure C would help.

I was thinking of something like this, but it didn't work:

void LCD_GLASS_DisplayINT(uint16_t integer)
{
  uint8_t i = 0x01;

    LCD_GLASS_Clear();
  /* Send the string character by character on lCD */
  while ((integer != 0) & (i < 8))
  {
    /* Display one number on LCD */
    LCD_GLASS_WriteChar("0" + integer%10, FALSE, FALSE, i);

    /* Point on the next number*/
    integer=integer/10;

    /* Increment the character counter */
    i++;
  }
}

Any idea on how to make it work? I need to either make a function to display the integers or a way to convert them to strings before I send them over to the LCD. The code is pure C, as what I'm programming are pure drivers right now.


Solution

  • You're not far off with "0" + integer%10 - but you need to treat it as a character - '0' + integer%10 - and you need to pass LCD_GLASS_WriteChar a pointer to this character.

    One way to do this is:

    char* digits = "0123456789";
    LCD_GLASS_WriteChar(&digits[integer % 10], FALSE, FALSE, i);
    

    Also, your loop condition - while ((integer != 0) & (i < 8)) should not use bitwise and (&), but rather logical and (&&).

    while ((integer != 0) && (i < 8))