我有一个标准的C函数,其原型如下:
extern void lcd_puts(const char *s);
在我的另一个函数中,我有如下内容:
send_to_lcd() {
uint8_t count = 10
lcd_puts(count);
}
我的问题是如何将计数转换为字符串指针,以便能够发送到lcd_puts函数,在液晶屏上打印出计数值。谢谢。
我有一个标准的C函数,其原型如下:
extern void lcd_puts(const char *s);
send_to_lcd() {
uint8_t count = 10
lcd_puts(count);
}
在微控制器上,你必须至少有一点担心性能(这排除了sprintf
),即使是除法在这种芯片上也是非常昂贵的操作。因此,您需要为微控制器优化代码。
我写了一些代码:http://ideone.com/SsEUW(需要进行一些更改以使用C风格字符串而不是C ++,但方法应该很清楚)
itoa
可能是可行的,但在微控制器上选择sprintf
是一个可怕的选择。 - Ben Voigtsprintf
是最佳选择;它是一个广泛支持的标准库函数(对于itoa
不能这样说)。 - Oliver Charlesworth这取决于lcd_puts
对其参数的处理方式。其中一种可能是:
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);
}
但请记住,str
变量在 send_to_lcd()
函数返回时就会超出作用域。因此,如果 lcd_puts
"记住" 其输入参数,这将导致未定义的行为。
如果是这种情况,您需要使用 malloc
函数来分配一个字符串缓冲区。但那样的话,您需要记得在某个时候使用 free()
函数释放内存,所有这些都会变得非常混乱。
malloc()
,您可以使用一个static
缓冲区(但我不认为lcd_puts
会保存它的参数--它可能只是设置数字引脚)。 - Owensprintf_P
(http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#ga6017094d9fd800fa02600d35399f2a2a)函数。 - Owen这似乎是一个合理的方法。
#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;
}
printf
? - Ben Voigtsprintf函数可以格式化字符串
快速示例:
char buf[50];
uint8_t count = 10;
sprintf(buf,'%d',count);
lcd_puts(buf);
以您的API为基础。
/* 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. */
它基本上与您选择的答案相同。将值计数转换为C字符串,以便lcd_puts可以使用它。
移除了静态内容并整理了代码:
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;
}
我无法打印超过32767的内容,但我认为这与我的编译器选项有关。
itoa
或sprintf
吗? - Owen