4

I have a standard c function with the following prototype

extern void lcd_puts(const char *s);

in my other function i have something like this

send_to_lcd() {
  uint8_t count = 10
  lcd_puts(count);
}

my question is how do i convert count to a string pointer to be able to send it to lcd_puts which should print out the count on a lcd screen

thanks

1
  • 2
    Do you know what libraries you have available? Do you have itoa or sprintf? Commented Sep 5, 2011 at 0:52

6 Answers 6

6

On a microcontroller, you have to be at least a little worried about performance (which rules out sprintf), even division is a very expensive operation on such chips. So you want code optimized for a microcontroller.

I've written some here: http://ideone.com/SsEUW (will need a few changes for use with C-style strings instead of C++, but the method should be clear)

Sign up to request clarification or add additional context in comments.

11 Comments

You don't necessarily have to be worried. It depends if it's time-critical. Prefer standard-library routines over rolling your own until you know that you need to.
@Oli: itoa as suggested by Owen might be feasible, but sprintf is a horrible choice on a microcontroller.
Until the OP adds the words "I need it to be fast" to his/her question, I stick with my answer that sprintf is the best choice here; it's a widely-supported standard library function (one can't say the same for itoa).
@Oli Yes, probably sprint would be fine, if he's writing to an LCD screen it's not like you're gonna see the 1 ms (1000 cycles on 1MHz proc) or so delay. Be careful with it though because a lot of embedded libraries use a stripped-down, non-standard sprintf that might not do what you expect (like always print in Hex (PIC for %ld)), or none at all
He is looking for a C answer.
|
5

It depends on what lcd_puts does with its argument. One possibility is as follows:

void send_to_lcd(uint8_t count)
{
    char str[SOME_CONSERVATIVE_MAX_LENGTH];
    sprintf(str, "%d", count);  // You might also snprintf() if it's available
    lcd_puts(str);
}

But remember that str goes out of scope as soon as send_to_lcd() returns. So if lcd_puts "remembers" its input argument, this will have undefined behaviour.

If that's the case, you will have to malloc a string buffer instead. But then you'll need to remember to free() it at some point, and it all gets rather messy.

5 Comments

Or, if you don't have malloc(), you could use a static buffer (but I don't think lcd_puts saves it's argument -- it's probably just setting digital pins).
@Owen: Yeah, probably not. I was just preparing for worst-case, where the LCD isn't actually updated til later or something.
As count is essentially a small unsigned integer, %s as a format to sprintf is likely to access violate.
If this is on AVR you might want to use sprintf_P instead (nongnu.org/avr-libc/user-manual/…)
For microcontroller projects I avoid malloc.
2

This seems like a reasonable approach.

#include <stdint.h>
#include <stdio.h>

const char *u82s(uint8_t count)
{
    static char aString[4];

    aString[3] = '\0';
    aString[2] = (count % 10) + '0';  count /= 10;
    aString[1] = (count % 10) + '0';  count /= 10;
    aString[0] = (count % 10) + '0';

    return aString;
}

int main(void)
{
    uint8_t z = UINT8_MAX;

    do
    {
        z++;
        printf("%s\n", u8ts(z));
    }
    while (z != UINT8_MAX);

    return 0;
}

8 Comments

You wrote up a nice short (if very slow) version, then called printf anyway?
The printf occurs in the unit test, not the function. The question raised was for a method to turn a uint8_t into a char *. The function does that, without spending the overhead that would be associated with the printf family.
renamed function to indicate its intent.
The function u82s() as written is not reentrant, because it uses a static string. Perhaps a better design would be to provide the string as an argument. That way, the function could format the number in any place of a longer string. (But then it should not write '\0' at the end). I don't agree with the previous comment that this function is slow. In fact, it's perhaps an order of magnitude faster than snprintf() or similar functions. It is also orders of magnitude smaller in code size.
@EvilTeach: sorry about my inexpertise with c, but wondering when exactly does the u82s() function gets called?
|
1

sprintf will format a string

Quick example:

char buf[50];
uint8_t count = 10;
sprintf(buf,'%d',count);

lcd_puts(buf);

1 Comment

I am just getting my hands wet at this uC thing, so performance for me is not a problem right now. I tried this and it worked out ofthe box. Thanks to everyone else for their comments and making me aware of performance issues.
0

Taking your api as a basis.

/* One assumes this is a function that somehow displays a c string on the lcd. */
extern void lcd_puts(const char *s);


send_to_lcd() 
{
  uint8_t count = 10;     /* This is the variable to send to the screen         */ 

  lcd_puts(u82s(count));  /* This creates a string representation of count,     */
}                         /* which is then passed to the lcd_puts function      */
                          /* giving you the result you are after.  You question */
                          /* was how to make a c string out of a uint8.         */
                          /* this is a way to do it.                            */

It is basically the same as the answer you picked. Convert the value count into a c string, so that lcd_puts can use it.

Comments

0

Removed static and tidied up:

void UART_SendInt( uint16_t num )
{
    #define MAX_LEN         6   // 32767 is 6 characters with NULL terminator
    #define BASE_10         10  // Print decimal format

    uint8_t index = MAX_LEN - 1;
    char str[ MAX_LEN ];

    str[ index ] = '\0';
    while( index-- )
    {
        str[ index ] = ( num % BASE_10 ) + '0'; 
        num /= BASE_10;

        if( 0 == num )
        {
            UART_SendStr( &str[ index ] );
            break;
        }
    }

    UART_SendStr( "\r\n" );

    return;
}

I can't print above 32767 but i think that's something to do with my compiler options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.