红外遥控小车程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
附录一程序代码
红外遥控编码程序
#include
#define uchar unsigned char
#define uint unsigned int
#define uintlong unsigned long
sbit p3_0=P3^0;
sbit p2_1=P2^1;
sbit p1_0=P1^0;
sbit p1_1=P1^1;
sbit p1_2=P1^2;
sbit p1_3=P1^3;
bit out;
uint keyvalue=0x00,flag_key=0,value1,value2,keycount=0,i,j,flag_set=0, flag_press=0;
uchar code keycode[4]={0x7f,0xbf,0xdf,0xef};
uchar code
portvalue[12]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x00, 0x0b};
uchar code wy[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//**********红外发送部分**********//
uchar user1=0x00,user2=0x00;
uint count=0,endcount=0;
uint irdata=0;
void deltime(void);
void key_scan(void);
void sendirdata(void);
main(void)
{
EA = 1;
TMOD = 0x11;
ET0 = 1;
p3_0=1;
P1=0xff;
TH0 = 0xFF;
TL0 = 0xE4;
TR0 = 0;
while(1)
{
key_scan();
if(flag_press==1)
{
flag_press=0;
TR0=1;
sendirdata();
}
}
}
//**********按键扫描***********// void key_scan(void)
{
for(i=0;i<=3;i++)
{
P1=keycode[i];
if(p1_3==0)
{keycount=i*3+0;flag_key=1; break;}
if(p1_2==0)
{keycount=i*3+1;flag_key=1;break;}
if(p1_1==0)
{keycount=i*3+2;flag_key=1;break;}
}
if(flag_key==1)
{
flag_key=0;
value1=P1;
deltime();
value2=P1;
if(value1==value2)
{keyvalue=portvalue[keycount];flag_set=1;flag_press=1;}
while(flag_set)
{value2=P1;
if(value1!=value2)
flag_set=0;
}
}
}
//**********延时**********//
void deltime(void)
{
uint k;
for(k=0;k<=20;k++)
{ }
}
//**********定时中断**********// void time0int(void) interrupt 1
{
TH0=0xFF;
TL0=0xE4;
count++;
}
//**********发送数据**********// void sendirdata()
{
uchar s=0,datapd=0;
endcount=320;
p3_0=0;
count=0;
do{}while(count count=0; p3_0=1; do{}while(count for(s=0;s<=11;s++) { endcount=20; count=0; p3_0=0; do{}while(count endcount=20; count=0; p3_0=1; do{}while(count } irdata=keyvalue; for(s=0;s<=7;s++) { datapd=irdata & wy[s]; if (datapd==0) {endcount=20;count=0;} else {endcount=60;count=0;} p3_0=0; do{}while(count endcount=20;count=0; p3_0=1; do{}while(count } irdata=keyvalue; for(s=0;s<=7;s++) { datapd=irdata & wy[s]; if (datapd==0) {endcount=60;count=0;} else {endcount=20;count=0;}