serialcom.c

Serial port funtions to allow non-blocking read and write (hardware UART). Useful for debugging without changing all the timing, and for general use of serial port. (by Jonathan Bisson).



#include "SerialCom.h"

void serialInit()
{
    txByteCount = 0;
    txIn = 0;
    txOut = 0;

    rxByteCount = 0;
    rxIn = 0;
    rxOut = 0;

    txsta = 0x24;   //dc, 8 bits, transmit enabled, Async, 0, HighSpeed, trmt, tx9d
    spbrg = BAUD;
    rcsta = 0x90;   //serial port enable, receive enable    
    set_bit( pie1, RCIE );    //Enable RX interrupt
}


void interruptRx()
{
    if ( pir1&0x20 && pie1&0x20 )
    {
        if ( rcsta&0x02 )
        {
            //Clear overrun error
            rcsta &= 0xEF;
            rcsta |= 0x10;
        }
        else if ( rcsta&0x04 )
        {
            //Clear framing error
            rxBuffer[rxIn] = rcreg;
        }
        else
        {
            rxBuffer[rxIn++] = rcreg;
            if (rxIn >= RX_BUFFER_SIZE)
            {
                    rxIn = 0;
            }
            if (rxByteCount < RX_BUFFER_SIZE)
            {
                rxByteCount++;
            }
            else
            {
                //keep only the last {RX_BUFFER_SIZE} bytes             
                rxOut++;
                if (rxOut >= RX_BUFFER_SIZE)
                {
                    rxOut = 0;
                }
            }
        }
    }
 }

void interruptTx()
{
    if ( pie1&0x10 && pir1&0x10 )
    {

        txreg = txBuffer[txOut++];
        if (txOut >= TX_BUFFER_SIZE)
        {
            txOut = 0;
        }
        if (txByteCount > 1)
        {
            txByteCount--;
        }
        else
        {
            txByteCount = 0;
            clear_bit(pie1,TXIE);       //Disable interrupt
        }
    }
}

char getRxByteCount()
{
    return rxByteCount;
}

char getChar(void)
{
    char temp = 0;
    if (rxByteCount > 0)
    {
        temp = rxBuffer[rxOut];
        clear_bit(pie1, RCIE);          //Disable Rx interrupt
        rxOut++;
        if (rxOut >= RX_BUFFER_SIZE)
        {
            rxOut = 0;
        }
        rxByteCount--;
        set_bit(pie1, RCIE);            //Enable Rx interrupt
    }
    return temp;
}


char getTxByteCount()
{
    return txByteCount;
}

void putChar(char character)
{
    txBuffer[txIn] = character;
    clear_bit(pie1,TXIE);     //Disable Tx interrupt 
    txIn++;
    if (txIn >= TX_BUFFER_SIZE)
    {
        txIn = 0;
    }
    if (txByteCount < TX_BUFFER_SIZE)
    {
        txByteCount++;
    }
    else
    {
        //keep only the last {TX_BUFFER_SIZE} bytes
        txOut++;
        if (txOut >= TX_BUFFER_SIZE)
        {
            txOut = 0;
        }
    }
    set_bit(pie1,TXIE);     //Enable Tx interrupt    
}

void sendString(const char * string)
{
    char i = 0;
    while(string[i] != 0x00)
    {
        putChar(string[i++]);
    }
}

void sendHex(char number)
{
    char i;
    char temp = number>>4;

    putChar('0');
    putChar('x');

    for(i=0; i < 2; i++)
    {
        if (temp >= 0x0a)
        {
            putChar('A' - 10 + temp);
        }
        else
        {
            putChar('0' + temp);
        }
        temp = number&0x0f;
   }
}



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies