在寫單片機程序時我們一般喜歡使用printf來通過串口打印調試信息,但這個函數是不可以直接使用的,必須做點對庫函數的改動。
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#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 ); 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 */ } }