#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={ //段选数组定义
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code aable[]={ //位选数组定义
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar irtimer;
uchar irdata[33]; //接收红外发送的数据
uchar bitnum;
uchar ircode[4];
uchar disp[8];
uchar duoji_timer;
uchar state = 0;
sbit PWM1 = P2^0;
sbit K1=P2^7;
sbit K2=P2^6;
sbit K3=P2^5;
bit IRstart_Flag; //红外发送标志位
bit IR_rec_ok_Flag; //接收红外发送的数据完毕标志位
bit IR_code_ok_Flag; //数据解码完成标志位
bit turn_flag;
void delay(uint t)
{
uchar i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void INT0_Init() //外部中断0初始化
{
IT0 = 1; //下降沿触发外部中断0
EX0 = 1; //打开外部中断0
EA = 1; //打开总中断
}
void Timer0_Init() //定时器0初始化
{
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
}
void Timer1_Init() //定时器1初始化
{
TMOD = 0x10;
TH1 = 0xff;
TL1 = 0x1a; //0.25ms
EA = 1;
ET1 = 1;
TR1 = 1;
}
void irdata_pros() //红外数据判断处理函数,逻辑“0” OR 逻辑“1”
{
uchar i,j,n;
uchar value;
n = 1;
for(j=0;j1;
if(irdata[n] > 6) //判断是逻辑“0” OR 逻辑“1”,根据两者间的中间值6来判断
{
value |= 0x80;
}
n++;
}
ircode[j] = value;
}
IR_code_ok_Flag = 1;
}
void main()
{
while(1)
{
INT0_Init();
Timer0_Init();
if(IR_rec_ok_Flag == 1)
{
irdata_pros();
IR_rec_ok_Flag = 0;
}
if(IR_code_ok_Flag == 1)
{
EA = 0;
IR_code_ok_Flag = 0;
}
Timer1_Init();
if (K3==0)
{
delay(2);
state = -1;
}
if (K2==0 )
{
delay(2);
state = 0 ;
}
if (K1==0 )
{
delay(2);
state = 1 ;
}
}
}
void INT0_ISP() interrupt 0 //外部中断0服务函数
{
if(IRstart_Flag) //红外遥控开始发送
{
if(irtimer > 30) // 9ms/277.8us = 32 开头的引导码
{
bitnum = 0;
}
irdata[bitnum] = irtimer;//接收红外发送的数据
irtimer = 0;
bitnum++;
if(bitnum==33) //接收红外发送的数据完毕
{
EA = 0;
bitnum = 0;
IR_rec_ok_Flag = 1;
}
}
else
{
IRstart_Flag = 1;
irtimer = 0;
}
}
void Timer0_ISP()interrupt 1 //定时器0服务函数
{
irtimer++; //每隔(256*12/11.0592)加一 277.8us
}
void Timer1_ISP() interrupt 3 //定时器1服务函数
{
TR1 = 0;
TH1 = 0xff;
TL1 = 0x1A; //0.25ms
TR1 =1;
duoji_timer++;
switch(state)
{
case -1 :
{
if(duoji_timer