#define FCY 16000000
#define BAUDRATE 115200
#define BRGVAL ((FCY/BAUDRATE)/16)-1
void Uart1_Init()
{
U1BRG = BRGVAL; //Set Baudrate
U1STA = 0;
U1MODE = 0;
U1MODEBITS.BRGH = 1;
U1MODEBITS.STSEL = 0; // 1-stop bit
U1MODEBITS.PDSEL = 0; // No Parity, 8-data bits
U1MODEBITS.ABAUD = 0; // Autobaud Disabled
U1STABITS.UTXISEL0 = 0; // Interrupt after one Tx character is transmitted
U1STABITS.UTXISEL1 = 0;
U1STABITS.URXISEL = 0; // Interrupt after one RX character is received
U1MODEBITS.UARTEN = 1;//Enable Uart1
U1STABITS.UTXEN = 1; //Enable Transmit
IEC0BITS.U1TXIE = 1; //Enable Transmit Interrupt
IEC0BITS.U1RXIE = 1; //Enable Receive Interrupt
IPC3BITS.U1TXIP = 0x01; //Setup Uart1 TX interrupt for desired priority level
IPC2BITS.U1RXIP = 0x01; //Setup Uart1 RX interrupt for desired priority level
IFS0BITS.U1RXIF = 0;//Clear the Recieve Interrupt Flag
IFS0BITS.U1TXIF = 0;//Clear the Transmit Interrupt Flag
IEC4BITS.U1ERIE = 0; //Uart1 Interrupt error
}
/********************************************************************
* Function: void PutChar(BYTE Char)
*
* Precondition: UART Setup
*
* Input: Char - Character to transmit
*
* Output: None
*
* Side Effects: Puts character into destination pointed to by ptrChar.
*
* Overview: Transmits a character on UART1.
* Waits for an empty spot in TXREG FIFO.
*
* Note: None
********************************************************************/
void PutChar(char txChar)
{
while(U1STAbits.UTXBF); //wait for FIFO space
U1TXREG = txChar; //put character onto UART FIFO to transmit
}//end PutChar(char txChar)
/********************************************************************
* Function: void GetChar(BYTE * ptrChar)
*
* PreCondition: UART Setup
*
* Input: ptrChar - pointer to character received
*
* Output:
*
* Side Effects: Puts character into destination pointed to by ptrChar.
* Clear WDT
*
* Overview: Receives a character from UART1.
*
* Note: None
********************************************************************/
void GetChar(char *ptrChar)
{
//get the data
while(U1STAbits.URXDA);
* ptrChar = UxRXREG; //get data from UART RX FIFO
}//end GetChar(char *ptrChar)
zhangtaibin209_962839323 2010-4-27 11:23
用户1290921 2009-7-18 18:16