要将数字转换为字符串并通过串口发送,可以使用 sprintf 函数将数字转换为字符串,然后使用串口发送函数发送字符串。

以下是一个示例代码:

#include "gd32f10x.h"
#include <stdio.h>

void uart_send_string(char *str);

int main(void)
{
    uint32_t number = 12345;
    char str[16];

    // 将数字转换为字符串
    sprintf(str, "%lu", number);

    // 通过串口发送字符串
    uart_send_string(str);

    while (1)
    {
    }
}

void uart_send_string(char *str)
{
    while (*str != '\0')
    {
        // 等待串口发送缓冲区为空
        while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
            ;
        
        // 发送一个字符
        usart_data_transmit(USART0, *str++);
    }
}

在这个示例中,我们将数字 12345 转换为字符串并通过串口发送。首先,我们使用 sprintf 函数将数字转换为字符串,并将其存储在 str 数组中。然后,我们使用 uart_send_string 函数通过串口发送字符串。该函数会循环发送字符串中的每个字符,直到遇到字符串的结束符 '\0'。


原文地址: https://www.cveoy.top/t/topic/pd4k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录