如何通过UART发送端口的多个数据?

问题描述 投票:1回答:2

我已经更改了接收器的代码,但发送器几乎相同,所以在接收器中我取消了中断,而是在循环中读取数据,每次检查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;
}
c microcontroller pic uart
2个回答
2
投票

首先,我想谈谈协议设计。任何通信协议,如果您希望它是可靠的,应至少实现两件事:帧同步和错误检查。好吧,有一些专门的协议不使用框架并将数据表示为连续流,但绝对不是你的情况。因此,如果你不想搞乱奇怪的错误,我强烈建议你实现这些东西。

现在关于你的代码。看起来GET_data等待发送寄存器为空并向其写入一个字节。我没有看到任何错误。但你的接收器看起来很可疑。等待RCIF标志置位,然后从输入寄存器RCREG读取5次。但非零RCIF意味着你收到一个字节。你不能等待RCIF一次,然后多次从RCREG读。你应该在每次从RCIF读取之前等待RCREG

我很乐意为您提供工作代码的示例,但我无法提出适合您当前架构的简单解决方案。我可以举例说明如何以正确的方式做到这一点,但它看起来会完全不同。


1
投票

有些事情:

  • UART接收ISR应尽可能短,只需将数据写入缓冲区并绝对包含任何延迟例程。
  • 每个接收到的字节使用一个中断。
  • 用startbyte开始你的框架。例如'S'
  • 用校验和字节结束你的帧。
  • 在缓冲区中检测到完整的帧后,在mainloop中执行portwrite(startbyte + databytes + correct chechsum)
© www.soinside.com 2019 - 2024. All rights reserved.