更新时间2019-03-02 16:48:18
#include<reg52.h> //°üº¬Í·Îļþ£¬Ò»°ãÇé¿ö²»ÐèÒª¸Ä¶¯£¬Í·Îļþ°üº¬ÌØÊ⹦ÄܼĴæÆ÷µÄ¶¨Òå
sbit IR=P3^2; //ºìÍâ½Ó¿Ú±êÖ¾
#define DataPort P0 //¶¨ÒåÊý¾Ý¶Ë¿Ú ³ÌÐòÖÐÓöµ½DataPort ÔòÓÃP0 Ìæ»»
sbit wei1 = P2^4;//¶¨ÒåµÚһλÊýÂë¹Ü
sbit wei2 = P2^5;//¶¨ÒåµÚ¶þλÊýÂë¹Ü
sbit wei3 = P2^6;//¶¨ÒåµÚÈýλÊýÂë¹Ü
sbit wei4 = P2^7;//¶¨ÒåµÚËÄλÊýÂë¹Ü
/*------------------------------------------------
È«¾Ö±äÁ¿ÉùÃ÷
------------------------------------------------*/
unsigned char code DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};// ÏÔʾ¶ÎÂëÖµ0~9
unsigned char irtime;//ºìÍâÓÃÈ«¾Ö±äÁ¿
bit irpro_ok,irok;
unsigned char IRcord[4];
unsigned char irdata[33];
/*------------------------------------------------
º¯ÊýÉùÃ÷
------------------------------------------------*/
void Ir_work(void);
void Ircordpro(void);
/*------------------------------------------------
¶¨Ê±Æ÷0Öжϴ¦Àí
------------------------------------------------*/
void tim0_isr (void) interrupt 1 using 1
{
irtime++; //ÓÃÓÚ¼ÆÊý2¸öϽµÑØÖ®¼äµÄʱ¼ä
}
/*------------------------------------------------
ÍⲿÖжÏ0Öжϴ¦Àí
------------------------------------------------*/
void EX0_ISR (void) interrupt 0 //ÍⲿÖжÏ0·þÎñº¯Êý
{
static unsigned char i; //½ÓÊÕºìÍâÐźŴ¦Àí
static bit startflag; //ÊÇ·ñ¿ªÊ¼´¦Àí±ê־λ
if(startflag)
{
if(irtime<63&&irtime>=33)//Òýµ¼Âë TC9012µÄÍ·Â룬9ms+4.5ms
i=0;
irdata[i]=irtime;//´æ´¢Ã¿¸öµçƽµÄ³ÖÐøʱ¼ä£¬ÓÃÓÚÒÔºóÅжÏÊÇ0»¹ÊÇ1
irtime=0;
i++;
if(i==33)
{
irok=1;
i=0;
}
}
else
{
irtime=0;
startflag=1;
}
}
/*------------------------------------------------
¶¨Ê±Æ÷0³õʼ»¯
------------------------------------------------*/
void TIM0init(void)//¶¨Ê±Æ÷0³õʼ»¯
{
TMOD=0x02;//¶¨Ê±Æ÷0¹¤×÷·½Ê½2£¬TH0ÊÇÖØ×°Öµ£¬TL0ÊdzõÖµ
TH0=0x00; //ÖØÔØÖµ
TL0=0x00; //³õʼ»¯Öµ
ET0=1; //¿ªÖжÏ
TR0=1;
}
/*------------------------------------------------
ÍⲿÖжÏ0³õʼ»¯
------------------------------------------------*/
void EX0init(void)
{
IT0 = 1; //Ö¸¶¨ÍⲿÖжÏ0ϽµÑØ´¥·¢£¬INT0 (P3.2)
EX0 = 1; //ʹÄÜÍⲿÖжÏ
EA = 1; //¿ª×ÜÖжÏ
}
/*------------------------------------------------
¼üÖµ´¦Àí
------------------------------------------------*/
void Ir_work(void)//ºìÍâ¼üֵɢת³ÌÐò
{
switch(IRcord[2])//ÅжϵÚÈý¸öÊýÂëÖµ
{
case 0x0c:DataPort=DuanMa[1];break;//1 ÏÔʾÏàÓ¦µÄ°´¼üÖµ
case 0x18:DataPort=DuanMa[2];break;//2
case 0x5e:DataPort=DuanMa[3];break;//3
case 0x08:DataPort=DuanMa[4];break;//4
case 0x1c:DataPort=DuanMa[5];break;//5
case 0x5a:DataPort=DuanMa[6];break;//6
case 0x42:DataPort=DuanMa[7];break;//7
case 0x52:DataPort=DuanMa[8];break;//8
case 0x4a:DataPort=DuanMa[9];break;//9
default:break;
}
irpro_ok=0;//´¦ÀíÍê³É±êÖ¾
}
/*------------------------------------------------
ºìÍâÂëÖµ´¦Àí
------------------------------------------------*/
void Ircordpro(void)//ºìÍâÂëÖµ´¦Àíº¯Êý
{
unsigned char i, j, k;
unsigned char cord,value;
k=1;
for(i=0;i<4;i++) //´¦Àí4¸ö×Ö½Ú
{
for(j=1;j<=8;j++) //´¦Àí1¸ö×Ö½Ú8λ
{
cord=irdata[k];
if(cord>7)//´óÓÚijֵΪ1£¬Õâ¸öºÍ¾§ÕñÓоø¶Ô¹Øϵ£¬ÕâÀïʹÓÃ12M¼ÆË㣬´ËÖµ¿ÉÒÔÓÐÒ»¶¨Îó²î
value|=0x80;
if(j<8)
{
value>>=1;
}
k++;
}
IRcord[i]=value;
value=0;
}
irpro_ok=1;//´¦ÀíÍê±Ï±ê־λÖÃ1
}
/*------------------------------------------------
Ö÷º¯Êý
------------------------------------------------*/
void main(void)
{
EX0init(); //³õʼ»¯ÍⲿÖжÏ
TIM0init();//³õʼ»¯¶¨Ê±Æ÷
wei1 = 1; //µÚһλÊýÂë¹Üѡͨ
wei2 = 1; //µÚ¶þλÊýÂë¹Üѡͨ
wei3 = 1; //µÚÈýλÊýÂë¹Üѡͨ
wei4 = 1;//µÚËÄλÊýÂë¹Üѡͨ
while(1)//Ö÷Ñ»·
{
if(irok) //Èç¹û½ÓÊÕºÃÁ˽øÐкìÍâ´¦Àí
{
Ircordpro();
irok=0;
}
if(irpro_ok) //Èç¹û´¦ÀíºÃºó½øÐй¤×÷´¦Àí£¬Èç°´¶ÔÓ¦µÄ°´¼üºóÏÔʾ¶ÔÓ¦µÄÊý×ÖµÈ
{
Ir_work();
}
}
}
静态局部变量使用static修饰符定义,即使在声明时未赋初值,编译器也会把它初始化为0。且静态局部变量存储于进程的全局数据区,即使函数返回,它的值也会保持不变。