我最近在研究用51做一個(gè)學(xué)習(xí)型遙控器,參考了網(wǎng)上的一些程序,只是這個(gè)程序只是用于學(xué)習(xí)單個(gè)按鍵的,而且還不是很穩(wěn)定,我想做一個(gè)能夠?qū)W習(xí)多個(gè)按鍵的遙控器,不過我剛開始學(xué)單片機(jī)不久,對(duì)于網(wǎng)上的那個(gè)程序還不能完全看出究竟有哪些不足,也不知道如何在它的基礎(chǔ)上將遙控代碼存進(jìn)AT24C02等系列的存儲(chǔ)器里面,希望有相同興趣愛好的或者之前有過研究的人能夠指點(diǎn)下,感激不盡,若有較完善的程序代碼的話,那就感激涕零了。
/********學(xué)習(xí)型遙控器程序*********/
#include<reg52.h>
#include<intrins.h> //延時(shí)函數(shù)
#define uchar unsigned char
#define uint unsigned int
sbit studylamp=P1^6; //學(xué)習(xí)指示燈
sbit lamp=P1^7; //發(fā)射指示燈
sbit studykey=P3^3; //學(xué)習(xí)鍵
sbit remotein=P3^2; //遙控信號(hào)輸入口
sbit remoteout=P3^5; //遙控信號(hào)輸出口
sbit txkey=P3^0; //發(fā)射鍵
uint i,j,m=2040,n,k,s;
uchar idata remotedata[206]; //存脈沖寬度數(shù)據(jù)用
uint head; //存起始位用
uint remdata; //
/****一毫秒延時(shí)程序****/
delay1ms(uint t)
{
for(i=0;i<t;i++)
for(j=0;j<120;j++)
;
}
/****初始化程序****/
clearmen()
{
studylamp=1;//關(guān)學(xué)習(xí)指示燈
lamp=1; //關(guān)發(fā)射指示燈
remoteout=0; //關(guān)遙控輸出
remotein=1; //
for(i=0;i<206;i++)
{remotedata=0x00;} //清內(nèi)存
IE=0x00;
IP=0x01;
TMOD=0x22;//八位自動(dòng)重裝模式
PCON=0x00;
TH1=0xf3;//40KHz初值
TL1=0xf3;
IT1=1;//脈沖延跳變模式
EX1=1; //外中斷允許
EA=1; //開總中斷
}
/*****鍵功能函數(shù)****/
keywork()
{
if (txkey==0)
{while(txkey==0);//等待鍵釋放
ET1=1;
TR1=1;//發(fā)起始位
for(i=head;i>0;i--){;}
remoteout=0;
ET1=0;
TR1=0;
n=0;
while(1)
{ if(remotedata[n]==0)
{delay1ms(10);break;} //數(shù)據(jù)為0結(jié)束
for(i=remotedata[n];i>0;i--)
{_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
lamp=~lamp;
n++;//偶地址不發(fā)送
ET1=1;
TR1=1;//奇地址發(fā)送調(diào)制脈沖
for(i=remotedata[n];i>0;i--){;}
remoteout=0;
ET1=0;
TR1=0;
n++;
}
}
}
/******主函數(shù)****/
main()
{
clearmen();
while(1)
{
keywork();
}
}
/****40KHz載波發(fā)生函數(shù)*****/
void time_intt(void)interrupt 3
{
remoteout=~remoteout;
}
/*******學(xué)習(xí)函數(shù)*****/
//外中斷1
void intt1(void)interrupt 2
{
ET1=0;TR1=0;EA=0;EX0=0;EX1=0;
head=0;studylamp=0;lamp=1;
while(studykey==0);//等待鍵釋放
while(remotein==1);//等待遙控碼輸入
head=0;//讀入起始位
while(remotein==0)
{_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
head++;
}
head=head-1;
n=0;remdata=0x00;
while(1)
{
while(remotein==1)
{_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();remdata++;
}
if(remdata>m) //高電平大于5毫秒退出
{remotedata[n]=0x00;EX1=1;EA=1; goto end;}
remotedata[n]=remdata;n++;//存高電平脈寬數(shù)據(jù)
remdata=0x00;//脈寬計(jì)數(shù)器清零
while(remotein==0)
{_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
remdata++;
}//低電平計(jì)數(shù)
remotedata[n]=remdata;
n++;
remdata=0x00;//存低電平脈寬數(shù)據(jù)
}
end:lamp=0;studylamp=1;
}
歡迎光臨 (http://www.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |