STC12C5A60S2串口中斷接收程序
#define UART0_BUF_LEN 32
int UART1_Recv_count; //接收計(jì)數(shù)
bit UART1_Overflow_Flag; //緩沖區(qū)滿標(biāo)志
idata uchar UART1_Recv_BUF[UART0_BUF_LEN]; //串口接收緩沖區(qū)
//串口初始化
void Init_Uart()
{
UART1_Overflow_Flag=0;
UART1_Recv_count=0;
//22.1184M 晶振 115200波特率
PCON &= 0x7f; //波特率不倍速
SCON = 0x50; //8位數(shù)據(jù),可變波特率
BRT = 0xFA; //設(shè)定獨(dú)立波特率發(fā)生器重裝值
AUXR |= 0x04; //獨(dú)立波特率發(fā)生器時(shí)鐘為Fosc,即1T
AUXR |= 0x01; //串口1選擇獨(dú)立波特率發(fā)生器為波特率發(fā)生器
AUXR |= 0x10; //啟動(dòng)獨(dú)立波特率發(fā)生器
ES = 1; //充許串口1中斷
}
//串口中斷接收程序
/*
說(shuō)明:
(1)如果緩沖區(qū)接收滿了,則中斷接收程序不在接收數(shù)據(jù)。UART1_Recv_count=32
(2)如果接收到“回車符”、“換行符”,則中斷不在接收數(shù)據(jù)。UART1_Recv_count={實(shí)際接收到的數(shù)據(jù)個(gè)數(shù)}
以上2種情況觸發(fā)后 UART1_Overflow_Flag標(biāo)志位置為1,等待MAIN函數(shù)(主程序處理)
*/
void UART1_Int(void) interrupt 4
{
if (RI == 1)
{
RI = 0;
if(UART1_Overflow_Flag==0)
{
if(UART1_Recv_count<UART0_BUF_LEN)
{
UART1_Recv_BUF[UART1_Recv_count++] = SBUF;
if(SBUF=='r' || SBUF=='n')
{
UART1_Overflow_Flag=1;
}
}
else
{
UART1_Overflow_Flag=1; // 關(guān)閉串口中斷,停止接收
}
}
LED2=~LED2;
}
}
int main()
{
char i;
TMOD=0x00;
AUXR=0x00;
LED1=0;
LED2=1;
Init_Uart();
EA=1; //開(kāi)總中斷
printf("System is start...rn");
while(1)
{
if (UART1_Overflow_Flag==1) //串口中斷數(shù)據(jù)接收完畢,開(kāi)始處理
{
for(i=0;i<UART1_Recv_count;i++)
printf("%c",UART1_Recv_BUF[i]);
Appcall(UART1_Recv_BUF,UART1_Recv_count);
printf("rn");
UART1_Recv_count = 0; //緩存清零
UART1_Overflow_Flag=0; //允許串口繼續(xù)接收數(shù)據(jù)
}
}
}