uart.c

This 3 Wire example application demonstrates the use of a DS1302 Real Time Clock (by Don Cramer).




#include <system.h>

#include "uart.h"





void setupUart(char rateScaler)

{

    // --- serial line inits

    set_bit(rcsta,SPEN);                // set serial port enable

    set_bit(rcsta,CREN);                // set continuous receive enable

    spbrg = rateScaler;                 // baud rate

    set_bit(txsta,BRGH);                // set high baud rate speed

    set_bit(txsta,TXEN);                // set transmit enable

    clear_bit(txsta,TX9);                // set transmit enable

    clear_bit(txsta,SYNC);              // set async

}





void sendChar(char value)

{

    while (!(txBufferIsReady()));

    txreg = value;  // Load TXREG

}



char receiveChar()

{

    while (!(rxBufferIsReady()));

    return rcreg;

}



void sendString( const char* text )

{

    char i = 0;

    while ( text[i] != 0 )

        sendChar( text[i++] );

}



short txBufferIsReady()

{

    return((pir1 & 1<<TXIF) != 0);

}



short rxBufferIsReady()

{

    return((pir1 & 1<<RCIF) != 0);

}



void setTxInterrupt(char enable)

{

    if (enable)

        set_bit(pie1,TXIE);

    else

        clear_bit(pie1,TXIE);

}



void setRxInterrupt(char enable)

{

    if (enable)

        set_bit(pie1,RCIE);

    else

        clear_bit(pie1,RCIE);

}



void sendDecNumber(unsigned int n)

{



    if( n >=10000 )

        sendChar(((n/10000)%10)+'0');

    if( n >=1000 )

        sendChar(((n/1000)%10)+'0');

    if( n >=100 )

        sendChar(((n/100)%10)+'0');

    if( n >=10 )

        sendChar(((n/10)%10)+'0');

    sendChar((n%10)+'0');



}



void sendHex(char value)

{

    char hexChar;

    char i;

    for (i=0;i<=1;i++)

    {

        if (i==0)

        {

            hexChar = value >> 4;

        }

        else

        {

            hexChar = value & 0x0F;

        }

        if (hexChar<10)

        {

            hexChar=hexChar+'0';

        }

        else

        {

            hexChar=hexChar+('A'-10);

        }

        sendChar(hexChar);

    }

}



void hexDump()

{

   short i;

   char val;

   for (i=0;i<16;i++)

   {

      sendHex(hexBuffer[i]);

   }

   sendChar(' ');

   for (i=0;i<16;i++)

   {

      val=hexBuffer[i];

      if (val<32) val='.';

      sendChar(val);

   }

}






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

Copyright © 2002-2006 SourceBoost Technologies