在写单片机程序时我们一般喜欢使用printf来通过串口打印调试信息,但这个函数是不可以直接使用的,必须做点对库函数的改动。
详细工程下载地址: http://download.csdn.net/detail/liucheng5037/8847961
STM32M CUBE是ST官方提供的库以及初始化工具,很好很强大,但是在UART方面值提供了如下函数:
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 添加如下信息
#include <stdio.h> #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; }
void LED_Task2(void const * argument) { while(1) { HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14); printf("LED_Task2\r\n"); osDelay(2000); } }
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 */ }
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 */ } }
STM32M CUBE实现printf打印调试信息以及实现单字节接收
原文地址:http://blog.csdn.net/liucheng5037/article/details/46673249