/*程序的功能是在T1定時(shí)器工作方式2下使用串口工作方式1實(shí)現(xiàn)串口通訊,即PC機(jī)發(fā)送一串?dāng)?shù)據(jù),51單片機(jī)將數(shù)據(jù)反饋回來(lái)。使用回車(chē)符作為一串字符串的結(jié)束標(biāo)志總結(jié):我是參照郭天祥51單片機(jī)寫(xiě)的,這個(gè)程序?qū)懙眠€是有點(diǎn)疑惑,就是下面在接受完數(shù)據(jù)后一定要先將串口中斷關(guān)閉(即ES = 0),然后再進(jìn)行處理,如果不關(guān)閉中斷的話,反饋回PC機(jī)的數(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; //設(shè)置波特率為4800TL1 = 0xF3;TR1 = 1;SCON = SCON & (0x3<<6); SCON = SCON | (0x01<<6) | (1<<4); //設(shè)置串口工作模式和使能接受PCON = PCON | (1<<7); //設(shè)置波特率加倍,即最終的波特率為9600EA = 1; //開(kāi)啟中斷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 ) //這一串?dāng)?shù)據(jù)結(jié)束{Len = index;index = 0;flag = 1; }}void main(){Configuration();while(1){if (1==flag) //判斷是否有數(shù)據(jù)到來(lái){ES = 0; //要關(guān)閉中斷,否則程序有問(wèn)題for(i=0;i<6;i++){SBUF = table[i];while(!TI); //等待數(shù)據(jù)發(fā)送完畢TI = 0;}for(i=0;i本文引用地址:http://butianyuan.cn/article/201611/318579.htm
評(píng)論