/**************************
Initialize the serial port
@ 160000 and 230400bps
***************************
Requires :none
Returns : none
**************************/
void uart_init()
{//Enables Serial port
/*RCSTA RECIEVER CONTROL REGISTER*/
RCSTA1bits.SPEN = 1;
RCSTA1bits.RX9=0;//8 bit reception
RCSTA1bits.CREN = 1; //enable the reciever
TRISCbits.TRISC7=0;
TRISCbits.TRISC6=0;
/*TXSTAx: TRANSMIT STATUS AND CONTROL REGISTER */
TXSTA1bits.BRGH=1;//baud rate high bits
TXSTA1bits.SYNC=0;//assynchronous mode
TXSTA1bits.TX9=0; //8 bit reception
TXSTA1bits.TRMT=1;//reset the buffer
TXSTA1bits.CSRC=0;// Master mode (clock generated internally from BRG)
/*BAUDCONx: BAUD RATE CONTROL REGISTER*/
BAUDCON1bits.BRG16=1;//16 bit Baud rate is enabled
SPBRGH1 = 0;
SPBRG1 = 16;//2;
//(((_XTAL_FREQ/baud)/4)-1);
//FCY/64)/BAUD) - 1;
// set baud to 9600 FCY=20000000
//Turn On the Tx and Rx
TXSTA1bits.TXEN=1;
RCSTA1bits.CREN=1;
}
///////////////////////////
//Function for read to a buffer
///////////////////////////
//Requires : <Xc.h>,Uartinit
//Returns : None
//Parameters: None
///////////////////////////
void uart_read2buff()
{
int count;
while(uart_read()>0)
{
rxr[count]=uart_read();
uart_char(uart_read());
count=count+1;
uart_num(count);
if (count>63)
break;
}
uart_string("out of here");
rxr[count]='\0';
count=0;
}
//////////////////////////
//Function for sending char
///////////////////////////
//Requires : <Xc.h>,Uartinit
//Returns : None
//Parameters: None
///////////////////////////
void uart_char(char data)
{
TXREG1= data;
while(!TXSTA1bits.TRMT);
}
///////////////////////////
//Function for reciveing from serial
///////////////////////////
//Requires : <Xc.h>,Uartinit
//Returns : None
//Parameters: None
///////////////////////////
char uart_read()
{
while(!RC1IF);
RC1IF=0;
return RCREG1;
//RCSTA1bits.CREN = 0;
//RCSTA1bits.CREN = 1;
}