从我的 pic24f 发送每条消息后,我都会收到一个帧错误。消息多长时间或我发送多少条消息都没有关系。在我的情况下发送最后一个字符('\0' 或 0x00)后,我收到一个帧错误,我不知道为什么。我认为这是一个波特率问题,但无论消息长度如何,这都会导致在同一位置或附近出现异位采样,从而导致此时出现帧错误。不是这种情况。错误发生在发送最后一个字符之后。
这是初始化代码:
_TRISF5 = 0; //TX pin to output
_TRISF3 = 1; //RX pin to input
iPPSOutput(OUT_PIN_PPS_RP17,OUT_FN_PPS_U1TX); //RP17 tied to TX
iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP16); //RP26 tied to RX
U1MODE = 0; //disable, set defaults
//setup
U1MODEbits.STSEL=0;//one stop bit
U1MODEbits.PDSEL=0;//8 bit no parity
U1MODEbits.BRGH=1;//High speed mode (baud clock from Fcy/4)
U1MODEbits.RXINV=0;//UxRX idle state is '1' (don't invert)
U1MODEbits.ABAUD=0;//baud rate measurement disabled or completed
U1MODEbits.LPBACK=0;//loopback disabled
U1MODEbits.WAKE=0;//No wake up enabled
U1MODEbits.UEN1=0;//TX,RX enabled (others controlled by latch)
U1MODEbits.UEN0=0;//TX,RX enabled (others controlled by latch)
U1MODEbits.RTSMD=0;//RTS pin in flow control mode (don't think this applies for UEN = 0)
U1MODEbits.IREN=0;//irda encoder and decoder disabled
U1MODEbits.USIDL=1;//stop in idle mode
IFS0bits.U1RXIF = 0; //clear rx flag
IFS0bits.U1TXIF = 0; //clear tx flag
IEC0bits.U1RXIE = 1; //enable rx interrupt
IEC0bits.U1TXIE = 1; //enable tx interrrupt
U1BRG = 34; //115200, -0.62% Error
U1STAbits.URXISEL = 0; //Interupt when RX buffer has one or more characters
U1STAbits.UTXISEL1 = 0; //Interrupt when all transmit operations are complete
U1STAbits.UTXISEL0 = 0; //Interrupt when all transmit operations are complete
U1STAbits.UTXEN = 1;
U1MODEbits.UARTEN = 1; //enable
这是传输代码:
void send_msg(const char* msg){
char* p = (char*) msg;
U1STAbits.UTXEN = 1;
while(*p != '\0'){
while(U1STAbits.UTXBF); //wait for buffer room (1: buffer full, 0: buffer has room)
U1TXREG = *p++;
}
while(!U1STAbits.TRMT); //wait for transmission to complete
U1STAbits.UTXEN = 0; //disable the transmitter
}
链接到 salae 逻辑捕获: