/*程序的功能是在T1定時器工作方式2下使用串口工作方式1實現(xiàn)串口通訊,即PC機發(fā)送一串數(shù)據(jù),51單片機將數(shù)據(jù)反饋回來。使用回車符作為一串字符串的結束標志總結:我是參照郭天祥51單片機寫的,這個程序?qū)懙眠€是有點疑惑,就是下面在接受完數(shù)據(jù)后一定要先將串口中斷關閉(即ES = 0),然后再進行處理,如果不關閉中斷的話,反饋回PC機的數(shù)據(jù)就是I getn*/#include//#define DEBUG#define u8 unsigned char#define u16 unsigned int#define MAXSIZE 64u8 code table[] = "I get ";u8 flag,index,i,Len;u8 Buff[MAXSIZE];void Configuration(){TMOD = 0x20;TH1 = 0xF3; //設置波特率為4800TL1 = 0xF3;TR1 = 1;SCON = SCON & (0x3<<6); SCON = SCON | (0x01<<6) | (1<<4); //設置串口工作模式和使能接受PCON = PCON | (1<<7); //設置波特率加倍,即最終的波特率為9600EA = 1; //開啟中斷ES = 1;}void Recieve() interrupt 4{RI = 0;Buff[index++] = SBUF;#ifdef DEBUGSBUF = Buff[index-1];while(!TI);TI = 0;#endifif (Buff[index-1]==n ) //這一串數(shù)據(jù)結束{Len = index;index = 0;flag = 1; }}void main(){Configuration();while(1){if (1==flag) //判斷是否有數(shù)據(jù)到來{ES = 0; //要關閉中斷,否則程序有問題for(i=0;i<6;i++){SBUF = table[i];while(!TI); //等待數(shù)據(jù)發(fā)送完畢TI = 0;}for(i=0;i本文引用地址:http://m.butianyuan.cn/article/201611/318579.htm
評論