不要使用printf函数
时间: 2023-09-21 17:07:54 浏览: 37
好的,如果您不想使用printf函数打印16位数,您可以使用单片机的串口发送函数,例如UART_Send函数(具体函数名可能根据您的单片机型号而有所不同)。下面是一个示例代码:
```c
#include <stdint.h>
#include <string.h>
// 定义一个16位整型数
uint16_t num = 65535;
// 将16位整型数转换成字符串
void int_to_str(uint16_t num, char *str)
{
uint16_t i = 0, j = 0;
char temp[10] = {0};
do
{
temp[i++] = num % 10 + '0';
num /= 10;
} while (num);
while (i--)
{
str[j++] = temp[i];
}
str[j] = '\0';
}
int main(void)
{
char buffer[10];
int_to_str(num, buffer); // 将16位整型数转换成字符串
UART_Send(buffer, strlen(buffer)); // 发送字符串
return 0;
}
```
在以上示例代码中,我们定义了一个int_to_str函数,用于将16位整型数转换成字符串。该函数将数字从低位到高位逐个取出,并转换成字符存储在临时数组temp中。最后将temp数组中的字符从高位到低位逐个存储到目标字符串中,并在字符串末尾添加结束符'\0'。最后,我们使用单片机的串口发送函数将字符串发送出去。