Summary of MSP430 Single Chip Multicomputer Communication

1. Hardware conditions: MSP430F149;

2. Compiler environment: IAR5.3;

3. Problems found in the process

(1) Problems with serial port configuration

 The program of the host also needs to be set to multi-machine mode, and the host also needs to have an address!

a), host serial port configuration program

void usart1_init(void)
{   U1CTL |=SWRST; //SWRST reset, USART allows   U1CTL=CHAR+MM; //8 data bits, 1 stop bit, address bit mode, 0x01 // U1CTL=CHAR; // U1BR0 = 0X03; // 0x03 bits when 9600,0x06 4800 // U1BR1 = 0X00; // U1MCTL = 0x4A; // use when 32KHZ crystal, the baud rate is 9600bps is 0x4A, the baud rate is 4800bps is 0x6F / / UTCTL1=0X10;   //11520   U1BR0=0X45;   U1BR1=0X00;   U1MCTL=0X00;   U1TCTL|= SSEL1;   U1CTL &= ~SWRST; // Setup is complete   U1RCTL |= URXWIE; // Only the address character makes URXIFG set   ME2 |= UTXE1 + URXE1;//Allow serial port to receive and send   P3SEL |=0XC0   ; //P3.6 and P3.7 are occupied by the USART1 sending module P3DIR |=0X40; //P3.6 output, P3.7 input   IE2 | =URXIE1; //Receive interrupt   _EINT(); //Don’t forget to enable interrupt )












  

  

  





b), the host sends data process

Send the address first, then send the data. The procedure for the host to send data to the slave is as follows:

//************************************************ **//
//address indicates the slave address
//*pBuffer indicates the pointer of the data to be sent
//n_byteb indicates the data length
//********************* *****************************//
void USART1_Send(uchar addr,uchar *pBuffer,uchar n_byte)
{   unsigned int i;   UTCTL1 |=   TXWAKE ; TXBUF1=addr;   while((UTCTL1&0X01)==0); //The query is waiting to be sent once    UTCTL1&=~TXWAKE;   for(i=0;i<n_byte;i++)   {     while((UTCTL1&0X01)== 0);     TXBUF1=*pBuffer;     pBuffer++;           } }

  










c), host serial port receiving interrupt

First compare the first data received with the address of the machine. If the address is the same, the data is sent to the machine. By setting U1RCTL&=~URXWIE;, the data received later is the data; if the address of the machine is not dead , It is still in the receiving address state, and the data sent by the slave later will not trigger the receiving interrupt. The procedure is as follows:

//Receive interrupt service function

#pragma vector=UART1RX_VECTOR 
__interrupt void USART1_RXIRQ (void) 
{   Data1[NRxBuff++]=RXBUF1;   if(URCTL1&URXWIE) //When receiving in address mode, wait for the correct address to appear, URXWIE,RXWAKE   {     if(RXBUF1==ADDRESS) //Address Correct, change the receiving mode to data, prepare to accept     {       U1RCTL&=~URXWIE;       flag=1;     }     else     {       U1RCTL |= URXWIE; //If it is not the local address, no interrupt will be generated when receiving data in the future     }   }   if((flag= =1)&&(!(URCTL1&URXWIE)))   {       num++;       if(num>1)//Avoid using the address as the first data           Data1[NRxBuff++]=RXBUF1; //Data is stored in the Data array       if(NRxBuff== N_XY_BAO)       {         USART0_SendUint(RXBUF1);





















        NRxBuff=0;
        flag_send=1;
        flag=0;
        num=0;    
        URCTL1|=URXWIE; //Change receiving to address mode, URXWIE        
        for(j1=0;j1<N_XY_BAO;j1++)
          aRxBuff[j1]=Data1[j1 ]; //Copy the string
        memset(Data1,0,36);//clear the array
      )
    }
  
    else
    {       URCTL1|=URXWIE; //Change the receiving mode to address, URXWIE       memset(Data1,0,36);// Clear the array       NRxBuff=0;     }



}


d), slave serial port configuration program

void usart1_init(void)
{   P3SEL |=0XC0   ; //P3.6 and P3.7 are occupied by the USART1 sending module P3DIR |=0X40; //P3.6 output, P3.7 input   UCTL1 |=CHAR+MM+SWRST; //8 data bits, 1 stop bit, address bit mode, 0x01, // UCTL1 |=CHAR+SWRST; // U1BR0=0X03; //0x03 bit 9600, 0x06 is 4800 // U1BR1=0X00; / / U1MCTL=0X4A; //When using 32KHZ crystal oscillator, the baud rate is 0x4A when the baud rate is 9600bps, and the baud rate is 0x6f when the baud rate is 4800bps. // U1TCTL=0x10; //Select ACLK (32KHZ crystal oscillator) as the clock source, 0x1    / /115200   U1BR0=0X45;   U1BR1=0X00;   U1MCTL=0X00;   U1TCTL|= SSEL1;     U1RCTL|=URXWIE; //Change receiving to address mode   U1CTL &= ~SWRST;   ME2 |= UTXE1 + URXE1;//Allow serial port to receive and Send     IE2 |=URXIE1; //Receive interrupt   //_ EINT (); //Open the general interrupt, other programs in the project have already opened the general interrupt }













  





The data sending of the slave is basically the same as that of the serial port receiving interrupt, so I won't repeat it here.

2) If multiple slaves are connected together, the communication is still abnormal

Through the test, it is found that it is caused by the TxD of the serial port of the slave. In the state of not sending data, TxD is high, causing the level on the entire bus to be inaccurate, so the slave needs to set the TxD of the serial port after sending the data It is a normal IO port and set to input state!

3) When there are multiple interrupts in the host program, the communication may not proceed normally

I was working on a project recently and the host used 3 interrupts: timeB timer overflow interrupt, usart0 and usart1 serial port receiving interrupt. In this way, the serial port may not receive correct and complete data. The solutions are:

a). Turn off other interrupts after the serial port receives the local address, and turn on other interrupts immediately after receiving the data;

b). If other interrupts are more important and cannot be closed arbitrarily, it is necessary to perform fault-tolerant processing on the data received by the serial port, to determine whether the received data is completed, whether the receiving process has been interrupted, etc.

 

 

 

 

          

Guess you like

Origin blog.csdn.net/zwb_578209160/article/details/85193659