/******************************************************************/ /* 定时器0初始化 */ /* 定时值:225us */ /* 方式2 自动重载 */ /******************************************************************/ void Timer0init(void)//定时器0初始化 {
//定时值 225us
TMOD=0x12;//定时器0工作方式2 自动重载 TH0=TIMER0_COUNT; //重载值 TL0=TIMER0_COUNT; //初始化值 ET0=1; //定时器0中断有效 TR0=1; //定时开始 ET1=1; TR1=1; //
// RCAP2H =0xF3; //赋T2的预置值,溢出1次是1/1200秒钟 // RCAP2L =0x98;
// TR2=1; //启动定时器
// ET2=1; //打开定时器2中断 // EA=1; }
/******************************************************************/ /* 外部中断初始化 */ /* 中断端口:外部中断0 P3.2 */ /* 触发方式:下降沿 */ /******************************************************************/ void EX0init(void) {
EA = 1; //总中断开
IT0 = 1; //指定外部中断0下降沿触发,INT0 (P3.2) EX0 = 1; //外部中断有效 }
/******************************************************************/ /* Timer0中断子程序 */
/******************************************************************/ void Timer0(void) interrupt 1 using 1 {
Timer0_cnt++;
if(Timer0_cnt>Timeout_cnt) timeout=1; //超时标志 }
/******************************************************************/ /* 外部中断0函数 */ /* 下降沿触发 */ /******************************************************************/ void ex0_isr (void) interrupt 0 using 0 {
static unsigned char m=0;
Timer0_cnt_bk=Timer0_cnt; //备份时间计数值,即前一个下降沿到本下降沿的时间间隔 Timer0_cnt=0x00; //清空时间计数值
if(timeout) //如果超时 {
TL0=TIMER0_COUNT; //初始化定时器0 Timer0_cnt=0x00; //清空时间计数值 timeout=0; //清除超时标志 Guide=0; //清除引导标志 Continuous=0; //清除连发标志 m=0; //复位数据位 } else {
if(Guide|Continuous) //如果引导码有效 {
IR_time[m++]=Timer0_cnt_bk; //保存时间间隔 if(m==32) //接收够32数据后 {
m=0; Guide=0; //清除引导标志 Decode(); //解码 }; };
if(Timer0_cnt_bk>Guide_timer) //如果时间间隔>引导码时长 {
Guide=1; //使能引导标志 m=0; };
if(Timer0_cnt_bk>Continuous_timer) //如果时间间隔>引导码时长 {
Continuous=1; //使能引导标志
EX0=0;
/**此处加入连发操作代码**/ //display_Continuous();
EX0=1; m=0; };
};//end of 超时 }
/******************************************************************/ /* 解码子程序 */ /******************************************************************/
void Decode(void) {
unsigned char m,n,s=0x00;
static unsigned char *p; //指向结构体IR_data.XX的指针 EX0 = 0; //外部中断无效
p=&IR_data.addr1;
for(n=0;n<4;n++) {
for(m=8;m>0;m--) {
if((IR_time[s]>IR_L_timer_min)&(IR_time[s] *p>>=1; *p&=0x7f; } else if((IR_time[s]>IR_H_timer_min)&(IR_time[s] 低电平+1.69ms高电平 { *p>>=1; *p|=0x80; }; s++; }; p++; }; if(IR_data.data2==~IR_data.data1) //数据2=数据1反码 { if((IR_data.addr1==IR_addr1)&(IR_data.addr2==IR_addr2)) { _nop_(); //show_L = ~show_tab[IR_data.data1 & 0x0f]; //show_H = show_tab[(IR_data.data1 & 0xf0)>>4]; key_flg = 1; key_value = IR_data.data1; } _nop_(); } else { /**非本机遥控器或数据错误**/ //display_error(); }; EX0 = 1; //重新开放外部中断 } 解码正确
相关推荐: