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

时间:2019-04-10 15:12:36

标签: c microcontroller pic uart

我更改了接收器的代码,但发送器几乎相同,因此在接收器中,我取消了中断,而是循环读取数据,每次都检查状态RC2IF_bit,并按照发送方发送数据的相同顺序进行检查,以确保哪个端口正在发送数据。 问题是在变形蛋白上进行仿真时,当我接收数据并将其输出到led上时,它们可以正常工作,并且数据可以正确发送,但是在仿真过程中,当我看着它时,发现它们会改变状态,例如我发送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;
}

2 个答案:

答案 0 :(得分:2)

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

现在介绍您的代码。看来GET_data等待发送寄存器为空,然后将其写入一个字节。我没有发现任何问题。但是您的接收器看起来可疑。您等待设置RCIF标志,然后从输入寄存器RCREG中读取5次。但是非零的RCIF表示您收到了一个字节。您不能等待RCIF一次然后多次从RCREG中读取。您应该先等待RCIF,然后{strong>每次从RCREG中读取内容。

我很乐意为您提供工作代码示例,但我无法提出一个适合您当前体系结构的简单解决方案。 我可以举个例子,说明如何正确执行此操作,但看起来会完全不同。

答案 1 :(得分:1)

一些事情:

  • UART的接收ISR应该尽可能短,并且只将数据写入缓冲区,并且绝对不包含任何延迟例程。
  • 每个接收到的字节使用一次中断。
  • 以起始字节开始帧。例如'S'
  • 以校验和字节结束帧。
  • 在缓冲区中检测到完整帧(起始字节+数据字节+正确的校验和)后,在主循环中执行portwrite