Use Msp430 serial port interrupt to receive a packet of data

Publisher:dadigtLatest update time:2016-08-16 Source: eefocusKeywords:Msp430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Assume there is a data packet, the data format is as shown in the table:

                                                                                                             

Serial number

project

Length (bytes)

illustrate

1

Packet header (STX)

1

Constant: 0x02

2

Data unit length (Data_len)

2

The length of the Data part of the data unit to be transmitted, with the high byte in front and the low byte in the back.

For example: 0x0010 means the Data part has 16 bytes.

3

Data unit to be transmitted (Data)

indefinite

The length is indicated by Data_len. The first two bytes of the data unit are the command code (the terminal sends a command to the reader) or the status code (the reader returns data to the terminal), followed by other parameters.

4

Redundancy Check Value (LRC)

1

The XOR value of each byte of the data in the Data part.

5

Packet tail (ETX)

1

Constant: 0x03

The total length of the data packet is: Data_len + 5 bytes, and the maximum length cannot exceed 512 bytes.

 

The program implementation is as follows:

[cpp]  view plain copy print ?
  1. #include    
  2.   
  3. typedef struct newStruct  
  4. {            
  5.       unsigned char startFlag;  
  6.       unsigned char finishFlag;  
  7.       unsigned char lenHighFlag;  
  8.       unsigned char lenLowFlag;  
  9.       unsigned char dataFlag;  
  10.       unsigned char lrcFlag;  
  11.       unsigned char buf[512];  
  12.       unsigned char lenHigh;  
  13.       unsigned char lenLow;  
  14.       unsigned char dataStartIndex;  
  15.       unsigned short len;  
  16.       unsigned short index;  
  17.       unsigned short tempLen;  
  18. }rxstruct;  
  19. rxstruct rxArray;  
  20.   
  21. void m430_InitUart()  
  22. {  
  23.       P3SEL |= BIT4|BIT5; // P3.4, P3.5 = USCI_A0 TXD/RXD   
  24.         
  25.       UCA0CTL1 |= UCSSEL_2; // SMCLK   
  26.         
  27.       //The following three lines are used to set the baud rate   
  28.       UCA0BR1 = 0;   
  29.       UCA0BR0 = 104; // 12MHz:1250->9600,625->19200,312->38400,214->56000,104->115200                
  30.       UCA0MCTL = 0x02; // Modulation UCBRSx = 1   
  31.      
  32.       UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine    
  33.       //IE2 |= UCA0RXIE | UCA0TXIE; //Note that the interrupt enable should be set after initializing USCI, otherwise it will not work. That is, if this sentence is placed before UCA0CTL1 &= ~UCSWRST;, the interrupt will not be responded to.   
  34.       IE2 |= UCA0RXIE;  
  35. }  
  36.   
  37. unsigned char uart_CalLrc(unsigned char *buf, unsigned short len)  
  38. {  
  39.       unsigned short i;  
  40.       unsigned char lrc;  
  41.       lrc = 0x00;  
  42.         
  43.       for( i=0; i
  44.       {            
  45.           lrc ^= buf[i];  
  46.       }  
  47.         
  48.       return lrc;  
  49. }  
  50.   
  51. void main()  
  52. {     
  53.       WDTCTL = WDTPW + WDTHOLD;  
  54.       BCSCTL1 = CALBC1_12MHZ;  
  55.       DCOCTL = CALDCO_12MHZ;  
  56.         
  57.       m430_InitUart();  
  58.         
  59.       rxArray.startFlag = 0;              
  60.       rxArray.finishFlag = 0;  
  61.         
  62.       _EINT();                        
  63.       _BIS_SR(LPM4_bits);  
  64.       while(1)  
  65.       {            
  66.       }  
  67. }  
  68.   
  69. #pragma vector = USCIAB0RX_VECTOR   
  70. __interrupt void uartRxHandle()  
  71. {  
  72.       unsigned char num;        
  73.       num = UCA0RXBUF;        
  74.                     
  75.       if( 0==rxArray.startFlag )//Judge whether the frame header is received   
  76.       {  
  77.             if( 0x02==num )//Judge whether the frame header is correct   
  78.             {  
  79.                 rxArray.startFlag = 1; //Sign that the frame header has been received   
  80.                 rxArray.finishFlag = 0;  
  81.                 rxArray.lenHighFlag = 0;  
  82.                 rxArray.lenLowFlag = 0;  
  83.                 rxArray.dataFlag = 0;  
  84.                 rxArray.lrcFlag = 0;  
  85.                 rxArray.index = 0;  
  86.                 rxArray.len = 0; //Storage frame length   
  87.                 rxArray.buf[rxArray.index] = num;  
  88.                 rxArray.index++;  
  89.             }  
  90.             return;  
  91.       }  
  92.         
  93.       if( 0==rxArray.lenHighFlag )//Judge whether the high byte information of the frame length is received   
  94.       {  
  95.             rxArray.lenHighFlag = 1; // Flag that the high byte of the frame length has been received   
  96.             rxArray.lenHigh = rxArray.buf[rxArray.index] = num;  
  97.             rxArray.index++;                            
  98.             return;  
  99.       }  
  100.         
  101.       if( 0==rxArray.lenLowFlag )//Judge whether the low byte information of the frame length is received   
  102.       {  
  103.             rxArray.lenLowFlag = 1; // Flag that the low byte of the frame length has been received   
  104.             rxArray.lenLow = rxArray.buf[rxArray.index] = num;  
  105.             rxArray.index++;  
  106.             rxArray.dataStartIndex = rxArray.index;  
  107.               
  108.             rxArray.tempLen = rxArray.len = (rxArray.lenHigh<<8) + rxArray.lenLow; //Add one byte to the end of the frame   
  109.             if( rxArray.len+5>512 )//If the data length is greater than 12, it means that the received data length information is incorrect and needs to be received again   
  110.             {  
  111.                 rxArray.startFlag = 0;  
  112.                 rxArray.lenHighFlag = 0;  
  113.                 rxArray.lenLowFlag = 0;  
  114.             }       
  115.             return;  
  116.       }  
  117.         
  118.       if( 0==rxArray.dataFlag )  
  119.       {  
  120.             rxArray.buf[rxArray.index] = num; //Store the data in the array   
  121.             rxArray.index++;  
  122.             rxArray.tempLen--;  
  123.             if( 0==rxArray.tempLen )  
  124.             {  
  125.                   rxArray.dataFlag = 1;  
  126.             }  
  127.             return;  
  128.       }  
  129.         
  130.       if( 0==rxArray.lrcFlag )//Receive lrc   
  131.       {  
  132.             rxArray.buf[rxArray.index] = num;  
  133.             rxArray.index++;  
  134.             rxArray.lrcFlag = 1;  
  135.             if( 0!=uart_CalLrc( &rxArray.buf[rxArray.dataStartIndex], rxArray.len+1 ) )//Judge whether the lrc of the received data is correct   
  136.             {  
  137.                 rxArray.startFlag = 0;  
  138.                 rxArray.lenHighFlag = 0;  
  139.                 rxArray.lenLowFlag = 0;  
  140.                 rxArray.dataFlag = 0;  
  141.                 rxArray.lrcFlag = 0;  
  142.             }  
  143.             return;  
  144.       }  
  145.         
  146.       rxArray.buf[rxArray.index] = num;       
  147.       rxArray.finishFlag = 1;  
  148.       rxArray.startFlag = 0;  
  149.       rxArray.lenHighFlag = 0;  
  150.       rxArray.lenLowFlag = 0;  
  151.       rxArray.dataFlag = 0;  
  152.       rxArray.lrcFlag = 0;        
  153.       if( rxArray.buf[rxArray.index]!=0x03 )//The last byte is not 0x03, indicating that the data is incorrect and needs to be received again   
  154.       {               
  155.             rxArray.finishFlag = 0;               
  156.       }     
  157.         
  158.       if( rxArray.finishFlag )  
  159.       {  
  160.             //Received data processing part                                                          
  161.       }  
  162. }  

A fatal bug in this program is that if an error occurs when receiving the two bytes of length information, a packet of data cannot be correctly formed, and subsequent data packets cannot be received correctly.

For example, there is a packet of data (hexadecimal): 02 00 02 11 11 00 03,

If an error occurs during the receiving process, the two bytes of length 00 02 will be changed to 00 05 at the receiving end. Therefore, the receiving end will wait until 5 bytes of data are received before considering that a packet of data has been completely received. As a result, part of the complete data packet sent later will be split. This vicious cycle will continue over and over again, and no good solution has been found yet!

Keywords:Msp430 Reference address:Use Msp430 serial port interrupt to receive a packet of data

Previous article:MSP430F2131 reads and writes DS1991
Next article:Which interfaces can be used to program MSP430?

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号