本帖最后由 czw07714 于 2012-10-4 11:40 编辑
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar num;
uchar dat[4];
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit dula = P2^6;
sbit wela = P2^7;
sbit int0 = P3^2;
void irq_init()
{
//外部中断
// EX0 = 1;
// EA = 1;
// IT0 = 1;
IE = 0x81; //允许总中断中断,使能 INT0 外部中断
TCON = 0x01; //触发方式为脉冲负边沿触发
//定时器中断
TMOD = 0X01;
TH0 = 0;
TL0 = 0;
}
void delayms(unsigned int ms)
{
unsigned char k;
while (ms--)
{
for (k = 0; k < 114; k++)
;
}
}
uint cnt_
time()
{
uint bit_time;
bit_time = TH0 * 256 + TL0;
TH0 = 0;
TL0 = 0;
return bit_time;
}
void display()
{
dula=0;
P0 = (table[num]); //个位
dula=1;
dula=0;
wela=0;
P0=0xfd;
wela=1;
wela=0;
delayms(1);
dula=0;
P0 = (table[num]); //十位
dula=1;
dula=0;
wela=0;
P0=0xfe;
wela=1;
wela=0;
delayms(1);
}
void main()
{
int0 = 1;
display();
irq_init();
while(1)
{
display();
if(1)//dat[2]==~dat[3])
{
switch(dat[2])
{
case 0x45: case 0x46: case 0x47:
num = 1; break;
case 0x44: case 0x40: case 0x43:
num = 2; break;
case 0x07: case 0x15: case 0xd9:
num = 3; break;
case 0x16: case 0x19: case 0x0d:
num = 4; break;
case 0x0c: case 0x18: case 0x5e:
num = 5; break;
case 0x08: case 0x1c: case 0x5a:
num = 6; break;
case 0x42: case 0x52: case 0x4a:
num = 7; break;
}
}
}
}
void extra0() interrupt 0
{
uint c_t;
uchar i,j;
EX0 = 0;
TR0 = 1;
if (int0 == 1)
//再确认IR信号是否出现
{
EX0 = 1; //开INT0中断
return ; //退出
}
while(!int0);
c_t = cnt_time();
if(c_t>8250&&c_t<10000) //检测是不是9ms的引导信号
{ dat[2] = 0x18; //为什么我不按遥控器都能运行到这里,实在不懂啊
while(int0);
c_t = cnt_time();
if(c_t>4000&&c_t<5000 ) //检测是不是4.5 ms的结果码
{
// dat[2] = 0x42;
for( i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
while(!int0);
while(int0);
dat
*=2;
if(cnt_time()>1500) //检测是否为‘1’有时不按遥控器都能运行到这里,搞不懂额 !!!
dat+=1;
}
}
}
}
EX0 = 1;
}
一周热门 更多>