STM32F767IGT6使用uart5做485通信,无法正常发送接收,下面是代码求解
#include "rs485.h"
#include "sys.h"
#include "includes.h"
u8 RS485RX[8];//HAL¿aê1óÃμÄ′®¿ú½óêÕ»o3å
UART_HandleTypeDef UART5_Handler; //UART¾ä±ú
void RS485_Init(u32 bound)
{
//GPIO????
GPIO_InitTypeDef GPIO_Initure;
//???PCF8574,????RE?
__HAL_RCC_GPIOB_CLK_ENABLE(); //??GPIOB??
__HAL_RCC_UART5_CLK_ENABLE(); //??UART5??
GPIO_Initure.Pin=GPIO_PIN_12|GPIO_PIN_13; //PB12,13
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //??????
GPIO_Initure.Pull=GPIO_PULLUP; //??
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //??
GPIO_Initure.Alternate=GPIO_AF7_UART5; //???USART2
HAL_GPIO_Init(GPIOB,&GPIO_Initure); //???PA2,3
GPIO_Initure.Pin=GPIO_PIN_14;
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //íÆíìêä3ö
GPIO_Initure.Pull=GPIO_PULLUP; //éÏà-
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //¸ßËù
HAL_GPIO_Init(GPIOB,&GPIO_Initure); //3õê¼
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14,GPIO_PIN_RESET);
//USART ?????
UART5_Handler.Instance=UART5; //USART2
UART5_Handler.Init.BaudRate=bound; //???
UART5_Handler.Init.WordLength=UART_WORDLENGTH_8B; //???8?????
UART5_Handler.Init.StopBits=UART_STOPBITS_1; //?????
UART5_Handler.Init.Parity=UART_PARITY_NONE; //??????
UART5_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //?????
UART5_Handler.Init.Mode=UART_MODE_TX_RX; //????
HAL_UART_Init(&UART5_Handler); //HAL_UART_Init()???USART2
__HAL_UART_CLEAR_IT(&UART5_Handler,UART_CLEAR_TCF);
__HAL_UART_ENABLE_IT(&UART5_Handler,UART_IT_RXNE);//??????
HAL_NVIC_EnableIRQ(UART5_IRQn); //??USART1??
HAL_NVIC_SetPriority(UART5_IRQn,3,3); //?????3,????3
RS485_TX_Set(0); //???????
}
u8 rs485=0;
void UART5_IRQHandler(void)
{
u8 Res;
OSIntEnter();
if((__HAL_UART_GET_FLAG(&UART5_Handler,UART_FLAG_RXNE)!=RESET)) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
HAL_UART_Receive(&UART5_Handler,&rs485,1,1000);
}
HAL_UART_IRQHandler(&UART5_Handler);
OSIntExit();
}
void rs485_send_data(u8 *buf,u8 len)
{
RS485_TX_Set(1);
HAL_UART_Transmit(&UART5_Handler,buf,len,1000);
RS485_TX_Set(0);
}
#ifndef _RS485_H
#define _RS485_H
#include "sys.h"
#define RS485_TX_Set(n) (n?HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14,GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14,GPIO_PIN_RESET))
void RS485_Init(u32 bound);
//void HAL_UART5_MspInit(UART_HandleTypeDef *huart);
void rs485_send_data(u8 *buf,u8 len);
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
串口代码,参考我们例程的。
不可能有单片机IO可以直接做485的,必定是串口转485
一周热门 更多>