📄 uart.c
字号:
/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2003
*
* File name : LPC_Uart.c
* Description : Define API for UART
*
* $Revision: 1.1.6.1 $
**************************************************************************/
#include "LPC2294.h"
/* Uart Interrupt Identification */
#define IIR_RSL 0x3
#define IIR_RDA 0x2
#define IIR_CTI 0x6
#define IIR_THRE 0x1
/*************************************************************************
* Function Name: UART_Init
* Parameters: void
* Return: void
*
* Description: Initialize Uart, configure baut rate, frame format and FIFO
*
*************************************************************************/
void UART_Init(void)
{
// Divisor = pclk / (16*bautrate)
/*** UART0 ***/
// set bautrate: 38.4K bps
// U0LCR_bit.DLAB = 1; // DLAB = 1
U0LCR = 0x83;
U0DLM = 0;
U0DLL = 90;
// Set frame format: 8,N,1
U0LCR = 0x3; // DLAB = 0
// Set FIFO
U0FCR = 0x1; // Rx Threshold = 1
// Set Interrupt
U0IER = 0x1; // Enable Rx Int
// Enable TxD0 and RxD0, bit 0~3=0101
PINSEL0 |= 0x05; // P0.0 and P0.1
/*** UART1 ***/
// bautrate: 19.2K bps
// U1LCR_bit.DLAB = 1; // DLAB = 1
U1LCR = 0x83;
U1DLM = 0;
U1DLL = 180;
// Set frame format: 8,N,1
U1LCR = 0x3; // DLAB = 0
// Set FIFO
U1FCR = 0x1; // Rx Threshold = 1
// Set Interrupt
U1IER = 0x1; // Enable Rx Int
// Enable TxD1 and RxD1, bit 16~19=0101
PINSEL0 |= 0x50000; // P0.8 and P0.9
}
/*************************************************************************
* Function Name: UART0_ISR
* Parameters: void
* Return: void
*
* Description: Uart0 interrupt subroutine
*
*************************************************************************/
void __irq UART0_ISR(void)
{
switch((U0IIR>>1)&0x7)
{
case IIR_THRE: // continue sending data
break;
case IIR_RSL: // error manage
break;
case IIR_RDA: // receive data
case IIR_CTI: // time out
// read U0RBR;
U0THR = U0RBR; // send received data
break;
default:
break;
}
VICVectAddr = 0; // Clear interrupt in VIC.
}
/*************************************************************************
* Function Name: UART1_ISR
* Parameters: void
* Return: void
*
* Description: Uart1 interrupt subroutine
*
*************************************************************************/
void __irq UART1_ISR(void)
{
switch((U1IIR>>1)&0x7)
{
case IIR_THRE: // continue sending data
break;
case IIR_RSL: // error manage
break;
case IIR_RDA: // receive data
case IIR_CTI: // time out
// read U1RBR;
break;
default:
break;
}
VICVectAddr = 0; // Clear interrupt in VIC.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -