51单片机红外遥控lcd1602解码,定时器工作方式1不写中断函数,为什么液晶屏上数字一闪即逝
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
sbit IR=P3^2;
sbit RS=P2^1;
sbit RW=P1^5;
sbit E=P1^6;
#define DataPort P0 //定义数据端口 程序中遇到DataPort 则用P0 替换
char code Tab[16]="0123456789ABCDEF";
unsigned char f=0;
unsigned char Im[4]={0x00,0x00,0x00,0x00};
unsigned char show[2]={0,0};
unsigned long m,Tc;
unsigned char IrOK;
void delay(unsigned char x)
{
unsigned char a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void time0() interrupt 1
{
TH0=0;
TL0=0;
}
void intersvr1(void) interrupt 0 using 1
{
Tc=TH0*256+TL0; //提取中断时间间隔时长
TH0=0;
TL0=0; //定时中断重新置零
if((Tc>Imin)&&(Tc<Imax))
{
m=0;
f=1;
return;
} //找到启始码
if(f==1)
{
if(Tc>Inum1&&Tc<Inum3)
{
Im[m/8]=Im[m/8]>>1|0x80; m++;
}
if(Tc>Inum2&&Tc<Inum1)
{
Im[m/8]=Im[m/8]>>1; m++; //取码
}
if(m==32)
{
m=0;
f=0;
if(Im[2]==~Im[3])
{
IrOK=1;
}
else IrOK=0; //取码完成后判断读码是否正确
}
//准备读下一码
}
}
void Time0_ini()
{
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
TR0=1;
}
void EX0_ini()
{
IT0=1;
EX0=1;
EA=1;
}
void write_com(unsigned char com)
{
RS=0;
delay(1);
E=1;
P0=com;
E=0;
}
void write_dat(unsigned char dat)
{
RS=1;
delay(1);
E=1;
P0=dat;
E=0;
}
void ini()
{
E=0;
RW=0;
write_com(0x38);
write_com(0x0c);//设置光标
write_com(0x06);
write_com(0x01);//清屏
write_com(0x80);
wela=1;
P0=0xff;
wela=0;
}
void Ir_work(void)//红外键值散转程序
{
write_com(0x80);
write_dat(Tab[Im[2]/16]);
write_dat(Tab[Im[2]%16]);
IrOK=0;
}
void main()
{
Time0_ini();
EX0_ini();
ini();
while(1)
{
if(IrOK)
{
Ir_work();
}
}
}
sbit IR=P3^2;
sbit RS=P2^1;
sbit RW=P1^5;
sbit E=P1^6;
#define DataPort P0 //定义数据端口 程序中遇到DataPort 则用P0 替换
#define Imax 14000 //此处为晶振为11.0592时的取值,
#define Imin 8000 //如用其它频率的晶振时,
#define Inum1 1450 //要改变相应的取值。
#define Inum2 700
#define Inum3 3000
sbit wela=P2^7;char code Tab[16]="0123456789ABCDEF";
unsigned char f=0;
unsigned char Im[4]={0x00,0x00,0x00,0x00};
unsigned char show[2]={0,0};
unsigned long m,Tc;
unsigned char IrOK;
void delay(unsigned char x)
{
unsigned char a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void time0() interrupt 1
{
TH0=0;
TL0=0;
}
void intersvr1(void) interrupt 0 using 1
{
Tc=TH0*256+TL0; //提取中断时间间隔时长
TH0=0;
TL0=0; //定时中断重新置零
if((Tc>Imin)&&(Tc<Imax))
{
m=0;
f=1;
return;
} //找到启始码
if(f==1)
{
if(Tc>Inum1&&Tc<Inum3)
{
Im[m/8]=Im[m/8]>>1|0x80; m++;
}
if(Tc>Inum2&&Tc<Inum1)
{
Im[m/8]=Im[m/8]>>1; m++; //取码
}
if(m==32)
{
m=0;
f=0;
if(Im[2]==~Im[3])
{
IrOK=1;
}
else IrOK=0; //取码完成后判断读码是否正确
}
//准备读下一码
}
}
void Time0_ini()
{
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
TR0=1;
}
void EX0_ini()
{
IT0=1;
EX0=1;
EA=1;
}
void write_com(unsigned char com)
{
RS=0;
delay(1);
E=1;
P0=com;
E=0;
}
void write_dat(unsigned char dat)
{
RS=1;
delay(1);
E=1;
P0=dat;
E=0;
}
void ini()
{
E=0;
RW=0;
write_com(0x38);
write_com(0x0c);//设置光标
write_com(0x06);
write_com(0x01);//清屏
write_com(0x80);
wela=1;
P0=0xff;
wela=0;
}
void Ir_work(void)//红外键值散转程序
{
write_com(0x80);
write_dat(Tab[Im[2]/16]);
write_dat(Tab[Im[2]%16]);
IrOK=0;
}
void main()
{
Time0_ini();
EX0_ini();
ini();
while(1)
{
if(IrOK)
{
Ir_work();
}
}
}
按照以往的经验感觉是刷新率的问题
这是LCD不是LED。
你还是仔细研究下的程序把