unsigned char remote_temp,P1_state,P3_state;
unsigned char INT1_times,remote_time;
unsigned int remote_value_buff,remote_value,remote_value_first_buff;
bit remote_ok,INT1_ISR_flag,resend,remote_end_ok,hold_ok,within_3_second;
unsigned char remote_hold;
unsigned int hold_count;
void T1_T0_INT1_initial(void);
void Port_initial(void);
void key_scan(void);
void key_action(void);
void delay_10us(unsigned int t);
void remote_action(void);
void timer1_ISR(void) interrupt 3
{
TH1=0XFc; //1000US
TL1=0x18;
if (INT1_ISR_flag==1)
{
remote_time++; //遥控器定时时间,如果在第一个外部中断有下降沿,则开始计时,22MS后结束。
}
if (remote_time >=80)//80ms
{
INT1_times=0;
INT1_ISR_flag=0;
remote_value_first_buff=0;
remote_value=0;
remote_time=0;
resend=0;
}
if (remote_hold)
{
hold_count++;
if (hold_count>=1500 /*&& remote_hold>3*/)
{
if (remote_hold>2)
{
hold_ok=!hold_ok;
if (hold_ok==1)
{ P3_0=0;} //指示锁死遥控。
else P3_0=1;
hold_count=0;
remote_hold=0;
}// P1_0=!P1_0; //For testing.
else {hold_count=0;
remote_hold=0;}
}
}
else
{
hold_count=0;
within_3_second=0;
//hold_ok=0;
}
}
/********************For remote control***********************/
void timer0_ISR(void) interrupt 1 //800us
{
unsigned char bit_value;
bit_value=REMOTE_IN;
if (resend)
{
if (bit_value==1)
{ remote_value= remote_value<<1;}
if (bit_value==0)
{remote_value=(remote_value<<1)+0x0001;}
if (INT1_times>=24)
{
if (remote_value_first_buff==remote_value)
{
remote_value_buff=remote_value;
resend=0;
remote_ok=1; // 遥控有效
}
}
}
else
{
if (bit_value==1)
{ remote_value= remote_value<<1;}
if (bit_value==0)
{remote_value=(remote_value<<1)+0x0001;}
if (INT1_times>=12)
{
resend=1;
remote_value_first_buff=remote_value;//第一次接收完毕. 置标志。
remote_value=0;
}
}
TR0=0;
}
/************************************************************/
void INT1_ISR(void) interrupt 2
{
TH0=0Xfc; //800us
TL0=0Xe0;
TR0=1;// TR0=1; //Start timer0 for testing pulse level,800us each timer0 interrupt
INT1_times++;
if (INT1_times==1)
{INT1_ISR_flag=1;}
}
/*===================================================*/
void main(void)
{
Port_initial();
T1_T0_INT1_initial();
LED_channel_3;
EA=1;
ET1=1;
ET0=1;
TR1=1; //Start Timer1
EX1=1;
TR0=0;
CD4052_INH=0;
P1_state=(P1|0X0F); //保存LED的状态
P3_state=(P3|0xcf);
hold_count=0;
while (1)
{
key_scan();
key_action();
remote_action();
//delay_10us(1); //1ms 100
}
}
/***********************************************************
Timer1 was used for bianma switch control ,each 1000us interrupt,
Timer0 was used for remote control ,each 800us interrupt,
***********************************************************/
void T1_T0_INT1_initial(void)
{
TMOD=0X11; //Timer1 mode1. 16bit timer.
TCON=0x04; //enable timer1 interrupt.TF1=1;TR1=0;Enable INT1 fall-edge interrupt.
TH1=0XFc; //1000us-->0xfc18
TL1=0x18;
TH0=0Xfc; //800us-->0xfce0,12MHz Crystal
TL0=0Xe0; //18.342 800us
}
/************************************************************
PORT端口初始化
***********************************************************/
void Port_initial(void)
{
P1M0=0X00;//全部为标准I/O口
P1M1=0X00;
P3M0=0x00; //P3。3设为输入。
P3M1=0x00;
P1=0XFF;
P3=0Xff;
}
/***********************************************************
key_scan();
***********************************************************/
void key_scan(void)
{
if ((P1|0xf0)!=0xff) //有键
{
key_temp=P1|0xf0;
if (key_temp==key_old)
{
key_delay++;
}
else key_old=key_temp;
}
else
{
if (key_delay >=60)
{
key_ok=1;
key_delay=0;
// return key_temp;//返回键值
}
}
}
/***********************************************************
key_action();
***********************************************************/
void key_action(void)
{
unsigned char temp;
if (key_ok) { temp=key_temp; goto action;}
if (remote_end_ok==1 && hold_ok==0)
{ remote_end_ok=0;temp=remote_temp; }
else {temp=0;remote_end_ok=0;}
action: switch (temp)
{
case channel_0:
LED_channel_0;//执行按键功能1
CD4052_A=0;
CD4052_B=0;
P1_state=(P1|0X0F); //保存LED的状态
P3_state=(P3|0xcf);
key_ok=0;
temp=0;
break;
case channel_1:
LED_channel_1;//执行按键功能2
CD4052_A=1;
CD4052_B=0;
P1_state=(P1|0X0F); //保存LED的状态
P3_state=(P3|0xcf);
key_ok=0;
temp=0;
break;
case channel_2: LED_channel_2;//执行按键功能3
CD4052_A=0;
CD4052_B=1;
P1_state=(P1|0X0F); //保存LED的状态
P3_state=(P3|0xcf);
key_ok=0;
temp=0;
break;
case channel_3: LED_channel_3;//执行按键功能4
CD4052_A=1;
CD4052_B=1;
P1_state=(P1|0X0F); //保存LED的状态
P3_state=(P3|0xcf);
key_ok=0;
temp=0;
break;
case mute:
remote_end_ok=0;
temp=0;
break;
default: remote_end_ok=0;
temp=0;
break;
}
}
/*********************************************************
remote_action。
*********************************************************/
void remote_action(void)
{
if (remote_ok)
{
remote_ok=0;
remote_end_ok=1;
switch (remote_value_buff)
{
case IR_TREBLE: remote_temp=channel_3; break; //channel_3;
case IR_2_1: remote_temp=channel_0; break; //channel_0;
case IR_5_1: remote_temp=channel_1; break; //channel_1;
case IR_AC3: remote_temp=channel_2; break; //channel_2;
case IR_MUTE: remote_temp=mute;
remote_hold++; //每次按键有效,则remote_hold 加1
break;
default: remote_value_buff=0;break;
}
}
}