簡體   English   中英

如何通過UART發送端口的多個數據?

[英]How to send multiple data of ports through UART?

我已經更改了接收器的代碼,但發送器幾乎相同,所以在接收器中我取消了中斷,而是在循環中讀取數據,每次檢查RC2IF_bit的狀態和檢查是通過發送器發送數據的相同順序進行的,以確保哪個端口正在發送數據。 問題在於,當我接收到數據並在leds上輸出它們工作正常並且數據被正確發送但在模擬過程中我看到它時發現它們改變狀態一目了然,例如如果我發送PORTA = 0X2D我收到相同的值並且右邊的LED開啟但是一秒鍾它們關閉然后再次打開,這是編寫的代碼中的問題。

 /*This is the transmitter code*/
void UART2_TX_init ();      //UART2 transmission initialization function porototype
void SEND_data (int output);     //Send data function prototype


void main()
{
     ANSELA = 0X00;        //Disable analogue function on PORTA
     ANSELE = 0X00;        //Disable analogue function on PORTE
     ANSELF = 0X00;        //Disable analogue function on PORTF
     TRISA = 0XFF;         // SET PORTA AS INPUT (AUTOMATIC BUTTONS)
     TRISB = 0XFF;         //SET PORTB AS INPUT (OFF BUTTONS)
     TRISD = 0XFF;         //SET PORTD AS INPUT (MANUAL BUTTONS)
     TRISE = 0XFF;         //SET PORTE AS INPUT (HIGH BUTTONS)
     TRISF = 0XFF;         //SET PORTF AS INPUT (LOW BUTTONS)
     TRISC = 0XFF;         //SET PORTC AS INPUT (TRIP BUTTONS)
     TRISG0_bit = 0;    //set PORTC pin0 as output for MAX487 DE pin
     PORTG.F0= 1;      //set PORTC pin0 HIGH , set    MAX487 as transmitter.
     UART2_TX_init();      //call UART1 transmission initialization

     while (1)
     {
                SEND_data(PORTA);   //send data of automatic
                delay_ms(10);
               SEND_data(PORTB);    //send data of off
               delay_ms(10);
               SEND_data(PORTD);    //send daata of manual
               delay_ms(10);
               SEND_data(PORTE);   //send data of high
               delay_ms(10);
               SEND_data(PORTF);   //send data of low
               delay_ms(10);
               SEND_data(PORTC);   //send data of TRIP
               delay_ms(10);
    }
}

 /*This function takes the data needed to be send
   as an integer. Wait for the TSR to be empty and
   start the transmission*/

void SEND_data (int output)
{
         while (!TRMT_TX2STA_bit){};    //checks if TSR is empty or not if empty TRMT_BIT is set and write to transmit register
         TX2REG = output;        //write data to be send in the transmission register

}




/*This function initializes the UART2 as an asynchronous
 transmitter at a baud rate of 9600kbps*/

void UART2_TX_init ()
{
    BAUD2CON = 0X08;
    BRGH_TX2STA_bit = 1;
    SP2BRGL = 207;
    SYNC_TX2STA_bit = 0X00;
    SPEN_RC2STA_bit = 0X01;
    TRISG1_bit = 0X01;
    TRISG2_bit = 0X01;
    TXEN_TX2STA_bit = 0X01;

}




/*This is the receiver code*/
void UART2_RX_init ();   // Receiver initialization function prototype
void main()
{

     ANSELA = 0X00;       // Disable analog function on PORTA
     ANSELE = 0X00;       // Disable analog function on PORTE
     ANSELF = 0X00;       // Disable analog function on PORTF
     TRISA = 0X00;        //set PORTA as output
     TRISB = 0X00;        //set PORTB as output
     TRISD = 0X00;        //set PORTD as output
     TRISE = 0X00;        //set PORTE as output
     TRISF = 0X00;        //set PORTF as output
     TRISC = 0X00;        //set PORTF as output
     PORTA = 0x00;        //clear PORTA
     PORTB = 0x00;        //clear PORTB
     PORTD = 0x00;        //clear PORTD
     PORTE = 0x00;        //clear PORTE
     PORTF = 0x00;        //clear PORTF
     PORTC = 0x00;        //clear PORTC
     TRISG0_bit = 0x00;   //set PORTC pin0 as output for MAX487 RE pin
     PORTG.F0 = 0x00;     // set PORTC pin0 as LOW , set MAX487 as receiver
     UART2_RX_init();     //call receiver initialization function
     while (1)
     {
             while (!RC2IF_bit) ;      //check if the RCIF flag is up to tell if there is a new data
             PORTA =~ RC2REG;          //write the new data to the specified port
             while (!RC2IF_bit) ;
             PORTB =~ RC2REG;
             while (!RC2IF_bit) ;
             PORTD =~ RC2REG;
             while (!RC2IF_bit) ;
             PORTE =~ RC2REG;
             while (!RC2IF_bit) ;
             PORTF =~ RC2REG;
             while (!RC2IF_bit) ;
             PORTC =~ RC2REG;
     }


}
/*This function initializes UART2 as an asynchronous
  receiver at a baud rate of 9600kbps with continous
  reception NO INTERRUPT ON RECEIVE*/
void UART2_RX_init ()
{
    BAUD2CON = 0X08;
    BRGH_TX2STA_bit = 1;
    SP2BRGL = 207;
    SYNC_TX2STA_bit = 0X00;
    SPEN_RC2STA_bit = 0X01;
    TRISG1_bit = 0X01;
    TRISG2_bit = 0X01;
    CREN_RC2STA_bit = 0X01;
}

首先,我想談談協議設計。 任何通信協議,如果您希望它是可靠的,應至少實現兩件事:幀同步和錯誤檢查。 好吧,有一些專門的協議不使用框架並將數據表示為連續流,但絕對不是你的情況。 因此,如果你不想搞亂奇怪的錯誤,我強烈建議你實現這些東西。

現在關於你的代碼。 看起來GET_data等待發送寄存器為空並向其寫入一個字節。 我沒有看到任何錯誤。 但你的接收器看起來很可疑。 等待RCIF標志置1,然后從輸入寄存器RCREG讀取5次。 但非零RCIF意味着您收到一個字節。 您不能等待RCIF一次,然后多次從RCREG讀取。 每次RCREG讀取之前,您應該等待RCIF

我很樂意為您提供工作代碼的示例,但我無法提出適合您當前架構的簡單解決方案。 我可以舉例說明如何以正確的方式做到這一點,但它看起來會完全不同。

有些事情:

  • UART接收ISR應盡可能短,只需將數據寫入緩沖區並絕對包含任何延遲例程。
  • 每個接收到的字節使用一個中斷。
  • 用startbyte開始你的框架。 例如'S'
  • 用校驗和字節結束你的幀。
  • 在緩沖區中檢測到完整的幀后,在mainloop中執行portwrite(startbyte + databytes + correct chechsum)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM