#include <LPC213x.H>
typedef unsigned int uint32;
typedef unsigned short int uint16;
typedef unsigned char uint8;
#define Fpclk 11059200
void DelayNS(uint32 dly)
{
uint32 i;
for(;dly>0;dly--)
for(i=0;i<50000;i++);
}
void PLL_Init()
{
PLLCON = 1; //使能PLL
//VPBDIV = 0;
//Fpclk=Fcclk/4;
PLLCFG=0x63; //P=2,M=4
PLLFEED = 0xaa;
PLLFEED = 0x55; //更改生效
while((PLLSTAT & (1 << 10)) == 0); //等待PLL锁定
PLLCON = 3;
PLLFEED= 0xaa;
PLLFEED = 0x55;
}
typedef struct UartMode //定义串口模式设置数据结构
{
uint8 datab;
uint8 stopb;
uint8 parity;
}
UARTMODE;
uint8 rcv_buf[8];
uint8 rcv_new;
void __irq IRQ_UART0(void) //接受中断
{
uint8 i;
if(0x04==(U0IIR&0x0F))
rcv_new=1;
for(i=0;i<8;i++)
{
rcv_buf[i]=U0RBR;
}
VICVectAddr=0x00;
}
void SendByte(uint8 data)
{
U0THR=data;
}
void ISendBuf(void)
{
uint8 i;
for(i=0;i<8;i++)
{
SendByte(rcv_buf[i]);
DelayNS(100);
}
while((U0LSR&0x40)==0);
}
uint8 UART0_Ini(uint32 baud,UARTMODE set)
{
uint32 bak; //参数过滤
if((0==baud)||(baud>115200)) return(0);
if((set.datab<5)||(set.datab>8)) return(0);
if((0==set.stopb)||(set.stopb>2)) return(0);
if(set.parity>4) return(0);
U0LCR=0x80; //设置串口波特率
bak=(Fpclk>>4)/baud;
U0DLM=bak>>8;
U0DLL=bak&0xFF;
bak=set.datab-5; //设置串口模式
if(2==set.stopb) bak|=0x04;
if(0!=set.parity){set.parity=set.parity-1;bak|=0x08;}
bak|=set.parity<<4;
U0LCR=bak;
return(1);
}
int main(void)
{
UARTMODE uart0_set;
PINSEL0=0x00000005;
PINSEL0=0x00000000;
rcv_new=0;
uart0_set.datab=8;
uart0_set.stopb=1;
uart0_set.parity=0;
UART0_Ini(115200,uart0_set);
U0FCR=0x81; //设置触发点为8个字节
U0IER=0x01;
VICIntSelect=0x00000000; //设置中断允许
VICVectCntl0=0x26;
VICVectAddr0=(int)IRQ_UART0;
VICIntEnable=0x00000040;
while(1)
{
if(rcv_new==1)
{
ISendBuf();
rcv_new=0;
}
}
return(0);
}
|