#include
#include
sbit IR = P3^2;
sbit BEEP = P3^6;
sbit PWM_OUT = P0^0;
bit inf_flag = 1; //ºìÍâ½ÓÊյĿªÆô¹Ø±Õ¿ØÖÆ룬ÔÚºìÍâ½âÂë¹ý³ÌÖÐÒª¹Ø±ÕºìÍâ½ÓÊÕ
bit ecode_ok; //½âÂë³É¹¦±ê־λ
unsigned char ircode[4];
unsigned int LowTime,HighTime; //ºìÍâ½ÓÊÕ¹ý³ÌÖб£´æ¸ß¡¢µÍµçƽµÄ¿í¶È
unsigned char se_PWM =15; //³õʼ»¯¶æ»úµÄPWM£¨¸ßµçƽʱ¼ä£©
unsigned char key_stime_counter; //¼Ç¼×ܵÄÖжϴÎÊý£¬³¬¹ý200(20ms)´Î¾Í»Ø0
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;i
delay1ms();
}
void beep()
{
unsigned char i;
for(i=0;i<100;i++)
{
delay1ms();
BEEP = !BEEP; //beepÈ¡·´
}
BEEP = 1 ;//¹Ø±Õ·äÃùÆ÷
delay(250); //ÑÓʱ
}
void DeCode()
{
unsigned char i,j;
unsigned char temp; //´¢´æ½âÂë³öµÄÊý¾Ý
for(i=0;i<4;i++) //Á¬Ðø¶ÁÈ¡4¸öÓû§ÂëÓëÊý¾ÝÂë
{
for(j=0;j<8;j--) //ÿ¸öÂëÓÐ8λÊý¾Ý
{
temp = temp>>1; //¸÷Êý¾ÝλÓÒÒÆһ룬ÒòΪÏȶÁ³öµÄÊǸßλÊý¾Ý
TH0 = 0;
TL0 = 0; //¶¨Ê±Æ÷0ÇåÁã
TR0 = 1; //´ò¿ª¶¨Ê±Æ÷0
while(IR == 0)
{
;
} //Èç¹ûÊǵ͵çƽ¾ÍµÈ´ý£¬¸øÒýµ¼ÂëµÍµçƽ¼Æʱ
TR0 = 0;//¹Ø±Õ¶¨Ê±Æ÷0
LowTime = TH0*256 + TL0; //±£´æµÍµçƽʱ¼ä
TH0 = 0;
TL0 = 0; //¶¨Ê±Æ÷0ÇåÁã
TR0 = 1; //´ò¿ª¶¨Ê±Æ÷0
while(IR == 1)
{
;
} //Èç¹ûÊǸߵçƽ¾ÍµÈ´ý
TR0 = 0;
HighTime = TH0*256 + TL0; //±£´æ¸ßµçƽʱ¼ä
if((LowTime < 370)||(LowTime >640))
{
ecode_ok = 0; //Èç¹ûµÍµçƽ³¤¶È²»ÔÚºÏÀí·¶Î§£¬ÔòÈÏΪ³ö´í£¬Í£Ö¹½âÂë
}
if((HighTime > 420) && (HighTime < 620))
//Èç¹û¸ßµçƽʱ¼äÔÚ560us×óÓÒ£¬560us / 1.085us = 516 ´Î
{
temp = temp&0x7f; //(520 - 100 =420 , 520 + 100 = 620),Ϊ0
}
if((HighTime >1300) && (HighTime < 1800))
//Èç¹û¸ßµçƽʱ¼äÔÚ1680us×óÓÒ£¬¼ÆÊý 1680 / 1.085 = 1548´Î
{
temp |= 0x80; //(1550-250 = 1300 £¬ 1500+250 = 1800)£¬Îª1
}
ircode[i] = temp;
}
if(ircode[2] = ~ircode[3]) //ÑéÖ¤Êý¾ÝÊÇ·ñÏàµÈ
{
//Èç¹û½âÂëÕýÈ·¾Í½øÈë¶Ô¶æ»úµÄ²Ù×÷
if(ircode[2] == 0x44) //×óת
{
se_PWM -= 5;
if(se_PWM <= 10)
se_PWM = 10;
}
else if(ircode[2] == 0x43)//ÓÒת
{
se_PWM+=5;
if(se_PWM >=15)
se_PWM = 15;
}
ecode_ok = 1; //½âÂëÕýÈ·£¬·µ»Ø1
}
}
}
void inf()//ºìÍâ½ÓÊÕ
{
if(inf_flag & IR == 0)
{
inf_flag = 0; //¹Ø±ÕºìÍâ¼ì²â
TH0 = 0;
TL0 = 0; //¶¨Ê±Æ÷0ÇåÁã
TR0 = 1; //´ò¿ª¶¨Ê±Æ÷0
while(IR == 0)
{
;
} //Èç¹ûÊǵ͵çƽ¾ÍµÈ´ý£¬¸øÒýµ¼ÂëµÍµçƽ¼Æʱ
TR0 = 0;//¹Ø±Õ¶¨Ê±Æ÷0
LowTime = TH0*256 + TL0; //±£´æµÍµçƽʱ¼ä
TH0 = 0;
TL0 = 0; //¶¨Ê±Æ÷0ÇåÁã
TR0 = 1; //´ò¿ª¶¨Ê±Æ÷0
while(IR == 1)
{
;
} //Èç¹ûÊǸߵçƽ¾ÍµÈ´ý
TR0 = 0;
HighTime = TH0*256 + TL0; //±£´æ¸ßµçƽʱ¼ä
if((LowTime > 7800) && (LowTime < 8800) && (HighTime > 3600) && (HighTime > 4700))
//Èç¹ûÊÇÒýµ¼Â룬¾Í¿ªÊ¼½âÂ룬·ñÔò·ÅÆú£¬Òýµ¼ÂëµÄµÍµçƽ¼Æʱ
//µÍµçƽ´ÎÊý = 9000us /(12/11.0592) = 8294 ,ÅжÏÇø¼ä£º8300-500 = 7800£¬ 8300+500 = 8800
{
DeCode();
if(ecode_ok == 1) //Ö´ÐÐÒ£¿Ø½âÂ빦ÄÜ
{
beep();//·äÃùÆ÷ÏìÒ»Éù£¬Ìáʾ½âÂë³É¹¦
}
}
inf_flag = 1; //¿ªÆôºìÍâ¼ì²â
}
}
void main()
{
EA = 1; //´ò¿ª×ÜÖжÏ
EX0 = 1; //´ò¿ªÍⲿÖжÏ0
//ET0 = 1; //¶¨Ê±Æ÷0ÖжÏÔÊÐí
IT0 = 1; //ÍⲿÖжÏ0ϽµÑØ´¥·¢
TMOD = 0x11;//ʹÓö¨Ê±Æ÷0·½Ê½1
TR0 = 0; //¶¨Ê±Æ÷0¹Ø±Õ
PWM_OUT = 0; //³õʼ»¯¶æ»ú
ET1 = 1; //¶¨Ê±Æ÷1ÖжÏÔÊÐí
TH1 = 0xFF;
TL1 = 0xA4;
TR1 = 1; //Æô¶¯¶¨Ê±Æ÷1
while(1) //µÈ´ýºìÍâÐźŲúÉúµÄÖжÏ
{
inf();
}
}
//¶¨Ê±Æ÷1ÖжϷþÎñº¯Êý
//Ö÷Òª¸ºÔð²úÉú¶æ»úËùÐèµÄPWM²¨
void timer1()interrupt 3 using 0
{
TH1 = 0xFF;
TL1 = 0xA4; //¶¨Ê±Æ÷1ÖØÐÂ×°ÈëÊýÖµ0.1ms
if(++key_stime_counter >= 200) //20msʱ¼äµ½
{
key_stime_counter = 0;
}
if(key_stime_counter <=se_PWM)//Éú³ÉPWM²¨£¬¿ØÖƶæ»úתÏò
{
PWM_OUT = 1;
}
else
{
PWM_OUT = 0;
}
} |