谢谢你们!小弟想做一个红外遥控系统,就是用
单片机编码发射红外数据,用另一个单片机接收并解码。这边按下 key2,key3,key4,那边相应的灯1,灯2,灯3点亮。下面是小弟的程序,新手写的不好,调了几天没有现象,请哥哥姐姐们帮忙调试下。由于红外不能
仿真,我用导线代替了发送和接收。
下边是程序
//*****************************红外发射***************************************************
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit ir=P1^4;
sbit k1=P2^1;
sbit k2=P2^2;
sbit k3=P2^3;
sbit k4=P2^4;
sbit k5=P2^5; //备用
uint count,set_count;
bit irflag,keyflag,irsendok;
uchar irdata,ircode;
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
EA=1;
TMOD=0x02; //定时0 8位自动重装模式
ET0=1;
TH0=0xe6; //定时13us,38K红外矩形波,晶振24M
TL0=0xe6;
count=0;
irflag=0;
ir=0;
}
void keyscan()
{
if(k1==0)
{
delay(10);
if(k1==0)
{
keyflag=1;
irdata=0x01;
}
}
if(k2==0)
{
delay(10);
if(k2==0)
{
keyflag=1;
irdata=0x02;
}
}
if(k3==0)
{
delay(10);
if(k3==0)
{
keyflag=1;
irdata=0x03;
}
}
if(k4==0)
{
delay(10);
if(k4==0)
{
keyflag=1;
irdata=0x04;
}
}
if(k5==0)
{
delay(10);
if(k5==0)
{
keyflag=1;
irdata=0x05;
}
}
}
void sendbyte()
{
uchar n;
for(n=8;n>0;n--)
{
set_count=43; //发送编码中的0.56ms高电平
irflag=1; //启动红外发射二极管发射
count=0; //中断计数先清0
TR0=1; //开启定时器0
while(count<set_count); //等待发送完
TR0=0; //关闭定时器0
if(ircode&0x01) set_count=130; //判断红外编码最低位,若为1则1.69ms的低电平
else set_count=43; //为0则0.565ms的低电平
irflag=0; //低电平则不发射
count=0; //中断计数清0
TR0=1; //开启定时器0
while(count<set_count); //等待发送完
TR0=0; //关闭定时器0
ircode=ircode>>1; //将数据右移,即从低位到高位发送
}
}
void senddata()
{
set_count=346; //发送编码中的引导码(4.5ms高电平)
irflag=1; //启动红外发射二极管发射
count=0; //中断计数清0
TR0=1;
while(count<set_count);
TR0=0;
set_count=346; //发送编码中的4.5ms低电平
irflag=0; //低电平则不发射
count=0;
TR0=1;
while(count<set_count); //等待发送完
TR0=0;
ircode=irdata; //发送8位数据码
sendbyte();
ircode=~irdata; //发送8位数据反码
sendbyte();
irsendok=1;
}
void main()
{
init();
while(1)
{
keyscan(); //键值扫描
if(keyflag) //若有按键按下
{
senddata(); //发送红外编码
if(irsendok==1)
{
keyflag=0; //按键标志清零
irsendok=0;
}
}
}
}
void
timer0() interrupt 1 //定时器0中断
{
count++;
if(irflag==1) ir=~ir; //有发射标志,则发射38khz的矩形波
else ir=0; //否则不发射,即相当于发射编码中的低电平
}
//******************************红外接收***********************************************
#include<reg52.h>
//#define uint unsigned int
#define uchar unsigned char
sbit led1=P2^1;
sbit led2=P2^2;
sbit led3=P2^3;
uchar irtime,startflag,irrecieveok;
uchar bitnum;
uchar irworkok;
uchar irdata[17];
uchar ircode[2];
void display();
void irwork();
/*void delay50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
} */
void init()
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
IT0=1;
EX0=1;
EA=1;
TR0=1;
}
void main()
{
init();
while(1)
{
if(irrecieveok==1)
{
irwork();
if(irworkok==1)
display();
}
}
}
void timer0() interrupt 1
{
irtime++;
}
void int0() interrupt 0
{
if(startflag)
{
if(irtime>32)
{
bitnum=0;
irdata[bitnum]=irtime;
irtime=0;
bitnum++;
if(bitnum==17)
{
bitnum=0;
irrecieveok=1;
}
}
}
else
{
startflag=1;
irtime=0;
}
}
void irwork()
{
char k,j,i,value;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
value=value>>1;
if(irdata[k]>6)
{
value=value|0x80;
}
k++;
}
ircode[j]=value;
}
irworkok=1;
}
void display()
{
if(ircode[0]==~ircode[1])
{
//uchar m;
switch(ircode[1])
{
case 0x80:
led1=1;
led2=1;
led3=1;
break;
case 0x40:
led1=0;
led2=1;
led3=1;
break;
case 0xC0:
led2=0;
led1=1;
led3=1;
break;
case 0x20:
led3=0;
led1=1;
led2=1;
break;
//case 0xA0: 备用
}
}
}
接收端由固定的接收头,TC9012解码程序+12864,我自己编的,
#include<reg52.h>//好不容易啊
#define uint unsigned int
#define uchar unsigned char
#define LED P0
sbit buzzer=P2^3;
sbit dula=P2^6;
sbit wela=P2^7;
sbit LCD_RS=P3^5;
sbit LCD_EN=P3^4;
sbit LCD_RW=P3^6;
sbit LCD_PSB=P3^7;//H
uint timer0_counter=0;
uchar ir_received_data[33];
uchar ir_transformed_data[4];
uchar code table1[]="成功了";
uchar code table2[]="你的名";
uchar code table3[]="2012.7.30";
uchar table4a[4];
uchar table4b[4];
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
bit IR_TRANSFER_FLAG;
bit IR_RECEIVE_FLAG;
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--)
;
}
void write_commond(uchar commond)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=commond;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void write_data(uchar dat)
{
LCD_RW=0;
LCD_RS=1;
LCD_EN=0;
P0=dat;
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void LCD12864_initial()
{
uchar i=0;
dula=0;
wela=0;
LCD_EN=0;
LCD_RW=0;
write_commond(0x30); //DL=1,8bits,RE=0,base
write_commond(0x0c);
write_commond(0x06);
write_commond(0x01);
}
void transform_ir_received_data();
void display(uint bai,uint shi,uint ge);
void Ir_work(void);
void timer0_initial()
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1 using 1
{
timer0_counter++;
}
void external_initerrupt0_initial()
{
EA=1;
EX0=1;
IT0=1;//负边缘触发
}
void ex0() interrupt 0 using 0
{
uchar i;
bit IR_START_FLAG;
IR_TRANSFER_FLAG=0;
IR_RECEIVE_FLAG=0;
buzzer=1;
LED=0xff;
if(IR_START_FLAG)
{
if((timer0_counter>=33)&&(timer0_counter<=42))
{
// timer0_counter=0;
i=0;
}
ir_received_data[i]=timer0_counter;
timer0_counter=0;
i++;
if(i==33)
{
IR_RECEIVE_FLAG=1;
i=0;
}
}
else
{
IR_START_FLAG=1;
timer0_counter=0;
}
}
void transform_ir_received_data()
{
uchar i=0,j=0,k=1,temp=0;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
temp>>=1;
if(ir_received_data[k]>=7)
temp=temp|0x80;
k++;
}
ir_transformed_data[j]=temp;
temp=0;
}
IR_TRANSFER_FLAG=1;
}
void tranfer_code()
{
uchar i=0;
for(i=0;i<4;i++)
{
if((ir_transformed_data[i]>>4)>9)
table4a[i]=(ir_transformed_data[i]>>4)-10+'A';
else
table4a[i]=(ir_transformed_data[i]>>4)+'0';
if((ir_transformed_data[i]&0x0f)>9)
table4b[i]=(ir_transformed_data[i]&0x0f)-10+'A';
else
table4b[i]=(ir_transformed_data[i]&0x0f)+'0';
}
}
void main()
{
uchar i=0;
timer0_initial();
external_initerrupt0_initial();
LCD12864_initial();
write_commond(0x88);
for(i=0;table2[i]!=' ';i++)
{
write_data(table2[i]);
}
write_commond(0x90);
for(i=0;table1[i]!=' ';i++)
{
write_data(table1[i]);
}
write_commond(0x98);
for(i=0;table3[i]!=' ';i++)
{
write_data(table3[i]);
}
while(1)
{
if(IR_RECEIVE_FLAG)
{
transform_ir_received_data();
tranfer_code();
IR_RECEIVE_FLAG=0;
}
if(IR_TRANSFER_FLAG)
{
write_commond(0x80);
write_data(table4a[0]);
write_commond(0x81);
write_data(table4b[0]);
if((table4a[2]=='0')&&(table4b[2]=='1'))
buzzer=0;
if((table4a[2]=='0')&&(table4b[2]=='2'))
LED=0;
write_commond(0x82);
write_data(table4a[1]);
write_commond(0x83);
write_data(table4b[1]);
一周热门 更多>