下面是利用中斷的串口通信程序
#include"2440addr.h"
void __irq UART0RX_isr()
{
char buf,i;
rINTMSK=0xffffffff;
ClearPending(BIT_UART0);
if(rUTRSTAT0 & 0X01)//接收是否完畢 =1結(jié)束
{
ClearSubPending(BIT_SUB_RXD0);
buf=rURXH0;//讀取數(shù)據(jù)
while(!(rUTRSTAT0 & 0X04));//是否允許發(fā)送 =1允許
rUTXH0=buf;
if(buf==1)
i=1;
else if(buf==2)
i=2;
else if(buf==3)
i=3;
else if(buf==4)
i=4;
switch(i){
case 1:
rGPBDAT^=(1<<5);
i=0;//將i清零防止下次收到其他數(shù)據(jù)時(shí)干擾
break;
case 2:
rGPBDAT^=(1<<6);
i=0;
break;
case 3:
rGPBDAT^=(1<<8);
i=0;
break;
case 4:
rGPBDAT^=(1<<10);
i=0;
break;
default:break;
}
}
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
}
static void __irq Key_ISR()
{
char key;//用來標(biāo)識是哪一個(gè)按鍵按下
//EnterCritical(&r);
rINTMSK=0xffffffff;
if(rINTPND==BIT_EINT0) {
ClearPending(BIT_EINT0);
key=1;
}
else if(rINTPND==BIT_EINT2) {
ClearPending(BIT_EINT2);
key=2;
}
else if(rINTPND==BIT_EINT3) {
ClearPending(BIT_EINT3);
key=3;
}
else if(rINTPND==BIT_EINT4_7){
rEINTPEND=(1<<4);
ClearPending(BIT_EINT4_7);
key=4;
}
switch(key){
case 1:
rGPBDAT^=(1<<5);
break;
case 2:
rGPBDAT^=(1<<6);
break;
case 3:
rGPBDAT^=(1<<8);
break;
case 4:
rGPBDAT^=(1<<10);
break;
}
//ExitCritical(&r);
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
}
int TSmain()
{
rULCON0 &=0XFFFFFF00;
rULCON0 |=0X03;//1位起始位,8位數(shù)據(jù)位
rUCON0=0x05;//0X0805;//串口時(shí)鐘PCLK,查詢方式 東:PCLK為50M
rUBRDIV0 =325;//0X1A;//波特率115200****325時(shí)設(shè)置為9600
rGPHUP=0x1ff;//H口上拉禁止
rGPHCON&=0x3c0000;
rGPHCON|=0x2faaa;
rGPBCON = 0x1dd7fc;//GPB5,6,8,10設(shè)置為輸出
rGPBDAT|=0x560;//4個(gè)LED全滅
rGPFCON &=~((3<<0)|(3<<4)|(3<<6)|(3<<8)) ;
rGPFCON |= ((2<<0)|(2<<4)|(2<<6)|(2<<8)) ;//GPF0,GPF2,GPF3,GPF4工作在第二功能狀態(tài),即中斷
rEINTPEND=(1<<4);
ClearPending(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
ClearSubPending(BIT_SUB_RXD0);
ClearPending(BIT_UART0);
pISR_EINT0= pISR_EINT2 =pISR_EINT3 = pISR_EINT4_7=(int)Key_ISR;
EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);
EnableIrq(BIT_UART0);
EnableSubIrq(BIT_SUB_RXD0);
rEINTMASK=~(1<<4);
pISR_UART0=(unsigned) UART0RX_isr;
while(1)
{
}
return 0;
}
評論