Home > other >  STM32F103 after 485 double machine communication from the machine can't receive
STM32F103 after 485 double machine communication from the machine can't receive

Time:09-17

Host from the machine send to normal, can't receive, a serial port 2 and MAX485 connection PA4 MAX485 transceiver control, use 2 receive the PC serial port to send data alone, serial port forwarding is normal, 1
From the machine program and circuit is as follows:
Under-16 USART_RX_STA;//serial port 1 receiving status flag
U8 USART_RX_BUF [USART_REC_LEN];//serial port 1 receive buffer
200//# define USART_REC_LEN defined maximum bytes received 200
# define RS485_TX GPIO_SetBits (GPIOA, GPIO_Pin_4)//485 can send make (when use note response latency)
# define RS485_RX GPIO_ResetBits (GPIOA, GPIO_Pin_4)//can receive 485 (when use note response latency)
//# define USART2_RX_EN 1 0: don't accept; 1: receiving


/* *
//function: RS485 serial port initialization
2//parameters: bound: baud rate
//the return value: None
* */
Void RS485_Init (u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2, ENABLE);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * GPIO Config * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);
# if USART2_RX_EN
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * USART Config * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
USART_InitStructure. USART_BaudRate=bound;
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_Rx | USART_Mode_Tx;
USART_Init (USART2, & amp; USART_InitStructure);
USART_Cmd (USART1, ENABLE);//can make serial peripheral
USART_ITConfig (USART1, USART_IT_RXNE, ENABLE);//can make a serial port receiving interrupt
USART_ClearFlag (USART1, USART_FLAG_TC);//the software to send a sign
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * NVIC Config * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
NVIC_InitStructure. NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);

# endif
RS485_RX;//default receive 485
}

/* *
//features: a serial port 2 service interruption function
//parameters: None
//the return value: None
* */
Void USART2_IRQHandler ()
{
Under-16 temp.

If (USART_GetITStatus (USART2, USART_IT_RXNE)!=RESET)//receiving interrupt, must start with a zero x0d. Zero x0a end
{
Temp=USART_ReceiveData (USART2);//read the received data
If ((USART_RX_STA & amp; 0 x8000)==0)//receiving unfinished
{
If (USART_RX_STA & amp; 0 x4000)//if received 0 x0d
{
If (temp!=0 x0a) USART_RX_STA=0;//to accept mistakes, start receiving
The else
{
USART_RX_STA |=0 x8000;//receive complete
}
}
The else//not received 0 x0d
{
If (temp==0 x0d) USART_RX_STA |=0 x4000;
The else
{
USART_RX_BUF x3fff] [USART_RX_STA & amp; 0=temp;
USART_RX_STA + +;

If (USART_RX_STA & gt; USART_RX_STA (USART_REC_LEN - 1))=0;//receiving data errors, start receiving
}
}
}

} else if (USART_GetFlagStatus (USART2, USART_FLAG_IDLE)!=RESET)
{
USART_ClearFlag (USART2, USART_FLAG_IDLE);
}

}


//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the main program * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
Int main ()
{
Under-16 t=0;
U8 length;
Under-16 times=0;
SysTick_Init ();
Delay_us (100);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//set the NVIC priority group and a preemption priority, two response priorities
UART1_Init (115200);
UART2_Init (9600);
RS485_Init (9600);
Printf (" \ r \ n01 finsh \ r \ n ");
Printf (" \ r \ n waiting for receiving \ r \ n ");
While (1)
{
If (USART_RX_STA & amp; 0 x8000)
{
RS485_RX;
Delay_ms (100);
Length=USART_RX_STA & amp; 0 x3fff;
Printf (" \ r \ nyour massage is: \ r \ n ");
For (t=0; t{
Delay_us (1000);
SendByte (USART1, USART_RX_BUF [t]);
}
Printf (" completion of \ r \ n \ r \ n ");
RS485_RX;
USART_RX_STA=0;

} the else
{
Times++;
If (times %==0 5000)
{
Printf (" \ r \ nlab \ r \ n ");
Printf (" ALIENTEK \ r \ n \ r \ n ");
}
If (times %==0 200) printf (" \ r \ n please input data, ended with the enter key \ n ");
If (30 times %==0) printf (" \ r \ nsystem running \ n ");//hint system running
Delay_ms (10);
}
}
}

CodePudding user response:

To debug serial port, serial port through, then insert the RS485 communication

CodePudding user response:

1, whether the clock configuration
RS485 interface configuration of the clock?
The serial clock configuration?
2, a serial port configuration
The sending and receiving can make?
The receiving interrupt and send interrupts enabled?

3, it is recommended that the following tests

(1), RS485 ports is always [received] states
PC to single-chip microcomputer sends data to see whether a serial port receiving interrupt, and view the receiving character is correct,
nullnullnull
  • Related