F00007: a single process processes data received by multiple usart interruptions, f00007usart

Source: Internet
Author: User

F00007: a single process processes data received by multiple usart interruptions, f00007usart

Resolution: usart automatically sends data solutions on the Cortex-M4. 1. usart is interrupted too quickly. The data has not been processed. The usart interrupt overwrites the unprocessed data. Data loss. 2. This program needs to process the data of four usart ports in main, so that the error probability is greater. The following solution is proposed: 1. specify the length of each completed data entry. The step length exceeds 256 bytes. Second: we define the ending table of the data as the last three digits of the data, and FFFFFF third: define a global data, it is a three-dimensional array. The data is used to store data sent from four usart ports. Uint8_t usart_data [4] [5] [256]; The following explains why to define a three-dimensional array: The first subscript 4: It is used to accept data from different usart ports. The second subscript 5: It indicates that the maximum number of data records that can be cached by each usart is 5. The third subscript 256: it indicates that a maximum of 256 bytes of data can be saved. The following is how the program is implemented. Define the following variables: 1. Accept the definition of usart data buffer. It is a global three-dimensional data uint8_t usart_data [4] [5] [256] = {0}; 2: records the number of data records received by each usart port. Uint8_t data_in_count [4] = {0}; 3. record the number of currently accepted data. Uint8_t data_len [4] = {0}; 4: used to record the number of errors. Uint8_t error_count = 0; 5: record the data processed by each usart port. Uint8_t data_out_count [4]; void UART_DATA_handle (uint8_t route) {if (data_in_count [route] <5) {if (usart_data [route] [data_in_count [route] [data_len [route]-3] = 0xFF) & (usart_data [route] [data_in_count [route] [data_len [route]-2] = 0xFF) & (usart_data [route] [data_in_count [route] [data_len [route]-1] = 0xFF )) {if (usart_data [route] [data_in_count [route] [0] = data_len [route]) {data_in_count [route] ++; data_len [route] = 0; if (data_in_count [route]> 5) {data_in_count [route] = 5 ;}else {error_count ++ ;}}} void usart=irqhandler (void) {if (USART_GetITStatus (USART1, USART_IT_RXNE )! = RESET) {USART_ClearITPendingBit (USART1, USART_IT_RXNE); usart_data [0] [data_in_cout [0] [data_len [0] ++] = USART_Rece_Data (USART1 ); if (data_len [0]> 2) {usart_data_route (0) ;}}return;} void USART2_IRQHANDLER (void) {if (USART_GetITStatus (USART2, USART_IT_RXNE )! = RESET) {USART_ClearITPendingBit (USART2, USART_IT_RXNE); usart_data [1] [data_in_cout [1] [data_len [1] ++] = USART_Rece_Data (USART2 ); if (data_len [1]> 2) {usart_data_route (1) ;}}return;} void USART3_IRQHANDLER (void) {if (USART_GetITStatus (USART3, USART_IT_RXNE )! = RESET) {USART_ClearITPendingBit (USART3, USART_IT_RXNE); usart_data [2] [data_in_count [2] [data_len [2] ++] = USART_Rece_Data (USART3 ); if (data_len [2]> 2) {usart_data_route (2) ;}}return;} void USART4_IRQHANDLER (void) {if (USART_GetITStatus (USART4, USART_IT_RXNE )! = RESET) {USART_ClearITPendingBit (USART4, USART_IT_RXNE); usart_data [3] [data_in_count [3] [data_len [3] ++] = USART_Rece_Data (USART4 ); if (data_len [3]> 2) {usart_data_route (3) ;}} return ;} /***** main *******/uint8_t tmp [512] = {0}; static _ INLINE void process_usartdata (uint8_t * tmpdata, uint8_t * usartdata, char * usart_format, int flag) {int tmp = 0, count = 0, I = 0; memset (tmpdata, 0x00,512); printf (usart_format, flag ); for (I = 0; I <usartdata [0] + 1; I ++) {tmp = sprintf (& tmpdata [count], "% 02X ", * (usartdata + I); count + = tmp;} memset (usartdata, 0x00,256); printf (tmpdata);} int main (void) {......... uint8_t I = 0; while (1) {for (I = 0; I <4; I ++) {if (data_in_count [I]> 0) {process_usartdata (tmp, usart_data [I] [data_out_count [I], "usart: % d", I) ;}data_in_count [I] --; data_out_count [I] ++; if (data_out_count [I]> 5) {data_out_count [I] = 0 ;}}}........}


STM32 USART receiving interrupt program Problems

Void usartjavasirqhandler (void) receives the interrupt function and keeps repeating. It may be the processing of your interrupt function.
Too many programs lead to too much latency. It is recommended that you put only some flags in the interrupt processing function, and change the flags when the interrupt occurs. In the main loop, the flags are processed and determined. And it is better to increase the system clock frequency. The 115200 speed you use is a bit high!
 
STM8L usart interruption

The last sentence USART_ClearITPendingBit (USART1, USART_IT_RXNE) is changed

USART_ITConfig (USART1, USART_IT_RXNE, DISABLE );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.