我正在尝试配置 arduino pro trinket 和我的 PIC18f87k22 之间的 UART 通信。 运行我的代码时出现以下错误:
这是函数的原型(prototype):
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 类型,但没有任何改变。 关于我的代码有什么问题的任何想法? 泰
最佳答案
放入一个文件:
// 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;
}
关于c - 在 PIC18f87k22 上使用 EUSART 模块时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51138748/