原文地址::https://www.2cto.com/kf/201506/412341.html
相关文章
1、使用stm32cubemx开发四:串口标准化输出----http://blog.csdn.net/u014256685/article/details/45893097
2、STM32Cube Printf uart重定向----http://blog.csdn.net/u014298427/article/details/50493905
在写单片机程序时我们一般喜欢使用printf来通过串口打印调试信息,但这个函数是不可以直接使用的,必须做点对库函数的改动。
STM32M CUBE是ST官方提供的库以及初始化工具,很好很强大,但是在UART方面值提供了如下函数:
1
2
3
4
5
6
|
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
|
其实要实现这些还是很简单的,首先是实现printf
在main.c 添加如下信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE
int
__io_putchar(
int
ch)
#
else
#define PUTCHAR_PROTOTYPE
int
fputc(
int
ch, FILE *f)
#endif
/* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
huart1.Instance->DR = (uint8_t) ch;
/* Loop until the end of transmission */
while
(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) == RESET){}
return
ch;
}
|
1
2
3
4
5
6
7
8
9
10
|
void
LED_Task2(
void
const
* argument)
{
while
(
1
)
{
HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);
printf(LED_Task2
);
osDelay(
2000
);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void
USART1_IRQHandler(
void
)
{
/* USER CODE BEGIN USART1_IRQn 0 */
static
int
count=
0
;
/* USER CODE END USART1_IRQn 0 */
// HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
if
(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) == SET)
//有接受到字符串
{
uart_recbuf[count++] = (uint8_t)(huart1.Instance->DR & (uint8_t)
0x00FF
);
//接收
huart1.Instance->DR = uart_recbuf[count-
1
];
//发送接收的数据
if
(count ==
100
) count =
0
;
}
/* USER CODE END USART1_IRQn 1 */
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
void
HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if
(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* Peripheral clock enable */
__USART1_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral interrupt init*/
HAL_NVIC_SetPriority(USART1_IRQn,
5
,
0
);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
huart->Instance->CR1 |= USART_CR1_RXNEIE;
//使能接收中断
/* USER CODE END USART1_MspInit 1 */
}
}
|