STM32F103VET6 HAL库使用

                               STM32F103VET6 HAL库使用

主要包括内容:

1.UART1printf重定向;

2.UART3收发使能;

 

1.UART1 printf重定向:

/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stdint.h"

#define Uart1Printf 1
/* USER CODE END Includes */



/* USER CODE BEGIN 0 */
#if Uart1Printf
	#ifdef __GNUC__
	#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
	#else
	#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
	#endif 
	PUTCHAR_PROTOTYPE
	{
		HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
		return ch;
	}
#endif
/* USER CODE END 0 */

2.UART3收发使能:

first of all:

你必须用stm32cubemx将要用的uart的中断(NVIC)打开;

1:宏定义一个开关;

#define Uart3RevSend 1


2:定义一个数据缓冲数组;

#if Uart3RevSend 
	uint8_t PrintData[] = "Hi,Boy!\r\n";
	uint8_t RevData[1];
#endif

3:在main()内部初始化接收中断,我这里开了uart1,uart3两个串口中断

  /* USER CODE BEGIN 2 */
	#if Uart3RevSend
	HAL_UART_Transmit_IT(&huart3,(uint8_t*)PrintData,sizeof(PrintData));//测试串口3是否发送正常;

	HAL_UART_Receive_IT(&huart3,(uint8_t*)RevData,1);
	HAL_UART_Receive_IT(&huart1,(uint8_t*)RevData,1);
	#endif
  /* USER CODE END 2 */

4:重写中断接收函数,这里是
如果uart1接收到数据,把数据给uart3,在串口助手观察uart3的com口看是否有数据;
如果uart3接收到数据,把数据给uart1,在串口助手观察uart1的com口看是否有数据;

/* USER CODE BEGIN 4 */
#if Uart3RevSend
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	UNUSED(huart);
	if(huart == &huart1)
	{
		HAL_UART_Transmit(&huart3,(uint8_t*)RevData,1,0xffff);
		HAL_UART_Receive_IT(&huart1,(uint8_t*)RevData,1);
	}
	if(huart == &huart3)
	{
		HAL_UART_Transmit(&huart1,(uint8_t*)RevData,1,0xffff);
		HAL_UART_Receive_IT(&huart3,(uint8_t*)RevData,1);
	}
}

 

你可能感兴趣的:(C,STM32)