新闻中心

EEPW首页>嵌入式系统>设计应用> 基于51单片机红外编码解码的C语言程序

基于51单片机红外编码解码的C语言程序

作者: 时间:2016-11-23 来源:网络 收藏
当学习键按下后,红外接收头便可将接受到的信号存储到单片机中。在接受到通用遥控器发出的信号后,如果按下发射键,单片机将调出刚刚储存的信息,通过红外发射头发射出和遥控器一样的信号来达到控制的作用。

#include
#include
#define uchar unsigned char
#define uint unsigned int

本文引用地址://m.amcfsurvey.com/article/201611/320393.htm

sbit studylamp=P2^7;
sbit lamp=P2^6;
sbit studykey=P3^2;
sbit remotein=P1^1;
sbit remoteout=P1^0;
sbit txkey=P2^0;

uint i,j,m=255,n,k,s;
uchar idata remotedata[206];
uint head;
uint remdata;


//一毫秒延时程序
delay1ms(uint t)
{
for(i=0;i for(i=0;j<120;j++);
}

//初始化函数
clearmen()
{
studylamp=1;
lamp=1;
remoteout=0;
remotein=1;
for(i=0;i<206;i++)
{
remotedata[i]=0x00;
}
IE=0x00;
IP=0x01;
TMOD=0x22;
PCON=0X00;
TH1=0xf3;
TL1=0xf3;
IT0=1;
EX0=1;
EA=1;
}

//键功能函数
void key_tx()
{
if(txkey==0)
{
delay1ms(1);
if(txkey==0)
{
while(txkey==0);
ET1=1;TR1=1;
for(i=head;i>0;i--);
remoteout=0;ET1=0;TR1=0;
n=0;
while(1)
{
lamp=0;
studylamp=1;
if(remotedata[n]==0x00)
{
delay1ms(10);break;
}
for(i=remotedata[n];i>0;i--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
n++;
ET1=1;TR1=1;
for(i=remotedata[n];i>0;i--);
remoteout=0;ET1=0;TR1=0;n++;
}
}
}
}

//主函数
void main()
{
clearmen();//初始化
while(1)
{
key_tx();//按键扫描
}
}

//40KHz发生器
void time_intt1(void)interrupt3
{
remoteout=~remoteout;
}


//外中断0
void intt0(void) interrupt 0
{
ET1=0;TR1=0;EX0=0;EA=0;
head=0;
while(studykey==0);
studylamp=0;lamp=1;
while(remotein==1);
head=0;
while(remotein==0)
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
head++;
}
n=0;remdata=0x0000;
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;EX0=1;EA=1;goto end;}
remotedata[n]=remdata;n++;
remdata=0x0000;
while(remotein==0)
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
remdata++;
}
remotedata[n]=remdata;
n++;
remdata=0x00;
}
end: lamp=0;studylamp=0;
}



评论


技术专区

关闭