程序如下,51单片机用int0口,不知为什么,总调不好,求高手不吝指教!
#include"reg51.H"
unsigned char IRCommandData = 0;
unsigned char INTTime = 0;
bit IRInt;
bit IRok = 0;
sbit BUZZER = P3^7;
sbit P2_7 = P2^7; //十位数码管选通
sbit P2_6 = P2^6; //个位数码管选通
unsigned char code Tab[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //共阳极数码管
void Delay()
{
unsigned char i,j;
for(i = 0; i < 255; i++)
for (j = 0; j < 5;j++)
;
}
void ISRInt(void) interrupt 0 using 1 // HS0038用中断0
{
EX0 = 0; // Disable INT 0
TR0 = 0; // Disable Timer 0
IRInt = 1;
INTTime = TH0;
TH0 = 0xB0;
TL0 = 0;
TR0 = 1; // Enable Timer 0
EX0 = 1; // Enable INT 0
BUZZER = 0;
BUZZER = 1;
}
bit IRDecode(void)
{
static unsigned char INTCount = 0;
static unsigned long IRData = 0;
unsigned char IRData1, IRData2, IRData3, IRData4;
if (IRInt)
{
IRInt = 0;
if((INTTime > 0xDE) && (INTTime < 0xE1)) // 0xDFE2 = 13.5 ms
{
IRData = 0;
INTCount = 0;
}
else if ((INTTime > 0xB6) && (INTTime < 0xB9)) // 0xB7FB = 2.25 ms(data = 1)
{
IRData = (IRData << 1) + 1;
INTCount++;
}
else if ((INTTime > 0xB2) && (INTTime < 0xB5)) // 0xB3FD = 1.125 ms(data = 0)
{
IRData = IRData << 1;
INTCount++;
}
else if ((INTTime > 0xD6) && (INTTime < 0xD9)) // 0xD7E7 = 11.25 ms
{
IRData = 0;
IRCommandData = 0xFF; //repeat code flag
return 1;
}
else
{
INTCount = 33;
IRData = 0;
}
if (INTCount == 32)
{
IRData1 = (unsigned char)(IRData >> 24);
IRData2 = (unsigned char)(IRData >> 16);
IRData3 = (unsigned char)(IRData >> 8);
IRData4 = (unsigned char)(IRData);
IRData = 0;
if (IRData4 == ~IRData3)
{
IRCommandData = IRData3; //还原命令成功
return 1;
}
else
return 0;
}
else
return 0;
}
return 0;
}
void Display(unsigned char DisplayData)
{
P2_7 = 0;
P2_6 = 1;
P0 = Tab[DisplayData & 0x0f];
Delay();
P2_7 = 1;
P2_6 = 0;
P0 =Tab[DisplayData >> 4];
Delay();
}
void main()
{
unsigned char DisplayData = 0;
IRInt = 0;
EA = 0;
TR0 = 0;
TH0 = 0XB0;
TL0 = 0;
ET0 = 1;
TMOD = 0X01; //IT0 工作方式1
TCON = 0X01; //INT0 边沿触发
EX0 =1;
EA = 1;
while(1)
{
IRok = IRDecode();
if((IRok==1)&(IRCommandData!=0xff))
{
DisplayData = IRCommandData;
}
Display(DisplayData);
}
} |