1. 配置串口的波特率、数据位、停止位等参数。
  2. 初始化 STM32F103C8T6 的 GPIO 口,使其能够输出数据。
  3. 编写发送数据的函数,该函数需要将要发送的数据存储到发送缓冲区中,并通过串口发送出去。
  4. 在主函数中调用发送数据的函数,将要发送的数据作为参数传入函数中。
  5. 在串口屏上查看是否成功发送数据。

下面是一个简单的示例代码:

#include "stm32f10x.h"
#include "stdio.h"

void USART1_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);
}

void USART1_SendChar(char c)
{
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    USART_SendData(USART1, c);
}

void USART1_SendString(char* str)
{
    while (*str)
    {
        USART1_SendChar(*str++);
    }
}

int main(void)
{
    USART1_Init();
    USART1_SendString('Hello, world!');

    while (1);

    return 0;
}
STM32F103C8T6 串口屏数据发送程序设计流程

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

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