hal库中的UART使用

1.初始化串口

UART_initStruct.Instance		=USART1;
UART_initStruct.Init.BaudRate   = 9600;
UART_initStruct.Init.WordLength = UART_WORDLENGTH_8B;
UART_initStruct.Init.StopBits   = UART_STOPBITS_1;
UART_initStruct.Init.Parity     = UART_PARITY_NONE;
UART_initStruct.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
UART_initStruct.Init.Mode       = UART_MODE_TX_RX;
if(HAL_UART_Init(&UART_initStruct) != 0)
{
	return;
}

2.重写HAL_UART_MspInit()函数

注意:在HAL_UART_MspInit()函数中不能使用HAL_UART_Receive_IT()或HAL_UART_Transmit_IT()函数,否则不会进入中断处理函数中,也就是说设置中断无效;

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{  
  GPIO_InitTypeDef  GPIO_InitStruct;
  
	/* gpio clock enable */
  USARTx_TX_GPIO_CLK_ENABLE();
  USARTx_RX_GPIO_CLK_ENABLE();

	/* uart clock enable */
  USARTx_CLK_ENABLE(); 
  
	/*set gpio*/
  GPIO_InitStruct.Pin       = USARTx_TX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;

  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);


  GPIO_InitStruct.Pin = USARTx_RX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;

  HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
    
  /*NVIC setting */
  HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(USARTx_IRQn);
}

3.重写USART1_IRQHandler()函数

让函数进入HAL_UART_IRQHandler(&UART_initStruct)中断
其中UART_initStruct是UART1
注意:在此函数中不要执行阻塞操作

void USART1_IRQHandler(void )
{
	HAL_UART_IRQHandler( &UART_initStruct );
}

4.重写HAL_UART_RxCpltCallback()函数

每进入一次改回调函数要重新开启用HAL_UART_Receive_IT( &UART_initStruct, &RxData, 1 );
注意:在HAL_UART_RxCpltCallback()回调函数中不能使用HAL_UART_Transmit_IT()进行发送数据,否则发送出去的就是乱码

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	/* 回发收到的数据 */
	HAL_UART_Transmit(&UART_initStruct, &RxData, 1,1000);
	HAL_UART_Receive_IT( &UART_initStruct, &RxData, 1 );
}

5.使能中断

HAL_UART_Receive_IT( &UART_initStruct, &RxData, 1 );

6.整份代码

#include "uart_1.h"
#include "main.h"

static UART_HandleTypeDef  UART_initStruct;
uint8_t RxData = 0;

void UART_Init(void)
{


	UART_initStruct.Instance			=USART1;
	UART_initStruct.Init.BaudRate = 115200;
	UART_initStruct.Init.WordLength = UART_WORDLENGTH_8B;
	UART_initStruct.Init.StopBits   = UART_STOPBITS_1;
	UART_initStruct.Init.Parity     = UART_PARITY_NONE;
  UART_initStruct.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UART_initStruct.Init.Mode       = UART_MODE_TX_RX;
	if(HAL_UART_Init(&UART_initStruct) != 0)
	{
		return;
	}
	HAL_UART_Receive_IT( &UART_initStruct, &RxData, 1 );
	
}

static uint8_t  strcmp(uint8_t * p,uint8_t *q)
{
	int i = 0;
	if(p == NULL || q == NULL)
	{
		return 1;
	}
	while(*q != '\0')
	{
		*p = *q;
		p++;
		q++;
		i++;
	}
	return i;
}

void k_printf(uint8_t* str)
{
	uint8_t txbuf[MAX] = {0};
	uint8_t len = 0;
//uint8_t data[] = "0123456789";
	len = strcmp(txbuf,str);
	HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_8);
	
	if (HAL_UART_Transmit(&UART_initStruct, (uint8_t*)txbuf, len,1000) != HAL_OK)
  {
    /* Transfer error in transmission process */
    //HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_8);
  }
	
	
	
}


void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{  

  GPIO_InitTypeDef  GPIO_InitStruct;
  
	/* gpio clock enable */
  USARTx_TX_GPIO_CLK_ENABLE();
  USARTx_RX_GPIO_CLK_ENABLE();

	/* uart clock enable */
  USARTx_CLK_ENABLE(); 
  
	/*set gpio*/
  GPIO_InitStruct.Pin       = USARTx_TX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;

  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);


  GPIO_InitStruct.Pin = USARTx_RX_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;

  HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
    
	/*NVIC setting */
  HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(USARTx_IRQn);
	
}

/**
  * @brief UART MSP De-Initialization 
  *        This function frees the hardware resources used in this example:
  *          - Disable the Peripheral's clock
  *          - Revert GPIO and NVIC configuration to their default state
  * @param huart: UART handle pointer
  * @retval None
  */
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
  /*##-1- Reset peripherals ##################################################*/
  USARTx_FORCE_RESET();
  USARTx_RELEASE_RESET();

  /*##-2- Disable peripherals and GPIO Clocks #################################*/
  /* Configure UART Tx as alternate function  */
  HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
  /* Configure UART Rx as alternate function  */
  HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
  
  /*##-3- Disable the NVIC for UART ##########################################*/
  HAL_NVIC_DisableIRQ(USARTx_IRQn);
}

void USART1_IRQHandler(void )
{
	//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
	//
	HAL_UART_IRQHandler( &UART_initStruct );
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	
	//HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
	
	//k_printf((uint8_t*)"6666");
	if(UART_initStruct.Instance	== USART1)
	{
		HAL_UART_Transmit(&UART_initStruct, &RxData, 1,1000);
		HAL_UART_Receive_IT( &UART_initStruct, &RxData, 1 );

	}
}
展开阅读全文
©️2020 CSDN 皮肤主题: 游动-白 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值