Article Directory
Software and hardware model
STM32F103RCT development board
STM32CUBEMX+KEIL5 programming
STM32F1 1.8.3 library version
1. CUBE configuration
- Open the serial port
- Open interrupt
- Configuration priority
- Generate project files
2. Code addition
- In
usart.h
adding:
#define USART_REC_LEN 200 //定义最大接收字节数 200
#define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收
extern uint8_t USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符
extern uint16_t USART_RX_STA; //接收状态标记
//用于缓存传输来的每一个字节
#define RXBUFFERSIZE 1 //缓存大小
extern uint8_t aRxBuffer[RXBUFFERSIZE];//HAL库USART接收Buffer
- In
usart.c
adding:
- Add in front:
uint8_t USART_RX_BUF[USART_REC_LEN];
uint16_t USART_RX_STA=0;
void MX_USART1_UART_Init(void)
Add at the end of the function ;
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
- Add a callback function:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
int len;
if(huart->Instance==USART1)//如果是串口1
{
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//接收错误,重新开始
else USART_RX_STA|=0x8000; //接收完成了
len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
HAL_UART_Transmit(&huart1,USART_RX_BUF,len,1000); //发送得到的数据
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET);
USART_RX_STA=0;
}
else //还没收到0X0D
{
if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
}
}
- In
stm32f1xx_it.c
adding:
#include "usart.h"
uint8_t aRxBuffer[RXBUFFERSIZE];
In void USART1_IRQHandler(void)
the alternative as follows:
uint32_t timeout=0;
uint32_t maxDelay=0x1FFFF;
HAL_UART_IRQHandler(&huart1); //调用HAL库中断处理公用函数
timeout=0;
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)//等待就绪
{
timeout++;超时处理
if(timeout>maxDelay) break;
}
timeout=0;
while(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)//一次处理完成之后,重新开启中断并设置RxXferCount为1
{
timeout++; //超时处理
if(timeout>maxDelay) break;
}
Now you can compile, use the serial debugging assistant to send messages to the microcontroller, and the data sent to the microcontroller will be sent to the PC again.
Note: Remember to add a carriage return after the data sent to let the MCU know the end position of the data when receiving the data
3. Supplement
- Serial port transmission (replace the buffer and length):
HAL_UART_Transmit(&huart1,(uint8_t *)buffer,len,1000);
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET);
- String left interception function:
//usart.c
//从左 字符串截取函数
char * left(char *dst,char *src, int n)
{
char *p = src;
char *q = dst;
int len = strlen(src);
if(n>len) n = len;
while(n--) *(q++) = *(p++);
*(q++)='\0'; /*有必要吗?很有必要*/
return dst;
}
- String comparison function (replace the string you want to wait, here is led):
if(strcmp((const char *)USART_RX_BUF,"led")==0)
{
// 添加如果相等需要执行的代码
}
The above need to include header files:
#include "string.h"