stm32 (register)-serial communication

1; Serial communication;
Since the computer does not have a serial port, we have to use the virtual serial port software XCOM V2.0, which can be downloaded from the official website of stm32, but if your microcontroller also uses USB to serial port, then your download and serial communication cannot be performed at the same time; Note that the CH340 driver must be installed in advance before using the serial port;

2; Function code;

在这里插入代码片

#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
int main()
{
    
    
 u8 t;//循环变量
 u16 len;//存储获取的字节数
 Stm32_Clock_Init(9);//PLL
 delay_init(72);
 uart_init(72,9600);//串口时钟设置,和波特率(系统默认一位停止位,8位数据位,无奇偶校验位)
 LED_Init();//LED灯初始化
 while(1)
 {
    
    
  if(USART_RX_STA&0x8000)//判断是否有输入,有输入的话,USART_RX_STA最高位被置1,通过按位与判断是否接受到数据;
  {
    
    
   len=USART_RX_STA&0x3fff;//获取接受到的字符长度,0x3fff是因为一次最大接受长度为14位,即2^14字节;
   printf("你发送的消息为: \r\n");
   for(t=0;t<len;t++)//循环发送数据
   {
    
    
    USART1->DR=USART_RX_BUF[t];//发送数据
    while((USART1->SR&0X40)==0)//通过SR寄存器判断是否发送完成
     ;
   }
   printf("\r\n");
   USART_RX_STA=0;//清除标志位
  }
  else
   {
    
    
    printf("你想发什么里? \r\n");//系统运行提醒
    delay_ms(1000);
  }
 }
}

If there is an error, please correct it;

Guess you like

Origin blog.csdn.net/qq_45906993/article/details/108567117