#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit output = P1^0;
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sta
tic bit OP; //红外发射管的亮灭
static unsigned int count; //延时计数器
static unsigned int endcount; //终止延时计数
static unsigned char flag; //红外发送标志
void SendIRdata(char p_irdata1,char p_irdata2,char p_irdata3,char p_irdata4,char p_irdata5);
void delay();
uchar KEY();
void Delay(uint del)
{
uint i,j;
for(i=0; i<del; i++)
for(j=0; j<1827; j++)
;
}
void main(void)
{
uchar t;
count = 0;
flag = 0;
OP = 0;
output = 0;
EA = 1; //允许CPU中断
TMOD = 0x11; //设定时器0和1为16位模式1
ET0 = 1; //定时器0中断允许
TH0 = 0xFF;
TL0 = 0xE8; //设定时值0为38K 也就是每隔26us中断一次 ,晶振11.0592M
TR0 = 1;//开始计数
do{
t=KEY();
switch(t)
{
case 0:
break;
case 1:
SendIRdata(25,2,224,80,4); // 1900e0514
break;
case 2:
SendIRdata(17,0,160,80,4); //
break;
case 3:
SendIRdata(25,1,224,80,4); //
break;
case 4:
SendIRdata(25,2,224,80,4); //
break;
}
}while(1);
}
//定时器0中断处理
void timeint(void) interrupt 1
{
TH0=0xFF;
TL0=0xE8; //设定时值为38K 也就是每隔26us中断一次
count++;
if (flag==1)
{
OP=~OP;
}
else
{
OP = 0;
}
output = OP;
}
void SendIRdata(char p_irdata1,char p_irdata2,char p_irdata3,char p_irdata4,char p_irdata5)
{
int i;
char irdata;
//发送9ms的起始码
endcount=250;
flag=1;
count=0;
do{}while(count<endcount);
//发送4.5ms的结果码
endcount=117;
flag=0;
count=0;
do{}while(count<endcount);
//发送十六位地址的前八位
irdata=p_irdata1;
for(i=0;i<8;i++)
{
//先发送0.56ms的38KHZ红外波(即编码中0.56ms的高电平)
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
//停止发送红外信号(即编码中的高电平)
if(irdata-(irdata/2)*2) //判断二进制数个位为1还是0
{
endcount=45; //1为宽的低电平
}
else
{
endcount=15; //0为窄的低电平
}
flag=0;
count=0;
do{}while(count<endcount);
irdata=irdata>>1;
}
//发送十六位地址的后八位
irdata=p_irdata2;
for(i=0;i<8;i++)
{
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
if(irdata-(irdata/2)*2)
{
endcount=45;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count<endcount);
irdata=irdata>>1;
}
//发送八位数据
irdata=p_irdata3;
for(i=0;i<8;i++)
{
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
if(irdata-(irdata/2)*2)
{
endcount=45;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count<endcount);
irdata=irdata>>1;
}
//发送八位数据2
irdata=p_irdata4;
for(i=0;i<8;i++)
{
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
if(irdata-(irdata/2)*2)
{
endcount=45;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count<endcount);
irdata=irdata>>1;
}
//发送35位数据中最后三位数据
irdata=p_irdata5;
irdata=irdata>>1; //因为从低位开始发
for(i=0;i<3;i++)
{
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
if(irdata-(irdata/2)*2)
{
endcount=45;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count<endcount);
irdata=irdata>>1;
}
endcount=12;
flag=1;
count=0;
do{}while(count<endcount);
flag=0;
}
void delay()
{
int i,j;
for(i = 0; i < 400; i++){
for(j = 0; j < 200; j++){
}
}
}
uchar KEY()
{
if(KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0)
{
Delay(2);
if(KEY1 == 0)
{
while(1)
{
if(KEY1 == 1)
{
Delay(2);
if(KEY1 == 1)
{
break;
}
}
}
return 1;
}
if(KEY2 == 0)
{
while(1)
{
if(KEY2 == 1)
{
Delay(2);
if(KEY2 == 1)
{
break;
}
}
}
return 2;
}
if(KEY3 == 0)
{
while(1)
{
if(KEY3 == 1)
{
Delay(2);
if(KEY3 == 1)
{
break;
}
}
}
return 3;
}
if(KEY4 == 0)
{
while(1)
{
if(KEY4 == 1)
{
Delay(2);
if(KEY4 == 1)
{
break;
}
}
}
return 4;
}
}
return 0;
}
一周热门 更多>