近段時間學習到 STM32 USART 部分,基本上在接收數(shù)據(jù)的時候都是采用定長,所以一直想實現(xiàn)接收任意長度的字符串。這里的任意長度不是指的無限長,而是在自己定義的緩沖區(qū)范圍之類。比如說緩沖區(qū)的大小是 1024 Byte,那么就能接收不大于 1024 個字符串。
當時有兩個思路:
1、使用結(jié)尾標志,如 "rn" 什么的
2、定時判斷接收數(shù)據(jù)的長度,如果在規(guī)定的時間內(nèi)長度沒有發(fā)生變化,證明已經(jīng)接收完了任意長度的字符
因為思路 1 比較好實現(xiàn),而且網(wǎng)上也有很多例程,所以著重講思路 2
宏定義:
usart.h 文件
/****************************************************************************************************************/
#ifndef USART_H
#define USART_H
#include "stm32f4xx.h"
#define MAX_LENGTH 1024
#define USARTX USART3
#define USART_CLK RCC_APB1Periph_USART3
#define USART_BAUD_TATE 115200
#define USART_GPIO_PORT GPIOB
#define USART_GPIO_CLK RCC_AHB1Periph_GPIOB
#define USART_RX_PIN GPIO_Pin_11
#define USART_TX_PIN GPIO_Pin_10
#define USART_RX_PINSOURCE GPIO_PinSource11
#define USART_TX_PINSOURCE GPIO_PinSource10
#define GPIO_AF_USART GPIO_AF_USART3
#define USART_IRQN USART3_IRQn
#define USART_IRQ_HANDLDER USART3_IRQHandler
void USART_Init(void);
char* USART_GetString(void);
/******************************************************************************************************************/
注意:
本文中使用的是 STM32F405 系列的單片機,使用的是 USART3 ,請讀者根據(jù)自己的單片機型號和 USART 做出相應更改
usart.c 文件
/****************************************************************************************************************/
#include "./XXX/usart.h" //XXX 代表存放 usart.h 文件的路徑
volatitle char receiveBuffer[MAX_LENGTH];
volatitle uint16_t receiveLength = 0;
volatitle uint8_t rxFlag = 0;
static char str[MAX_LENGTH + 1];
static void GPIO_Config(void)
{
GIPO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(USART_GPIO_PORT, ENABLE);
GPIO_PinAFConfig(USART_GPIO_PORT, USART_RX_PINSOURCE, GPIO_AF_USART);
GPIO_PinAFConfig(USART_GPIO_PORT, USART_TX_PINSOURCE, GPIO_AF_USART);
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USART_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_Init(USART_GPIO_PORT, &GPIO_InitStructure);
}
static void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART_IRQN;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void delay_us(uint16_t time)
{
uint8_t count;
while(time--)
{
count = 10;
while(count--);
}
}
void USART_Init(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(USART_CLK, ENABLE);
GPIO_Config();
NVIC_Config();
USART_InitStructure.USART_BaudRate = USART_BAUD_TATE;
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(USARTX, &USART_InitStructure);
USART_ITConfig(USARTX, USART_IT_RXNE, ENABLE);
USART_Cmd(USARTX, ENABLE);
}
char* USART_GetString(void)
{
uint16_t temp;
while(!rxFlag); //等待接收數(shù)據(jù),在串口接收到一幀數(shù)據(jù)時 rxFlag 將會被置 1 (USART3 中斷服務函數(shù)中)
while(rxFlag)
{
temp = receiveLength;
delay_us(500); //等待 500us
if(temp == receiveLength) //判斷 receiveLength 是否發(fā)生變化(USART3 中斷函數(shù)中 receiveLength 會有變化),如果沒有,證明已經(jīng)收完所有的數(shù)據(jù),否則等待接收完成
{
rxFlag = 0;
}
}
for(temp = 0; temp < receiveLength; temp++)
{
str[temp] = receiveBuffer[temp];
}
receiveLength = 0;
str[temp] = "