#include "main.h"
#define UART0_INT 6
#define UARTBAUD 9600
uint32 Rcv_New = 0; /* 为1时表明接收到新数据 */
uint32 Snd_N = 0; /* 记录接收数据完毕后, */
/* 需发送数据的个数 */
uint8 Rcv_Buf[8];
uint8 *strpt; /* 字符接收数组 */
void Send(uint8 *dat)
{
uint32 i;
for (i = 0; i < 8; i++)
{
*strpt = dat[i];
strpt++;
}
}
void __irq IRQ_UART0(void)
{
uint32 i = 0;
uint32 IIR = 0;
/*
* UART0接收中断处理
*/
while (((IIR = (uint32)U0IIR) & 0x01) == 0) { /* 若尚有中断待处理 */
switch (IIR & 0x0e){
case 0x04: /* RDA中断处理入口 */
case 0x0c: /* CTI中断处理入口 */
Rcv_New = 1;
for (i = 0; i < 8; i++) {
if ((U0LSR & 0x01) == 0x01){
Rcv_Buf[i] = (uint8)U0RBR;
}
else{
break;
}
}
Snd_N = i;
break;
case 0x02:
for (i = 0; i < 8; i++)
{
if (Snd_N <= 0)
{
break;
}
U0THR = *strpt;
strpt++;
Snd_N--;
}
default:
break;
}
}
VICVectAddr = 0x00;
}
void ClrRcvFIFO(void)
{
volatile uint32 i, temp = 0;
for (i = 0; i < 8; i++) {
if ((U0LSR & 0x01) == 0x01){
temp = (uint32)U0RBR; /* 读空接收FIFO */
}
else{
break;
}
}
}
void UART0_Init(void)
{
uint32 Fdiv = 0;
PCONP |= 1 << 3; /* 使能UART0 */
PINSEL0 = (0x01 << 4) | (0x01 << 6); /* 设置P0.2和P0.3连接到UART0 */
U0LCR = 0x83;
Fdiv = (Fpclk / 16) / UARTBAUD; /* 设置波特率 */
U0DLM = (unsigned long)Fdiv / 256;
U0DLL = (unsigned long)Fdiv % 256;
U0LCR = 0x03;
/*
* 设置FIFO
*/
U0FCR = 0x81; /* 使能FIFO,并设置FIFO触发深度 */
/* 大小为8个字节 */
ClrRcvFIFO(); /* 清除接收FIFO内的数据 */
/*
* 初始化UART VIC 中断接口
*/
VICIntEnClr |= 1 << UART0_INT;
VICVectPri6 = 0x05;
VICVectAddr6 = (uint32)IRQ_UART0;
VICIntEnable |= 1 << UART0_INT;
IRQEnable();
}
int main(void)
{
TargetInit();
UART0_Init();
U0IER |= 0x03;
while (1){
if (Rcv_New == 1){
Rcv_New = 0;
Send(Rcv_Buf);
U0THR = *(strpt++);
}
}
return (0);
}
|