#include
#include
#include "./delay/delay.h"
#include "./lcd1602/lcd1602.h"
unsigned int irtime = 0;
bit startflag = 0;
unsigned char irdata[33];
unsigned char ircood[4];
bit irok = 0;
sbit IR = P3^2;
void timer0()
{
EA = 1;
TMOD |= 0x02;
TH0 = 0;
TL0 = 0;
ET0 = 1;
TR0 = 1;
}
void int0()
{
EA = 1;
IT0 = 1;
EX0 = 1;
}
void int0_isr() interrupt 0
{
static unsigned char bitnum = 0;
if(1 == startflag)
{
if((irtime > 40) && (irtime < 60))
{
bitnum = 0;
}
irdata[bitnum] = irtime;
bitnum++;
irtime = 0;
if(33 == bitnum)
{
irok = 1;
bitnum = 0;
startflag = 0;
}
}
else
{
startflag = 1;
irtime = 0;
}
}
void timer0_isr() interrupt 1
{
irtime++;
}
unsigned char irprocess()
{
unsigned char i,j,k;
unsigned char temp;
k = 1;
for(j = 0; j < 4; j++)
{
for(i = 0; i < 8; i++)
{
temp >>= 1;
if(irdata[k] > 6)
{
temp += 0x80;
}
k++;
}
ircood[j] = temp;
}
if((ircood[0] + ircood[1] == 0xff) && (ircood[2] + ircood[3] == 0xff))
{
switch(ircood[2])
{
case 0x16: return 0;
case 0x0c: return 1;
case 0x18: return 2;
case 0x5e: return 3;
case 0x08: return 4;
case 0x1c: return 5;
case 0x5a: return 6;
case 0x42: return 7;
case 0x52: return 8;
case 0x4a: return 9;
case 0x45: return 10;
case 0x46: return 11;
case 0x47: return 12;
case 0x44: return 13;
case 0x40: return 14;
case 0x43: return 15;
case 0x07: return 16;
case 0x15: return 17;
case 0x09: return 18;
case 0x19: return 19;
case 0x0d: return 20;
default: break;
}
}
return 0xff;
}
void display()
{
unsigned char high,low;
unsigned char i;
for(i = 0; i < 4; i++)
{
high = ircood[i]/16;
low = ircood[i]%16;
if(high > 9)
{
high += 0x37;
}
else
{
high += 0x30;
}
if(low > 9)
{
low += 0x37;
}
else
{
low += 0x30;
}
lcd_print(1, 2*i+1, high);
lcd_print(1, 2*i + 2, low);
}
}
void irdisplay(unsigned char num)
{
unsigned char high,low;
high = num/10 +0x30;
low = num%10 + 0x30;
lcd_print(2, 1, high);
lcd_print(2, 2, low);
}
void main()
{
unsigned char keynum;
lcd_init();
timer0();
int0();
while(1)
{
if(1 == irok)
{
irok = 0;
keynum = irprocess();
display();
if(keynum != 0xff)
{
irdisplay(keynum);
}
}
}
}
单片机——红外遥控器——代码
最新推荐文章于 2024-10-07 23:50:35 发布