#include <stdio.h>
#include "LPC213X.h" /* LPC21xx definitions */
/*----------------------------------------------------------*\
| TYPE DEFINE |
\*----------------------------------------------------------*/
typedef char S8;
typedef unsigned char U8;
typedef short S16;
typedef unsigned short U16;
typedef int S32;
typedef unsigned int U32;
typedef long long S64;
typedef unsigned long long U64;
typedef unsigned char BIT;
typedef unsigned int BOOL;
#define Fosc (12000000) //晶振频率,10MHz~25MHz,应当与实际一至
#define Fcclk (Fosc * 5) //60M 系统频率,必须为Fosc的整数倍(1~32),且<=70MHZ
#define Fcco (Fcclk * 4) //CCO频率,必须为Fcclk的2、4、8、16倍,范围为156MHz~320MHz
#define Fpclk (Fcclk / 4) * 1 //016.5888,VPB时钟频率,只能为(Fcclk / 4)的1 ~ 4倍
U8 recver_buffer[14];
U8 rcv_new;
U32 GetNum;
/*----------------------------------------------------------*\
| Delay ns |
\*----------------------------------------------------------*/
void DelayNS (U32 dly) {
U32 i;
for ( ; dly>0; dly--)
for (i=0; i<500; i++);
}
/*----------------------------------------------------------*\
| UART0 Initial |
\*----------------------------------------------------------*/
void UART0_init(void) {
// unsigned short int fdiv;
PINSEL0 |= 0x00000005; // 设置I/O连接到UART0
U0LCR = 0x83; // DLAB = 1,可设置波特率
U0DLM = 0x00;
U0DLL = 0x47;
U0FDR = 0x83;
U0LCR = 0x03;
}
/*----------------------------------------------------------*\
| PLL init |
\*----------------------------------------------------------*/
void PLL_Init(void)
{
while (!(PLLSTAT & 0x00000400)) ; // test Lock bit 判断PLOCK 为锁定?1锁定到制定的频lv
PLLCFG = 0x00000024; // Set multiplier and divider of PLL to give ;CCLK=60MHz; P=2,M=5
PLLCON = 0x00000003; // Connect the PLL PLL连接和PLL使能 PLL控制寄存器
PLLFEED = 0x000000AA; //Update PLL registers
PLLFEED = 0x00000055; //PL馈送寄存器
VPBDIV = 0x00000000; //设置VPB总线时钟为处理器时钟(cclk)的1/4 = 15M
}
/*----------------------------------------------------------*\
| UART0 Send Char |
\*----------------------------------------------------------*/
void UART0_PutCh(unsigned char Ch) {
U0THR = Ch; // 发送数据
while( (U0LSR&0x40)==0 ); // 等待数据发送完毕
DelayNS(5);
}
/*----------------------------------------------------------*\
| UART0 Send String |
\*----------------------------------------------------------*/
void UART0_PutStr(unsigned char const *str,unsigned int SendNum) {
unsigned int i;
for(i=0;i<SendNum;i++)
UART0_PutCh(*str++);
}
/*----------------------------------------------------------*\
| UART1 Get Byte |
\*----------------------------------------------------------*/
U8 UART0_GetByte (void) {
U8 rcv_dat;
while ((U0LSR & 0x01) == 0);
rcv_dat = U0RBR;
return (rcv_dat);
}
/*----------------------------------------------------------*\
| UART1 Get String |
\*----------------------------------------------------------*/
void UART0_GetStr(U8 *s) {
while( (*s = UART0_GetByte()) != '\n' ) s++;
}
/***********UART0中断接收程序***********/
void IRQ_UART0(void) __irq
{
GetNum = 0;
while ((U0IIR & 0x01) == 0){ /* 判断是否有中断挂起 */
switch (U0IIR & 0x0E){
case 0x06: //发生RLS中断,读U0LSR中断复位
break;
case 0x04: /* 接收数据可用中断*/
rcv_new = 1; /* 置接收新数据标志 */
for (GetNum = 0; GetNum < 14; GetNum++) /* 连续接收14个字节 */
{
recver_buffer[GetNum] = U0RBR;
}
break;
case 0x0C: /* 字符超时中断 */
rcv_new = 1;
while ((U0LSR & 0x01) == 0x01) /* 判断数据是否接收完毕 */
{
recver_buffer[GetNum] = U0RBR;
GetNum++;
}
break;
case 0x02: //发生THRE中断,读U0IIR或写U0THR中断复位
break;
default:
break;
}
}
VICVectAddr = 0x00; //清除中断
}
void IRQEnable(void)
{
int temp;
__asm
{
MRS temp,CPSR
BIC temp,temp,#0x80
MSR CPSR_c,temp
}
}
void Interrupt_Init (void)
{
VICIntSelect = 0x00000000; // 设置所有通道为IRQ中断 ,置1为FIQ中断,置0为IRQ中断
VICVectCntl0 = (0x20 | 6); // UART0中断通道分配到IRQ slot0 即获得最高中断优先级
VICVectAddr0 = (int)IRQ_UART0; // 设置UART0向量地址
VICIntEnable = 1 << 6; // 使能串口UART0中断
}
/*----------------------------------------------------------*\
| MAIN ENTRY |
\*----------------------------------------------------------*/
int main(void) {
U0FCR = 0xC7; // 使能FIFO,并设置触发点为14
U0IER = 0x07; // 允许RBR中断,即接收中断
rcv_new = 1;
PLL_Init();
UART0_init(); //串口初始化
Interrupt_Init(); //中断向量初始化
while(1)
{
if(rcv_new == 1)
{
rcv_new = 0;
UART0_PutStr(recver_buffer,3);
}
}
}
/*----------------------------------------------------------*\
| END OF FILE |
\*----------------------------------------------------------*/
|