调试了好几天了,都没有成功,还请高手帮忙看一下程序是否有问题.谢谢
一体红外接收头接到P3^2脚上,
#include <at89x52.h>
#include <1602LCD.h>
#define uchar unsigned char
sbit IR=P3^2; //接收头
sbit sp=P2^1; //蜂鸣器
bit ok; //解码成功标志
static unsigned int a,d;
extern display_long_char(uchar x,uchar y,uchar *pa);
void display_char (uchar x,uchar y,uchar temp);
extern void INIT_LCD (void);
unsigned char code tab1[16]={" LCD REMOTE TSET"};
void beep (void) //蜂鸣器程序
{
unsigned int t;
t=50000;
sp=0;
while (t)
{t--;}
sp=1;
}
void delay840ns (void) //延时0.84ms
{
unsigned int t=860;
while (t--);
}
void delay9ms (void) //延时9ms
{
unsigned int t=50000;
unsigned int a;
while (t--)
{
for (a=60;a<=0;a--);
}
}
void starting (void) //初始化中断
{
IT0=1;
EX0=1;
EA=1;
}
void hex_bin (unsigned int r,unsigned int t) //转换程序
{
unsigned char a1,a2,d1,d2;
a1=(char)r&0xff;
a2=(char)r>>8;
d1=(char)t&0xff;
d2=(char)t>>8;
display_char (2,2,a1+0x30);
display_char (2,3,'A');
}
main ()
{
beep ();
INIT_LCD ();
display_long_char (1,1,tab1);
starting (); //初始化
while (1)
{
display_long_char (1,1,tab1);
if (ok) hex_bin (a,d);
}
}
void remote (void) interrupt 0
{
unsigned char i;
a=0;
d=0;
EA=1;
return;
delay9ms ();
if (IR) while (IR) if (!IR) {EA=1;ok=0;return;} //等等4.5MS结果码
else {EA=1;ok=0;return;} //出错返回
while (IR==0);
for (i=0;i<16;i++)
{
delay840ns ();
if (IR==1) {a=(a>>1)|0x80;while (IR==1);} //取0
else a=(a>>1); //取1
while (IR==0);
}
for (i=0;i<16;i++)
{
delay840ns ();
if (IR==1) {d=(d<<1)|1;while (IR==1);} //取0
else d=(d<<1); //取1
while (IR==0);
}
beep ();
ok=1;
EA=1;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
/*24M时的延时函数*
完成时间:2009-06-17
作者:周勇*/
#include <at89x52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit di1=P3^4; //数码管控制位
sbit di2=P3^5; //数码管控制位
sbit di3=P3^6; //数码管控制位
sbit di4=P3^7; //数码管控制位
sbit IR=P3^2;
sbit sp=P1^3; //蜂鸣器
bit ok;
uchar recode[4]={0,0,0,0};
void delay840us(void) //delay 0.84ms
{
uint i;
for(i=0;i<188;i++)
{
_nop_();
}
}
void delay1ms (void) //delay 1ms
{
uint i;
for (i=0;i<222;i++)
{
_nop_();
}
}
void delay4700us (void) //delay 接近5MS
{
uint i;
for (i=0;i<1600;i++)
{
_nop_();
}
}
void scan_delay (unsigned int i) //扫描延时 5 MS
{
unsigned char b;
for (;i>0;i--)
{
for (b=200;b>0;b--)
{}
}
}
void beep (void) //蜂鸣器程序
{
unsigned int t;
t=60000;
sp=0;
while (t--)
{
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
_nop_ ();
}
sp=1;
}
IR_Remote (void) interrupt 0 using 1 //解码核心程序
{
uchar t,count;
EX0=0;
for (t=0;t<8;t++)
{
delay1ms ();
if (IR) {EX0=1;return;}
}
if (!IR)
{
delay4700us ();
for (t=0;t<4;t++)
{
for (count=0;count<8;count++)
{
while (!IR);
delay840us ();
if (IR) {recode[t]=recode[t]>>1|0x80;while (IR);}
else recode[t]=recode[t]>>1;
}
}
}
if (recode[0]==~recode[1]) //校验解码
{
beep ();
ok=1;
}
else
{
ok=0;
}
EX0=1;
return;
}
main ()
{
IT0=1;
EX0=1;
EA=1;
while (1)
{
if (ok)
{
P0=recode[0];
di1=0;
scan_delay (10);
di1=1;
P0=0XFF;
P0=recode[1];
di2=0;
scan_delay (10);
di2=1;
P0=0XFF;
P0=recode[2];
di3=0;
scan_delay (10);
di3=1;
P0=0XFF;
P0=recode[3];
di4=0;
scan_delay (10);
di4=1;
P0=0XFF;
}
/* else
{
P0=0xbf;
di1=0;
scan_delay (10);
di1=1;
P0=0XFF;
P0=0xbf;
di2=0;
scan_delay (10);
di2=1;
P0=0XFF;
P0=0xbf;
di3=0;
scan_delay (10);
di3=1;
P0=0XFF;
P0=0xbf;
di4=0;
scan_delay (10);
di4=1;
P0=0XFF;
}*/
}
}
一周热门 更多>