正文
空闲帧
一空闲帧所需要的时间:1位起始位+8位数据位+1位校验位+1位停止位。
编程要点
串口硬件相关宏定义,bsp_uart.h
#ifndef __BSP_UART_H
#define __BSP_UART_H
#include "stm32f10x.h"
#include <stdio.h>
#define RCC_UART_GPIO_Clk_Cmd RCC_APB2PeriphClockCmd
#define RCC_UART_GPIO_Clk RCC_APB2Periph_GPIOA
#define UART_GPIO_Tx_Pin GPIO_Pin_9
#define UART_GPIO_Tx_Port GPIOA
#define UART_GPIO_Rx_Pin GPIO_Pin_10
#define UART_GPIO_Rx_Port GPIOA
#define BSP_USARTx USART1
#define RCC_UART_Clk_Cmd RCC_APB2PeriphClockCmd
#define RCC_UART_Clk RCC_APB2Periph_USART1
#define UART_BaudRate 115200
#define BSP_USARTx_IRQHandler USART1_IRQHandler
void BSP_UART_Init(void);
#endif /* __BSP_UART_H */
串口模块配置,bsp_uart.c
#include "bsp_uart.h"
static void BSP_UART_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_UART_GPIO_Clk_Cmd(RCC_UART_GPIO_Clk,ENABLE);
GPIO_InitStructure.GPIO_Pin = UART_GPIO_Tx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART_GPIO_Tx_Port,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = UART_GPIO_Rx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART_GPIO_Rx_Port,&GPIO_InitStructure);
}
static void BSP_UART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_UART_Clk_Cmd(RCC_UART_Clk,ENABLE);
USART_InitStructure.USART_BaudRate = UART_BaudRate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits =USART_StopBits_1;
USART_InitStructure.USART_WordLength =USART_WordLength_8b;
USART_Init(BSP_USARTx,&USART_InitStructure);
USART_ITConfig(BSP_USARTx,USART_IT_RXNE,ENABLE);
USART_ITConfig(BSP_USARTx,USART_IT_IDLE,ENABLE);
USART_Cmd(BSP_USARTx,ENABLE);
}
void BSP_UART_Init(void)
{
BSP_UART_Config();
BSP_UART_GPIO_Config();
}
串口不定长数据接收
主函数,main.c
#define data_size 100
typedef struct{
uint16_t flag;
uint16_t len;
char data[data_size];
}Data_TypeDef;
void Data_Process(Data_TypeDef* Data_Struct)
{
Usart_SendString(BSP_USARTx,Data_Struct->data);
Data_Struct->flag = 0;
Data_Struct->len = 0;
}
void BSP_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
Data_TypeDef Data_Structure;
int main(void)
{
BSP_UART_Init();
BSP_NVIC_Config();
while(1)
{
if(Data_Structure.flag == 1)
{
Data_Process(&Data_Structure);
}
}
}
中断服务,stm32f10x_it.c
extern Data_TypeDef Data_Structure;
void BSP_USARTx_IRQHandler(void)
{
if(USART_GetITStatus(BSP_USARTx, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
Data_Struct.data[Data_Structure.len] = USART_ReceiveData(BSP_USARTx);
if(++Data_Structure.len > data_size - 2)
{
Data_Struct.len = 0;
}
}
if(USART_GetITStatus(BSP_USARTx,USART_IT_IDLE) != RESET)
{
BSP_USARTx->SR;
BSP_USARTx->DR;
Data_Structure.data[Data_Structure.len] = '\0';
Data_Structure.flag = 1;
}
}
这里使用软件清除RXNE标志,对USART_DR读操作可以将该位清零。
这里使用软件清除IDLE标志,对USART_SR,USART_DR读操作可以将该位清零。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ZipingPan/article/details/137313795
|