This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

stm32 nucleo-l053R8板子的USART串口问题

各位大神,学渣遇到一个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);进行发送,可是就是收不到。。。。。。。。求指导

Parents Reply Children
No data