STM32F1系列MCU串口通信的发送与接收功能

目录

串口配置

串口发送

1使用SendString函数发送

2使用printf函数发送

​串口接收


 串口配置

首先对串口进行初始化

包括使能串口时钟,这里我使用的是usart2,使能GPIO时钟,这里我用的是A口,以及GPIO口的配置,这里我的串口输出是PA2,输入是PA3

然后初始化usart2,再使能usart2,具体代码如下:

/* 串口初始化 */

void STM_EVAL_COMInit(USART_InitTypeDef* USART_InitStruct)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIOA使能

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);USART2时钟使能

  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//输出PA2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//输入PA3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* USART configuration */
  USART_Init(USART2, USART_InitStruct);//USART2初始化
    
  /* Enable USART */
  USART_Cmd(USART2, ENABLE);//使能USART2
}

之后需要配置串口的参数

参数包括波特率,数据位个数,是否有校验位等

void USART2_Init (void)
{
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure; 
	
	USART_InitStructure.USART_BaudRate = 115200;                  //波特率
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;   //数据位个数=8
  USART_InitStructure.USART_StopBits = USART_StopBits_1;        //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;

  STM_EVAL_COMInit(&USART_InitStructure);
	
	
//数据接收的使能中断
  /* Enable the USARTz Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//为USART2
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
  /* Enable the USARTz Receive Interrupt */
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//
}

为了保证串口随时都能接收数据,需要开启中断

串口发送

串口发送是指,将stm32的数据发送给上位机(串口助手)

1使用SendString函数发送

在.c文件里面定义SendString函数

对应USART数据发送有两个标志, 一个是TXE=发送数据寄存器空,另一个是TC=发送结束

void SendString (char *s)
{
	while(*s)
	{
		USART_SendData(USART2, *s++);//依次发送

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)//TC标志位为reset发送结束
  {}
	}
}

在发送数据时出现第一个字符打印不出来的情况,例如发送“hello”,串口助手只接收到了“ello”,没有h

这是因为stm32的usart的sr寄存器的TC初始为1,导致第一次while时第一个数据被覆盖

这种情况,将TC修改为TXE即可

void SendString (char *s)
{
	while(*s)
	{
		USART_SendData(USART2, *s++);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  {}
	}
}

在main.c文件里面调用SendString函数

USART2_Init();//打开串口
SendString("字符串");

2使用printf函数发送

在使用printf函数之前需要先勾选微库

在.c文件里面定义printf函数 ,代码如下

int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART2, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
  {}

  return ch;
}

 在main.c文件里面调用printf函数

USART2_Init();
 printf(" ", ); 

关于printf函数的使用方法可以参考下面的表格:

串口接收

串口接收是指,将上位机(串口助手)的数据发送给stm32,由stm32进行接收

 USART_IT_RXNE为接收中断标志位,一旦接收数据,USART_IT_RXNE会置位

u8 RxCounter = 0;//定义计数值
u8	RxBuffer2[10];//定义缓冲区为10

void USART2_IRQHandler(void)
{
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//串口接收中断
  {
    USART_ClearITPendingBit(USART2, USART_IT_RXNE);//清除标志位
		/* Read one byte from the receive data register */
		
    RxBuffer2[RxCounter++] = USART_ReceiveData(USART2);
		
		
		if(RxCounter>6)//定义接收的字节个数
		{
			RxCounter=0;
		}
}	
}

首先构建缓冲区和计数器

串口接收中断之后,清除 USART_IT_RXNE中断接收标志位,将串口接收到的数据存入缓冲区

可以通过debug里面的watch来看缓冲区数据是否正确

物联沃分享整理
物联沃-IOTWORD物联网 » STM32F1系列MCU串口通信的发送与接收功能

发表评论