This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Serial port ring buffer proglem??

I use GetChar() and PutChar() functions to read or write to serial port but when write too long streams using PutChar() it stops at "while(txn > 31)" line in PutChar().. I think the interrupt must go on decreasing the txn value, but not. The program stops running. But the timer interrupt is working and blinking some leds. Whats wrong?? Please help me.

static volatile unsigned char idata rxbuf[32];  // Uart recieve buffer
static volatile unsigned char rxrp;             // Uart receive buffer read pointer
static volatile unsigned char rxwp;             // Uart receive buffer write pointer
static volatile unsigned char rxn;              // Number of characters left in receive buffer
static volatile unsigned char idata txbuf[32];  // Uart transmit buffer
static volatile unsigned char txrp;             // Uart transmit buffer read pointer
static volatile unsigned char txwp;             // Uart transmit buffer write pointer
static volatile unsigned char txn;              // Number of characters left to send
.....
void Serial (void) interrupt 4 using 2
{
    if (RI)
    {
        RI = 0;                                 // Clear receive interrupt bit
        rxbuf[rxwp] = SBUF;                     // Store received character in receive buffer
        rxwp = (rxwp + 1) & 0x1f;               // Advance write pointer
        rxn++;                                  // Increment number of characters in buffer
    }
    if (TI)
    {
        TI = 0;                                 // Clear transmit interrupt bit
        txn--;                                  // Decrement number of characters to send
        if (txn > 0)                            // More characters in transmit buffer?
        {
            SBUF = txbuf[txrp];                 // Yes, store next character in uart buffer
            txrp = (txrp + 1) & 0x1f;           // Advance read pointer
        }
    }
}

void PutChar(char c)
{
    while(txn > 31)                             // Wait while transmit buffer is full
        ;
    if (txn == 0)                               // If all previous characters are sent...
    {
        SBUF = c;                               // ...write character directly to the uart...
        txn = 1;
    } else
    {
        txbuf[txwp] = c;                        // ...else write it to the transmit buffer
        txwp = (txwp + 1) & 0x1f;               // Advance write pointer
        ES = 0;                                 // Disable serial interrupt while...
        txn++;                                  // ...incrementinfg numnber of characters to send
        ES = 1;                                 // Enable serial interrupt
    }
}

unsigned char GetChar(void)
{
    unsigned char c;

    while(rxn == 0)                             // Wait until there is at least one character in the buffer
        ;
    c = rxbuf[rxrp];                            // Read next character from buffer
    rxrp = (rxrp + 1) & 0x1f;                   // Advance buffer read pointer
    ES = 0;                                     // Disable serial interrupt while...
    rxn--;                                      // ...decrementing number of characters in buffer
    ES = 1;                                     // Enable serial interrupt
    return c;
}