#include<P30F5011.h>
#include"UART1.h"
char buf[24];
int j;
void init_UART1() // Initialization function.
{
TRISFbits.TRISF3 = 0; // Configure RF3 as Output for UART1 Tx .
TRISFbits.TRISF2 = 1; // Configure RF2 as Input for UART1 Rx.
/********************* CONFIGURE UART1 Baud Rate Generator Register *********************
* *
* FCY : 8 MHz, *
* Desired Baud Rate : 9600 / 19200 *
* Desired U1BRG = 25 or 0x19h / 0x33 *
* *
****************************************************************************************/
U1BRG = 51; //XT /w PLL 4x
/************************* CONFIGURE UART1MODE Register********************************/
U1MODEbits.USIDL = 0; // Continue operation in Idle mode
U1MODEbits.ALTIO = 0; // UART communicates usingalternate UxTX and UxRX I/O pins
U1MODEbits.WAKE = 1; // Enable Wake up
U1MODEbits.LPBACK = 0; // Disable Loop Back
U1MODEbits.ABAUD = 0; // Disable Auto Baud
U1MODEbits.PDSEL = 0; // 8 Bit Data, No Parity
U1MODEbits.STSEL = 0; // 1 Stop Bit
IPC2bits.U1TXIP = 7; // Tx priority level.
IPC2bits.U1RXIP = 7; // Rx priority level.
U1MODEbits.UARTEN = 1; // Enable UART
IEC0bits.U1TXIE = 1; // Enabling TX interrupt.
IEC0bits.U1RXIE = 1; // Enabling Rx interrupt.
/********************* CONFIGURE UART1 STATUS & CONTROL Register ***********************/
U1STAbits.UTXISEL = 1; // Interrupt when a character is transferred to the Transmit Shift register
U1STAbits.UTXBRK = 0; // Operate U1Tx Normally on Transmit Break Bit
U1STAbits.UTXEN = 1; // Enable U1 Tranmission
U1STAbits.URXISEL = 0; // Interrupt flag bit is set when Receive Buffer is 3/4 full (i.e., has 1 data characters)
// putsUART1((unsigned int *)"UART Ready");
}
void putsUART1(unsigned int *buffer)
{
char * temp_ptr = (char *) buffer; // transmit till NULL character is encountered
if(U1MODEbits.PDSEL == 3) // check if TX is 8bits or 9bits
{
while(*buffer != '\0')
{
while(U1STAbits.UTXBF); // wait if the buffer is full
U1TXREG = *buffer++; // transfer data word to TX reg
}
}
else
{
while(*temp_ptr != '\0')
{
while(U1STAbits.UTXBF); // wait if the buffer is full
U1TXREG = *temp_ptr++; // transfer data byte to TX reg
}
}
}
void __attribute__((__interrupt__,no_auto_psv)) _U1TXInterrupt(void) // This is UART1 transmit ISR
{
IFS0bits.U1TXIF = 0;
}
void __attribute__((__interrupt__,no_auto_psv)) _U1RXInterrupt(void) // This is UART1 receive ISR
{
IFS0bits.U1RXIF = 0;
for(j=0;j<=24;j++)
buf[j]=ReadUART1();
putsUART1((unsigned int*)buf);
}