include
#define uint unsigned int
#define uchar unsigned char
unsigned char a[4]; //储存用户码、用户反码与键数据码、键数据反码
unsigned int LowTime,HighTime; //储存高、低电平的宽度
sbit led_r=P1^0;
sbit led_g=P1^2;
sbit led_b=P1^4;
sbit IR=P3^2;
sbit BEEP = P2^3;
bit kg_flag;
bit zb_flag;
bit m_flag;
bit s_flag;
bit f_flag;
uchar num,tl,tb,tm,tj;tt;
uchar number;
uchar scw;
uchar pwm_r,pwm_g,pwm_b;
uchar pwm[3];
uchar bright,bright_t;
uchar x[30]={0};
uchar type[30]={0};
void dealy(uint z)
{ uint x,y; for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
void delay(unsigned char n)
{
unsigned char i;
for(i=0;i0){led_r=0;pwm_r--;}
else{led_r=1;}
if(pwm_g>0){led_g=0;pwm_g--;}
else{led_g=1;}
if(pwm_b>0){led_b=0;pwm_b--;}
else{led_b=1;}
scw++;
}}*/
bit DeCode(void)
{
unsigned char i,j;
unsigned char temp; //储存解码出的数据
for(i=0;i<4;i++) //连续读取4个用户码和键数据码
{
for(j=0;j<8;j++) //每个码有8位数字
{
temp=temp>>1; //temp中的各数据位右移一位,因为先读出的是高位数据
TH0=0; //定时器清0
TL0=0; //定时器清0
TR0=1; //开启定时器T0
while(IR==0) //如果是低电平就等待
; //低电平计时
TR0=0; //关闭定时器T0
LowTime=TH0*256+TL0; //保存低电平宽度
TH0=0; //定时器清0
TL0=0; //定时器清0
TR0=1; //开启定时器T0
while(IR==1) //如果是高电平就等待
;
TR0=0; //关闭定时器T0
HighTime=TH0*256+TL0; //保存高电平宽度
if((LowTime<370)||(LowTime>640))
return 0; //如果低电平长度不在合理范围,则认为出错,停止解码
if((HighTime>420)&&(HighTime<620)) //如果高电平时间在560微秒左右,即计数560/1.085=516次
temp=temp&0x7f; //(520-100=420, 520+100=620),则该位是0
if((HighTime>1300)&&(HighTime<1800)) //如果高电平时间在1680微秒左右,即计数1680/1.085=1548次
temp=temp|0x80; //(1550-250=1300,1550+250=1800),则该位是1
}
a[i]=temp; //将解码出的字节值储存在a[i]
}
if(a[2]=~a[3]) //验证键数据码和其反码是否相等,一般情况下不必验证用户码
return 1; //解码正确,返回1
}
void exter_int0() interrupt 0 using 0
{ EX0=0;
//关闭外中断0,不再接收二次红外信号的中断,只解码当前红外信号
TH0=0; //定时器T0的高8位清0
TL0=0; //定时器T0的低8位清0
TR0=1; //开启定时器T0
while(IR==0); //如果是低电平就等待,给引导码低电平计时
TR0=0; //关闭定时器T0
LowTime=TH0*256+TL0; //保存低电平时间
TH0=0; //定时器T0的高8位清0
TL0=0; //定时器T0的低8位清0
TR0=1; //开启定时器T0
while(IR==1); //如果是高电平就等待,给引导码高电平计时
TR0=0; //关闭定时器T0
HighTime=TH0*256+TL0; //保存引导码的高电平长度
if((LowTime>7800)&&(LowTime<8800)&&(HighTime>3600)&&(HighTime<4700))
{
//如果是引导码,就开始解码,否则放弃,引导码的低电平计时
//次数=9000us/1.085=8294, 判断区间:8300-500=7800,8300+500=8800.
if(DeCode()==1); // 执行遥控解码功能
{// beep();
switch(a[2])
{
case 0x07:
{kg_flag=1;
TR0=1;
f_flag=0;
s_flag=0;
zb_flag=0;
number=0;
m_flag=0;
bright_t=225;
pwm[0]=bright_t;
pwm[1]=bright_t;
pwm[2]=bright_t;
TR1=0;
}
break;
case 0x06:
{
kg_flag=0;
pwm[0]=0;
pwm[1]=0;
pwm[2]=0;
} break;
case 0x04:
{
if((kg_flag==1)&&(m_flag==0)&&
(s_flag==0)&&
(zb_flag==0)&&(f_flag==0))
{
if(bright_t!=25)
{
bright_t=bright_t-25;
}
}
} break;
case 0x05:
{
if((kg_flag==1)&&(m_flag==0)&&
(s_flag==0)&&
(zb_flag==0)&&(f_flag==0))
{
if(bright_t!=255)
{
bright_t=bright_t+25;
}
}
} break;
case 0x09:
{
if(kg_flag==1)
{pwm[0]=bright_t;
pwm[1]=0;
pwm[2]=0;
}
} break;
case 0x08:
{if(kg_flag==1)
{pwm[0]=0;
pwm[1]=bright_t;
pwm[2]=0;}
}break;
case 0x0A:
{if(kg_flag==1)
{pwm[0]=0;
pwm[1]=0;
pwm[2]=bright_t;}
}break;
}
}
}
EX0=1;
}
void main()
{
init();
while(1);
}
void timer1()interrupt 1 using 1
{TR1=1;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
scw++;
if(scw==255)
{
scw=0;
pwm_r=pwm[0];
pwm_g=pwm[1];
pwm_b=pwm[2];
}
else
{
if(pwm_r>0){led_r=0;pwm_r--;}
else{led_r=1;}
if(pwm_g>0){led_g=0;pwm_g--;}
else{led_g=1;}
if(pwm_b>0){led_b=0;pwm_b--;}
else{led_b=1;}
}TR1=0;
} 怎么没有高手回下,改了下现在按遥控后一直闪,好像T1一直中断,请高手指下