我是在开发板上做的实验,485通过485转USB接到了PC机,串口调试助手发送数据
STM32板子上接收到后显示出来 并回发
现在板子上显示接受到了 串口助手发的1
但是串口助手看没有收到回发的数据
麻烦大家给指导下 什么问题
波特率9600 串口3
代码比较短 仅是485实验 以下是主要代码
另外我单独试过 就是发送 不接受,485方向始终为高,但依然串口助手没有收到数据
大家看看 谢谢了
int main(void)
{
Init_All_Periph();
UART3Init();
LCD_Init(); // LCD初始化
LCD_Clear(WHITE);
while(1)
{
RS485_GetByte();
if(i==1)
{
i=0;
RS485_SendByte(RS485_GetByte());
LCD_ShowNum(132,150,RS485_GetByte(),10,16); //显示电压值整数部分
}
}
}
void UART3Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;//LED指示灯
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //485方向控制
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
void Delay(unsigned short time)
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
void RS485_SendByte(unsigned char temp)
{
GPIO_SetBits(GPIOB, GPIO_Pin_2);
Delay(100);
USART_SendData(USART3, temp);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
Delay(100);
GPIO_ResetBits(GPIOB, GPIO_Pin_2);
}
unsigned char RS485_GetByte(void)
{
if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
{i=1;}
return(USART_ReceiveData(USART3)); } |