Two division analysis of serial port based on STM32

First summarize the serial ports 232, 422, 485

Serial port 232: bidirectional transmission, full duplex, maximum rate 20Kbps, negative logic level, -15V~-3V logic "1", +3V~+15V logic "0".

Serial port 422: bidirectional transmission, 4-wire full-duplex, 2-wire simplex.

Serial port 485: bidirectional transmission, 4-wire full-duplex, 2-wire simplex, maximum rate 10Mb/s, differential signal, sender: +2V~+6V logic "1", -2V~-6V logic "0", Receiver: +200mV logic "1", -200mV logic "0".

There are two schemes for the realization of serial port:

Scheme 1, like Atomic's "Example STM32", is first received, then processed, without message verification processing, so there will be message coverage, and the machine will crash after a message error, unable to clearly distinguish commands, and unable to respond to handshake signals in time. Option 2, learn from the message queue of uC/OSII, after entering the interrupt service function, close the interrupt and receive data. If there is no data to receive, wait for a period of time (the time is related to the baud rate), then open the interrupt, and output the interrupt, and then on the receiving The received data is processed, see the code below:

Two division analysis of serial port based on STM32

Message queue and its initialization function:

/*osq structure to manage the message queue*/

typedefstructos_q{/*QUEUECONTROLBLOCK*/

u8*OSQStart;/*Pointertostartofqueuedata*/

u8*OSQEnd;/*Pointertoendofqueuedata*/

u8*OSQIn;/*PointertowherenextmessagewillbeinsertedintheQ*/

u8*OSQOut;/*PointertowherenextmessagewillbeextractedfromtheQ*/

u8OSQSize; /*Sizeofqueue(maximumnumberofentries)*/

u8OSQEntries;/*Currentnumberofentriesinthequeue*/

}OS_Q;

OS_Q*posq, osq;

u8USART_RX_BUF[length_buff];//Circular queue to store the received information.

voidOS_QInit()//Initialize the structure

{

posq=&osq;

posq->OSQStart=USART_RX_BUF;

posq->OSQEnd=&USART_RX_BUF[length_buff];

posq->OSQIn=USART_RX_BUF;

posq->OSQOut=USART_RX_BUF;

posq-"OSQSize=length_buff;

posq->OSQEntries=0;

}

///* Save all bytes of a message in the message queue at once in the interrupt function*/

u8message_buff[20];

voidUSART1_IRQHandler(void)

{

u8num=0;

//u8i;

u8TIme=0;//Accept timeout technology

USART1->CR1&=0XFFDF;

LED=! LED;

while(1)

{

if (USART1->SR& (1<<5))//If data is received, store the message in the message queue

{

message_buff[num]=(u8) USART1->DR;

num++;

*posq->OSQIn++=(u8)USART1->DR;

posq->OSQEntries++;

if (posq->OSQIn==posq->OSQEnd)

{

posq-"OSQIn=posq-"OSQStart;

}

TIme=0;

}

else

{

delay_us(10);

TIme++;

if(TIme"=50)break;

}

}

USART1-"CR1|=0X0020;

}

In this way, all the data is stored at one time, and the rest is to process the messages in the message buffer message_buff[], which solves the problem of message coverage and uncorrectable message errors. As for how to deal with messages, it depends on different The needs of different processing, and also note that the handshake signal is easy to use the timer interrupt.

Universal Stylus Pen

Product categories of Universal Stylus Pen, We are Specialized Stylus Pen manufacturers from China, The Universal Stylus Pen can be worked on android phones / apple phones and all brands capacitive touch screens. We have perfect after-sale service and technical support. Looking forward to your cooperation.

Phone Stylus Pen,Touchscreen Stylus Pen,Stylus Pen For Android,Tablet Stylus Pen

Shenzhen Ruidian Technology CO., Ltd , https://www.wisonen.com