1

我正在尝试在 arduino pro trinket 和我的 PIC18f87k22 之间配置 UART 通信。运行我的代码时,出现以下错误:

错误代码

这是函数的原型:

char EUSART1_Read(void)
{

RCSTA1bits.SREN = 1;
 while(!PIR1bits.RC1IF)
 {
    ;
 }


if(1 == RCSTA1bits.OERR)
{
    // EUSART1 error - restart

    RCSTA1bits.CREN = 0; 
    RCSTA1bits.CREN = 1; 
}

return RCREG1;
}

这就是我使用它的方式(到目前为止唯一的用途):

int16_t get_courant()
{
   char courant1;
   char courant2;
   int16_t courant;

   if(EUSART1_is_tx_ready())
           {
               EUSART1_Write(0b00000001);
           }
   if(EUSART1_is_rx_ready())
           {
           courant1= EUSART1_Read(); 
           }

   if(EUSART1_is_tx_ready())
           {
               EUSART1_Write(0b00000010);
           }
   if(EUSART1_is_rx_ready())
           {
           courant2= EUSART1_Read(); 
           }
   if (CheckBit(courant1,8))
           {
           bit_clr(courant1,8);
           courant = (courant1 << 8) | courant2;
           courant = - courant;
           }
   else 
   {
       courant = (courant1 << 8) | courant2;
   }
   return courant;   
   }

我尝试用 unsigned char 或 uint8_t 替换 char 类型,但它没有改变。关于我的代码有什么问题的任何想法?泰

4

1 回答 1

0

放入一个文件:

// Prototype
char EUSART1_Read(void);
int16_t get_courant(void);

// function definition
char EUSART1_Read(void)
{
    RCSTA1bits.SREN = 1;
    while(!PIR1bits.RC1IF)
    {
        ;
    }
    if (1 == RCSTA1bits.OERR)
    {
        // EUSART1 error - restart
        RCSTA1bits.CREN = 0; 
        RCSTA1bits.CREN = 1; 
    }
    return RCREG1;
}


int16_t get_courant(void)
{
    char courant1;
    char courant2;
    int16_t courant;

    if(EUSART1_is_tx_ready())
    {
        EUSART1_Write(0b00000001);
    }
    if(EUSART1_is_rx_ready())
    {
        courant1= EUSART1_Read(); 
    }

    if(EUSART1_is_tx_ready())
    {
        EUSART1_Write(0b00000010);
    }
    if(EUSART1_is_rx_ready())
    {
        courant2= EUSART1_Read(); 
    }
    if (CheckBit(courant1,8))
    {
        bit_clr(courant1,8);
        courant = (courant1 << 8) | courant2;
        courant = - courant;
     }
     else 
     {
         courant = (courant1 << 8) | courant2;
     }
     return courant;   
}
于 2018-07-05T09:45:00.587 回答