PIC32MX270F256B UART:如何从RX FIFO获取单个字节?

时间:2017-12-27 23:59:39

标签: pic uart fifo pic32

我试图从RX FIFO读取每字节的字节,即8字节深。 问题是当收到第一个字节时,我必须等待8个其他字节才能最终获得U1RXREG上的第一个字节。 我想知道如何在FIFO上执行一些虚拟读取以访问特定字节和" flush"它。简单地将U1RXREG放在一个数组中就不行了。

这是我的初始化:

void UART_Initialize(void)
{
    // SIDL: Stop in Idle Mode bit : 1 = Discontinue module operation when the device enters Idle mode
    U1MODEbits.SIDL = 0;
    // IREN: IrDA Encoder and Decoder Enable bit : 0 = IrDA is disabled
    U1MODEbits.IREN = 0;
    // RTSMD: Mode Selection for UxRTS Pin bit : 0 = UxRTS pin is in Flow Control mode
    U1MODEbits.RTSMD = 0;
    // UEN<1:0>: UARTx Enable bits : 10 = UxTX, UxRX, UxCTS and UxRTS pins are enabled and used
    U1MODEbits.UEN1 = 1;
    U1MODEbits.UEN0 = 0;
    // WAKE: Enable Wake-up on Start bit Detect During Sleep Mode bit : 0 = Wake-up disabled
    U1MODEbits.WAKE = 0;
    // LPBACK: UARTx Loopback Mode Select bit : 0 = Loopback mode is disabled
    U1MODEbits.LPBACK = 0;
    // ABAUD: Auto-Baud Enable bit : 0 = Baud rate measurement disabled or completed
    U1MODEbits.ABAUD = 0;
    // RXINV: Receive Polarity Inversion bit : 0 = UxRX Idle state is ‘1’
    U1MODEbits.RXINV = 0;
    // BRGH: High Baud Rate Enable bit : 0 = Standard Speed mode – 16x baud clock enabled
    U1MODEbits.BRGH = 0;
    // PDSEL<1:0>: Parity and Data Selection bits : 01 = 8-bit data, even parity
    U1MODEbits.PDSEL1 = 0;
    U1MODEbits.PDSEL0 = 1;
    // STSEL: Stop Selection bit : 0 = 1 Stop bit
    U1MODEbits.STSEL = 0;

    // ADM_EN: Automatic Address Detect Mode Enable bit : 0 = Automatic Address Detect mode is disabled
    U1STAbits.ADM_EN = 0;
    // UTXISEL<1:0>: TX Interrupt Mode Selection bits : 00 = Interrupt is generated and asserted while the transmit buffer contains at least one empty space
    U1STAbits.UTXISEL1 = 0;
    U1STAbits.UTXISEL0 = 0;
    // UTXINV: Transmit Polarity Inversion bit : 0 = UxTX Idle state is ‘1’ (with IrDA disbled)
    U1STAbits.UTXINV = 0;
    // URXEN: Receiver Enable bit : 1 = UARTx receiver is enabled. UxRX pin is controlled by UARTx (if ON = 1)
    U1STAbits.URXEN = 1;
    // UTXBRK: Transmit Break bit : 0 = Break transmission is disabled or completed
    U1STAbits.UTXBRK = 0;
    // URXISEL<1:0>: Receive Interrupt Mode Selection bit : 00 = Interrupt flag bit is asserted while receive buffer is not empty (i.e., has at least 1 data character)
    U1STAbits.URXISEL1 = 0;
    U1STAbits.URXISEL0 = 0;
    // ADDEN: Address Character Detect bit (bit 8 of received data = 1) : 0 = Address Detect mode is disabled
    U1STAbits.ADDEN = 0;

    // Baud Rate Calculation :
    // FPB = 10MHz ; Desired Baud Rate = 9600 bauds
    // => U1BRG = FPB/(16*BaudRate)-1 = 64 (error = 0,16%)
    U1BRG = 64;

    // Enable UART RX interrupts
    //IEC1bits.U1RXIE = 1;

    // Enable UART 
    // ON: UARTx Enable bit : 1 = UARTx is enabled. UARTx pins are controlled by UARTx as defined by the UEN<1:0> and UTXEN control bits.
    U1MODEbits.ON = 1;
    // UTXEN: Transmit Enable bit : 1 = UARTx transmitter is enabled. UxTX pin is controlled by UARTx (if ON = 1).
    U1STAbits.UTXEN = 1;

}

目前我尝试在while(1)循环中尝试阅读,但没有成功:

    while (1)
    {
        uint8_t rxbyte[8];
        bool b;
        //if (U1STAbits.URXDA == 1)
        while(!U1STAbits.URXDA);
        rxbyte[0] = U1RXREG;
        rxbyte[1] = U1RXREG;
        rxbyte[2] = U1RXREG;
        rxbyte[3] = U1RXREG;
        rxbyte[4] = U1RXREG;
        rxbyte[5] = U1RXREG;
        rxbyte[6] = U1RXREG;
        rxbyte[7] = U1RXREG;
        sprintf(s, "I received : %u %u %u %u %u %u %u %u\n\r", rxbyte[0], rxbyte[1], rxbyte[2], rxbyte[3], rxbyte[4], rxbyte[5], rxbyte[6], rxbyte[7]);
        myPrint(s);
        IFS1bits.U1RXIF = 0;
    }

rxbytes []中的8个字节始终相同。我试图清除读取之间的RX中断标志,读取之间也读取URXDA,添加延迟,仍然没有成功,我还是要等待8个传入的字节才能访问第一个。

Thanx提前帮助你!

最好的问候。

埃里克

2 个答案:

答案 0 :(得分:0)

我终于找到了解决方案:我有一个双重配置,一个是MCC,另一个是我自己,似乎中断激活导致这个。现在我关闭了中断,我只是轮询URXDA,一切正常。 很抱歉打扰你们!

答案 1 :(得分:0)

您也可以使用原始方法。您的While(1)循环的问题是您检查“硬件Rx缓冲区中是否存在”至少1个字节“(U1STAbits.URXDA会在1个字节可用时告诉您)。当1个字节可用时,你在循环中读取8个字节...当然,当缓冲区中只有1个字节时,你将获得8个相同的字节。