c - pic24f 尾随 UART 帧错误

标签 c embedded uart microchip pic24

从 pic24f 发送每条消息后,我都会收到帧错误。消息多长或我发送多少消息并不重要。在我的情况下,发送最后一个字符(“\0”或 0x00)后,我收到一个帧错误,但我不知道为什么。我认为这是一个波特率问题,但是无论消息长度如何,这都应该会导致在同一位置或附近出现位采样错误,从而导致该点出现帧错误。不是这种情况。该错误发生在发送最后一个字符之后。 enter image description here

初始化代码如下:

_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逻辑捕获:

最佳答案

寻找here ,我看到这个:

The UART transmission is enabled by setting the UTXEN enable bit (UxSTA<10>). The actual transmission will not occur until the UxTXREG has been loaded with data and the baud rate generator has produced a shift clock, in accordance with the value in the register UxBRG. The transmission can also be started by first loading the UxTXREG register and the setting the UTXEN enable bit.

Clearing the UTXEN bit during a transmission will cause the transmission to be aborted and will reset the transmitter. As a result, the UxTX pin will revert to a high-impedance state.

[加粗我的。]

我对 PIC 不熟悉,但在我看来,您不应该启用和禁用发射器。 TX 引脚变为高阻态将导调用平转换器的输入变为 float ,这可能是帧错误的根源。

我建议在初始化期间启用发射器并保持启用状态。只要您不向 UART 提供新的传输字符,它就应该保持空闲状态。

如果要使用中断,当发送器空闲时,屏蔽中断而不是禁用发送器。

关于c - pic24f 尾随 UART 帧错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651701/

相关文章:

web-services - 物联网设备如何连接到服务器

arrays - 为什么在 char 数组的字符之间插入 "\000"个字符?

冲突的返回类型

c - 项目 : Body Scanner, ACM 1993

C语言使用余数时返回错误答案

使用位移位计算 C 中有符号的长最大值

c++ - 如何找到分配动态内存的 C++ 语言结构?

c - 从函数中为外部变量赋值

android - 蓝牙低功耗通知间隔

c - 为什么我输入两次 't' 时得到的是垃圾值?