程序如下:
1.usart.c程序
#include "usart.h"
#include "gpio.h"
#include "stm32f7xx_hal.h"
/* USER CODE BEGIN 0 */
uint8_t aRxBuffer[RXBUFFERSIZE]; //定义全局变量“aRxBuffer[RXBUFFERSIZE]”为HAL库UASRT函数接收缓冲区
uint16_t USART_RX_STA=0; //定义接收数据标识符
uint8_t USART_RX_BUF[USART_RX_LEN];//定义接收数据的最大缓存
/* USER CODE END 0 */
UART_HandleTypeDef huart2;
/* USART2 init function */
void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
//HAL_UART_Init(&huart2);//HAL_UART_Init()会使能UART1
if (HAL_UART_Init(&huart2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
//开启接收中断,同时设置接收的缓冲以及接收缓冲接收的最大数据量
HAL_UART_Receive_IT(&huart2,(uint8_t *)aRxBuffer,RXBUFFERSIZE);
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(uartHandle->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* USART2 clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/**USART2 GPIO Configuration
PD5 ------> USART2_TX
PD6 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* USART2 interrupt Init */
HAL_NVIC_SetPriority(USART2_IRQn, 1, 0);//设置抢占优先级3,子优先级3
HAL_NVIC_EnableIRQ(USART2_IRQn); //使能USART2中断通道
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspDeInit 0 */
/* USER CODE END USART2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART2_CLK_DISABLE();
/**USART2 GPIO Configuration
PD5 ------> USART2_TX
PD6 ------> USART2_RX
*/
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
/* USART2 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART2_IRQn);
/* USER CODE BEGIN USART2_MspDeInit 1 */
/* USER CODE END USART2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
//数据处理函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef*huart)
{
//1.判断接收标识符“USART_RX_STA”bit15是否为1,进而判断接收是否完成
if((USART_RX_STA & 0x8000)==0)
{
//1.1判断接收标识符“USART_RX_STA”bit14是否为1,进而判断是否收到“0x0d”
if(USART_RX_STA & 0x4000)
{
if(aRxBuffer[0] != 0x0a )USART_RX_STA=0;
else USART_RX_STA |= 0x8000;
}
else
{
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_RX_LEN-1))USART_RX_STA = 0;
}
}
}
}
//中断函数
void huart2_IRQHandler(void)
{
uint32_t timeout = 0;
uint32_t maxDelay = 0x1FFFF;
HAL_UART_IRQHandler(&huart2);//调用HAL库公用中断处理函数
timeout=0;
//判断串口是否就绪,如果没有就等待就绪
while(HAL_UART_GetState(&huart2)!= HAL_UART_STATE_READY)
{
timeout++;//超时处理
if(timeout > maxDelay) break;
}
timeout = 0;
//一次处理完成之后,重新开始中断并设置RxXferCount为1
while(HAL_UART_Receive_IT(&huart2,(uint8_t *)aRxBuffer,RXBUFFERSIZE) != HAL_OK)
{
timeout++;//超时处理
if(timeout > maxDelay) break;
}
}
2.main.c如下
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3ffff;//得到此次接收到的数据长度
HAL_UART_Transmit(&huart2,(uint8_t *)USART_RX_BUF,len,1000);//发送接收到的信息
while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC)!=SET); //等待发送结束
USART_RX_STA=0;
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
HAL_UART_Transmit(&huart2,TxData1,8,1000);
}
else
{
times++;
if(times % 100000 == 0)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
}
}
3.出现的错误:接收第一个字符后,跳入void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)函数下面的判断语句,从而跳出接收中断。
if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) ||
(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
{
/* Blocking error : transfer is aborted
Set the UART state ready to be able to start again the process,
Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
UART_EndRxTransfer(huart);
初来乍到,求各路大神指点迷津,先行谢过! |