各位大神,学渣遇到一个USART串口问题,自己写了一个源程序,但是却不能收发数据,求指导:
#include "usart.h"#include "stm32l0xx_hal.h"
USART_HandleTypeDef USART_HandleStructure;__IO ITStatus USART_Ready = RESET;/* Configure and Initialize the USRAT */void USART_Init(void){ GPIO_InitTypeDef GPIO_InitStructure;// USART_HandleTypeDef USART_HandleStructure; __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_USART1_CLK_ENABLE(); GPIO_InitStructure.Pin=GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStructure.Mode=GPIO_MODE_AF_PP; GPIO_InitStructure.Pull=GPIO_NOPULL; GPIO_InitStructure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStructure.Alternate=GPIO_AF4_USART1; HAL_GPIO_Init(GPIOA,&GPIO_InitStructure); USART_HandleStructure.Instance=USART1; USART_HandleStructure.Init.BaudRate=115200; USART_HandleStructure.Init.WordLength=USART_WORDLENGTH_8B; USART_HandleStructure.Init.StopBits=USART_STOPBITS_1; USART_HandleStructure.Init.Parity=USART_PARITY_NONE; USART_HandleStructure.Init.Mode=USART_MODE_TX_RX; HAL_USART_Init(&USART_HandleStructure); if(HAL_USART_Init(&USART_HandleStructure)!=HAL_OK) { Error_Handler(); } /* NVIC for USART1 */ HAL_NVIC_SetPriority(USART1_IRQn,0,1); HAL_NVIC_EnableIRQ(USART1_IRQn);
}/* Send an amount of data in interrupt mode */void USART_Transmit(uint8_t TxBuffer){ if(HAL_USART_Transmit_IT(&USART_HandleStructure,&TxBuffer,sizeof(TxBuffer))!=HAL_OK) { Error_Handler(); } while(USART_Ready != SET) { } USART_Ready=RESET;}/* Receive an amount of data in blocking mode */void USART_Receive(uint8_t RxBuffer){ if(HAL_USART_Receive_IT(&USART_HandleStructure,&RxBuffer,sizeof(RxBuffer))!=HAL_OK) { Error_Handler(); } while(USART_Ready != SET) { } USART_Ready=RESET; }/* USART error callbacks */void Error_Handler(void){ while(1) { }}/* USART interrupt request */void USART1_IRQHandler(void){ HAL_USART_IRQHandler(&USART_HandleStructure);}/* Tx/Rx Transfers completed callback for the non-blocking process */void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *USART_HandleStructure){ USART_Ready=SET;}
在主程序里用 USART_Transmit(Distance);进行发送,可是就是收不到。。。。。。。。求指导
是引脚问题,已经解决!!!
不过用的轮询模式输出的
不能用中断吗?
可以,已经实现!
错误1:GPIO_InitStructure.Pin=GPIO_PIN_9|GPIO_PIN_10;//PA9-TX,PA10-RX
错误2:
/* Send an amount of data in interrupt mode */
void USART_Transmit(uint8_t *TxBuffer)
{
if(HAL_USART_Transmit_IT(&USART_HandleStructure,(uint8_t *)TxBuffer,sizeof(TxBuffer))!=HAL_OK)
while(1);
}
/* Receive an amount of data in interrupt mode */
void USART_Receive(uint8_t *RxBuffer)
if(HAL_USART_Receive_IT(&USART_HandleStructure,(uint8_t *)RxBuffer,sizeof(RxBuffer))!=HAL_OK)
使用取地址输出错误,以数组的格式发送
赞,
一直觉得自己是个牛逼的人,低调的连自己都不敢相信。那天在群里我介绍的自己的思路 还请你好好 理会一下。