stm32f4 rs485 无法实现即时收发功能

rs485原理图:

image.png

贴上代码:

UART_HandleTypeDef huart1;


/* USART1 init function */


void MX_USART1_UART_Init(void)

{


  huart1.Instance = USART1;

  huart1.Init.BaudRate = 115200;

  huart1.Init.WordLength = UART_WORDLENGTH_8B;

  huart1.Init.StopBits = UART_STOPBITS_1;

  huart1.Init.Parity = UART_PARITY_NONE;

  huart1.Init.Mode = UART_MODE_TX_RX;

  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart1.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }


}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

{


  GPIO_InitTypeDef GPIO_InitStruct;

  if(uartHandle->Instance==USART1)

  {

  /* USER CODE BEGIN USART1_MspInit 0 */


  /* USER CODE END USART1_MspInit 0 */

    /* USART1 clock enable */

    __HAL_RCC_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_FREQ_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    /* USART1 interrupt Init */

    HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);

    HAL_NVIC_EnableIRQ(USART1_IRQn);

  /* USER CODE BEGIN USART1_MspInit 1 */


  /* USER CODE END USART1_MspInit 1 */

  }

}



void USART_TaskInit(void)

{

    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);

while(HAL_UART_Receive_IT(&huart1,huart1_rx_buffer,1) != HAL_OK)

{

LED1_REV();

LED2_REV();

}

}

HAL_StatusTypeDef USART1_SendData(uint8_t *data,uint16_t size)

{

HAL_StatusTypeDef return_state;

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);

return_state = HAL_UART_Transmit_IT(&huart1,data,size);

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);

return return_state;

}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

uint32_t timeout=0;

if(huart == &huart1)

{

timeout=0;

while(USART1_SendData(huart1_rx_buffer,1) != HAL_OK)

{

timeout++; //超时处理

if(timeout>HAL_MAX_DELAY) break;

}

timeout=0;

while(HAL_UART_Receive_IT(&huart1,huart1_rx_buffer, 1) != HAL_OK)//一次处理完成之后,重新开启中断并设置RxXferCount为1

{

timeout++; //超时处理

if(timeout>HAL_MAX_DELAY) break;

}

LED1_REV();

}


}


主函数:

int main(void)

{

  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();


  /* USER CODE BEGIN Init */


  /* USER CODE END Init */


  /* Configure the system clock */

  SystemClock_Config();


  /* USER CODE BEGIN SysInit */


  /* USER CODE END SysInit */


  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */

  USART_TaskInit();

  

  /* USER CODE END 2 */


  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {


  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */

  }

  

  /* USER CODE END 3 */


}

测试效果显示:

image.png


问题:

image.png


串口助手无法接收到发出去的数据,目的是测试RS485的收发功能。




已邀请:

要回复问题请先登录注册