********學(xué)習(xí)型遙控器程序*********/ #include<reg52.h> #include<intrins.h> //延時函數(shù) #include<rradwrite.h> #define uchar unsigned char #define uint unsigned int sbit remoteout=P3^5; sbit turkey=P3^1; sbit IR=P3^2; //將IR位定義為P3.2引腳 uchar a[4]; //儲存引導(dǎo)碼高低電平寬度、用戶碼、用戶反碼與鍵數(shù)據(jù)碼、鍵數(shù)據(jù)反碼 uint LowTime,HighTime; //儲存高、低電平的寬度 uchar addr=0; void SendIRdata(); void delay(); //void savedata(); bit DeCodestudy(void); //void readdata(); static bit OP; //紅外發(fā)射管的亮滅 static uint count;//延時計數(shù)器 static uint endcount; //終止延時計數(shù) static uchar flag;//紅外發(fā)送標(biāo)志 /************************************************************ 函數(shù)功能:主函數(shù) *************************************************************/ void main() { EA=1; //開啟總中斷 EX0=1; //開外中斷1 ET0=1; ET1=1; //定時器T0/T1中斷允許 IT0=1; //外中斷的下降沿觸發(fā) TMOD=0x11; //使用定時器T0/T1的模式1 TR0=0; //定時器T0關(guān)閉 TR1=0; TH1=0xFF; TL1=0xE6; count=0; flag=0; OP = 0; remoteout=0; while(1) { if(turkey==0) { delay(); EX0=0; TR1=1; SendIRdata(); TR1=0; } } } /********************************************************* 函數(shù)功能:存儲/讀取學(xué)習(xí)到的編碼 *********************************************************/ /*void savedata() { uint n; for(n=2;n<6;n++) { Write24c02(a[n],addr); addr++; } } void readdata() { uint n; for(n=2;n<6;n++) { a[n]=Read24c02(addr); addr++; } } /* /************************************************************ 函數(shù)功能: 紅外發(fā)射模塊 ************************************************************/ //定時器1中斷處理 void timeint(void) interrupt 3 { TH1=0xFF;TL1=0xE6; //設(shè)定時值為38K 也就是每隔26us中斷一次 count++; if (flag==1) { OP=~OP; } else { OP = 0; } remoteout = OP; } void SendIRdata() { int i; char irdata; //發(fā)送9ms的起始碼 endcount=223; flag=1; count=0; do{}while(count<endcount); //發(fā)送4.5ms的結(jié)果碼 endcount=117; flag=0; count=0; do{}while(count<endcount); //發(fā)送十六位地址的前八位 irdata=a[0]; for(i=0;i<8;i++) { //先發(fā)送0.56ms的38KHZ紅外波(即編碼中0.56ms的低電平) endcount=10; flag=1; count=0; do{}while(count<endcount); //停止發(fā)送紅外信號(即編碼中的高電平) if(irdata-(irdata/2)*2) //判斷二進制數(shù)個位為1還是0 { endcount=41; //1為寬的高電平 } else { endcount=15; //0為窄的高電平 } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } //發(fā)送十六位地址的后八位 irdata=a[1]; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } //發(fā)送八位數(shù)據(jù) irdata=a[2]; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } //發(fā)送八位數(shù)據(jù)的反碼 irdata=a[3]; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } endcount=10; flag=1; count=0; do{}while(count<endcount); flag=0; } void delay() { int i,j; for(i=0;i<400;i++) { for(j=0;j<100;j++) { } } } /************************************************************ 函數(shù)功能:對4個字節(jié)的用戶碼和鍵數(shù)據(jù)碼進行解碼 說明:解碼正確,返回1,否則返回0 出口參數(shù):dat *************************************************************/ bit DeCodestudy(void) { uchar i,j; uchar temp; //儲存解碼出的數(shù)據(jù) EX0=0; //關(guān)閉外中斷0,不再接收二次紅外信號的中斷,只解碼當(dāng)前紅外信號 while(IR==1); TH0=0; //定時器T0的高8位清0 TL0=0; //定時器T0的低8位清0 TR0=1; //開啟定時器T0 while(IR==0); //如果是低電平就等待,給引導(dǎo)碼低電平計時 TR0=0; //關(guān)閉定時器T0 LowTime=TH0*256+TL0; //保存低電平時間 TH0=0; //定時器T0的高8位清0 TL0=0; //定時器T0的低8位清0 TR0=1; //開啟定時器T0 while(IR==1); //如果是高電平就等待,給引導(dǎo)碼高電平計時 TR0=0; //關(guān)閉定時器T0 HighTime=TH0*256+TL0; //保存引導(dǎo)碼的高電平長度 TH0=0; TL0=0; if((LowTime>8500)&&(LowTime<9500)&&(HighTime>4020)&&(HighTime<5000)) { //如果是引導(dǎo)碼,就開始解碼,否則放棄,引導(dǎo)碼的低電平計時 //次數(shù)=9000us/0.9995=9004, 判斷區(qū)間:9004-500=8504,9004+500=9504. for(i=0;i<4;i++) //連續(xù)讀取4個用戶碼和鍵數(shù)據(jù)碼 { for(j=0;j<8;j++) //每個碼有8位數(shù)字 { temp=temp>>1; //temp中的各數(shù)據(jù)位右移一位,因為先讀出的是高位數(shù)據(jù) TH0=0; //定時器清0 TL0=0; //定時器清0 TR0=1; //開啟定時器T0 while(IR==0) ; //如果是低電平就等待 //低電平計時 TR0=0; //關(guān)閉定時器T0 LowTime=TH0*256+TL0; //保存低電平寬度 TH0=0; //定時器清0 TL0=0; //定時器清0 TR0=1; //開啟定時器T0 while(IR==1); //如果是高電平就等待 TR0=0; //關(guān)閉定時器T0 HighTime=TH0*256+TL0; //保存高電平寬度 if((LowTime<400)||(LowTime>700)) return 0; //如果低電平長度不在合理范圍,則認為出錯,停止解碼 if((HighTime>460)&&(HighTime<660)) //如果高電平時間在560微秒左右,即計數(shù)560/0.9995=560次 temp=temp&0x7f; //(560-100=460, 560+100=660),則該位是0 if((HighTime>1400)&&(HighTime<1900)) //如果高電平時間在1680微秒左右,即計數(shù)1680/0.9995=1700次 temp=temp|0x80; //(1700-250=1450,1700+250=1950),則該位是1 } a=temp; //將解碼出的字節(jié)值儲存在a } } if(a[2]==~a[3]) //驗證鍵數(shù)據(jù)碼和其反碼是否相等,一般情況下不必驗證用戶碼 return 1; //解碼正確,返回1 } /************************************************************ 函數(shù)功能:紅外線觸發(fā)的外中斷學(xué)習(xí)函數(shù) *************************************************************/ void Int0(void) interrupt 0 { DeCodestudy(); if(DeCodestudy()==1) { P1=a[3];//測試代碼接收正確與否 }; EX0=1; //開啟外中斷EX1 }
上面那個程序是我最近弄的有關(guān)紅外發(fā)射和接收的程序,但是不知道為什么,接收解碼后的數(shù)值存進a數(shù)組里,然后再由發(fā)射模塊那邊調(diào)用就不行了,那位大神能夠幫我檢查一下啊。
|