C語言寫的紅外遙控解碼程序
#include "STC89C51RC.H"
sbit DQ = P4^3;
sbit SPK = P1^0;
#include absacc.h>
#define seg1 XBYTE[0xffec]
#define seg2 XBYTE[0xffed]
#define seg3 XBYTE[0xffee]
#define seg4 XBYTE[0xffef]
unsigned char IRMcode[4];
unsigned char code ledmap[]=//共陰LED數(shù)碼管譯碼表
{
0x3f,0x06,0x5b,0x4f,0x66,//0,1,2,3,4,
0x6d,0x7d,0x07,0x7f,0x6f,//5,6,7,8,9,
0x77,0x7C,0x39,0x5E,0x79,//A,b,C,d,E,
0x71,//F,
};
void delay(unsigned int);
void main()
{
EA = 1;
EX2 = 1;
while(1)
{
if (IRMcode[2]==~IRMcode[3]){
seg1 = ~ledmap[IRMcode[2]/100];
seg2 = ~ledmap[IRMcode[2]/10%10];
seg3 = ~ledmap[IRMcode[2]%10];
seg4 = 0xff;//熄滅
}
}
}
void Sound(void)
{
SPK = 0;
delay(10000);
SPK = 1;
}
void IRMint(void) interrupt 6 using 2
{
unsigned char count;
unsigned int count1;
unsigned char i,j;
EA = 0;
count = 9;
while(--count)
{
delay(100);//440us
if(DQ){EA = 1;return;}
}
while(DQ==0);//通過引導碼余下的低電平
//以上完成引導碼4.5ms的低電平檢測
count = 9;
count1 = 0;
while(--count)
{
delay(100);//440us
if(DQ==0){EA = 1;return;}
}
while(DQ==1)//通過引導碼余下的高電平
{
if(count1++>1000) {
P1=0x55;EA = 1;return;}
}
//以上完成引導碼4.5ms的高電平檢測
count1 = 0;
for(j=0;j4;j++)//有效字節(jié)數(shù)設定
{
for(i=0;i8;i++)//字節(jié)bit數(shù)
{
while(DQ==0);//通過編碼低電平
delay(200);//延時判斷編碼高電平維持長度
IRMcode[j]=1;
if(DQ)IRMcode[j]|=0x01;
while(DQ==1){//通過編碼余下的高電平
if(count1++>2000){EA = 1;return;}
}
}
}
Sound();
EA = 1;
}
//通用延時函數(shù)
//22.1184,10 = 48.4 us (5us+43.4)
//22.1184,100 = 439 us (43.4*10+5)
//22.1184,1000 = 4345 us(43.4*100+5)
//22.1184,10000 = 43450 us(43.45*1000+5)
void delay(unsigned int t)
{
while(--t);
}
c語言相關文章:c語言教程
評論