基于51單片機的紅外線遙控程序編寫代碼
#define uchar unsigned char
#define uint unsigned int
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};
uchar irtime;
uchar startflag;
uchar irdata[33];
uchar bitnum;
uchar irreceok;
uchar ircode[4];
uchar irprosok;
uchar disp[8];
//************************************************
//延時函數(shù),在12MHz的晶振頻率下
//大約50us的延時
//************************************************
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
//************************************************
//延時函數(shù),在12MHz的晶振頻率下
//大約50ms的延時
//************************************************
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void timer0init(void)
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
EA=1;
TR0=1;
}
void int0init(void)
{
IT0=1;
EX0=1;
EA=1;
}
void irwork(void)
{
disp[0]=ircode[0]/16;
disp[1]=ircode[0]%16;
disp[2]=ircode[1]/16;
disp[3]=ircode[1]%16;
disp[4]=ircode[2]/16;
disp[5]=ircode[2]%16;
disp[6]=ircode[3]/16;
disp[7]=ircode[3]%16;
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
P1=smg_du[disp[i]];
P2=smg_we[i];
delay_50us(20);
}
}
void irpros(void) //0x1c
{
uchar k,i,j;
uchar value;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
value=value>>1; //7
if(irdata[k]>6) //8
{
value=value | 0x80;
}
k++;
}
ircode[j]=value;
}
irprosok=1;
}
void main()
{
timer0init();
int0init();
while(1)
{
if(irreceok)
{
irpros();
irreceok=0;
}
if(irprosok)
{
irwork();
irprosok=0;
}
display();
}
}
void timer0 () interrupt 1
{
irtime++; //255
}
void int0 () interrupt 0
{
if(startflag)
{
if(irtime>32) //檢測引導(dǎo)碼
{
bitnum=0;
}
irdata[bitnum]=irtime;
irtime=0;
bitnum++;
if(bitnum==33)
{
bitnum=0;
irreceok=1;
}
}
else
{
startflag=1;
irtime=0;
}
}
評論