#include "stm32f10x.h"
#include <stdio.h>
void COM1_Init( void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //开启串口时钟
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);
//配置串口TX作为推挽复用端口
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);
//配置串口RX作为悬空输入端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//使能串口
USART_Cmd(USART1, ENABLE);
}
int i;
char rx_data[50]="顾利军本科就读于于中南民族大学";
int main(void)
{
//初始化系统,包括选择系统时钟频率。
SystemInit();
//初始化串口
COM1_Init();
while(rx_data[i]!='\0')
{USART_SendData(USART1, rx_data[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
i++;
}
}
本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。