【问题标题】:How to send multiple data of ports through UART?如何通过UART发送多个端口的数据?
【发布时间】:2019-04-10 15:12:35
【问题描述】:

我已经更改了接收器的代码,但发送器几乎相同,所以在接收器中我取消了中断,而是在循环中读取数据,每次都检查状态RC2IF_bit 并且检查是按照发送器发送数据的相同顺序进行的,以确保哪个端口正在发送数据。 问题出在 proteus 上的模拟中,当我收到数据并将其输出到 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;
}

【问题讨论】:

  • 请在您的问题中添加更多详细信息,会发生什么以及您希望发生什么。 “在其他 PIC 上收到的输出似乎闪烁并且不稳定”我不清楚。只是在不知道具体控制器的情况下的一些想法:interrupt 的行为可能取决于时间。 if (RCIF_bit ==1) 是什么意思?确切/接收缓冲区中至少有一个字节可用?假设RCREG 读取最后收到的字节。有先进先出吗?如果还没有新数据怎么办?也许在 interrupt 中,您应该只读取 1 个字节并使用静态计数器来决定哪个 PORTA..F 发送这个字节。
  • 整个想法是我想从一些开关读取输入,每个端口上有五个开关,然后通过 UART 将此数据发送到另一个 PIC,在其中打开相应的位对应的端口。当我为任意三个端口传输数据时,程序运行良好,但是当我添加另一个端口时,接收器上的 LED 一直闪烁,并且正在传输的数据没有被正确接收。
  • 永远永远永远不要延迟接收中断。中断程序应尽可能短。
  • @aymanmagdy 您应该edit 您的问题以添加更多信息,而不是在 cmets 中回答。我的问题也旨在暗示您应该思考什么。正如其他人已经在答案中写的那样,您的协议需要同步。您的接收器实现可能会多次读取相同的数据,这将导致数据被写入错误的端口。

标签: c microcontroller pic uart


【解决方案1】:

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

现在关于您的代码。看起来GET_data 等待发送寄存器为空并向其写入一个字节。我看不出有什么问题。但是您的接收器看起来很可疑。您等待设置RCIF 标志,然后从输入寄存器RCREG 读取5 次。但非零 RCIF 表示您收到 一个 字节。您不能等待RCIF 一次,然后多次阅读RCREG。在RCREG读取之前,您应该等待RCIF

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

【讨论】:

  • 我将不胜感激任何可以指导我的例子。
  • @aymanmagdy 您使用操作系统还是想要裸机解决方案?除了读写端口,你的程序还需要做其他事情吗?
  • 不,我只是想读取一个端口的值,发送它在接收器上读取它,等等其他端口。
【解决方案2】:

一些事情:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    相关资源
    最近更新 更多