📄 ir_decode.c
字号:
/************************************************
ir_decode.c
遥控解码子程序
包含ir_decode.h
需在主程序中设置T2CON = 0x0d;//T2工作在16位捕获方式并开始计时
需要几个全局变量
************************************************/
#include "stc89c51rc.h"
//#include "intrins.h"
#include "ir_decode.h"
unsigned char ir_code;
unsigned char ir_systemcode;
unsigned char ir_repeat_num;
bit ir_repeat = 0;
bit ir_enable = 0;
static unsigned char ir_count = 0;
static unsigned int ir_pulse_width;
static union ir_code//联合在这个时候有用了
{
unsigned char data8[4];
unsigned long int data32;
}ir;
/***********************************************
功能:T2遥控捕获
描述:定时器2的中断服务程序,用做红外遥控捕获解码
参数:无
返回:无
***********************************************/
void timer2_int() interrupt 5 using 3
{
TH2 = 0;
TL2 = 0;//清零,让其重新计时
if(EXF2)
{
EXF2 = 0;//time2需要软件清除中断标志位
ir_pulse_width = RCAP2L + RCAP2H * 256;
if(ir_count > 32)
{
ir_count = 0;
return; //遥控第一次下降沿中断
}
//////////////////////////////////////////////////
if(ir_count == 0)
{
if((ir_pulse_width > (13500-RANGE)) && (ir_pulse_width < (13500+RANGE)))
{
// ir_repeat = 0;//连续码
ir_count++;
return;//成功测到红外头,加1退出中断
}
if((ir_pulse_width > (11250-RANGE)) && (ir_pulse_width < (11250+RANGE)))
{
ir_repeat = 1;//连续码
ir_succeed();
return;
}
ir_count = 33;
return;//没有测到正确红外头,清0退出中断
}
//////////////////////////////////////////////////
else
{
if((ir_pulse_width > (1125-RANGE)) && (ir_pulse_width < (1125+RANGE)))
{
ir.data32 >>= 1;
if(ir_count == 32)ir_succeed();
ir_count++;
return;//读到0退出中断
}
if((ir_pulse_width > (2250-RANGE)) && (ir_pulse_width < (2250+RANGE)))
{
ir.data32 >>= 1;
ir.data32 |= 0x80000000;
if(ir_count == 32)ir_succeed();
ir_count++;
return;//读到1退出中断
}
ir_count = 33;
return;//错误,清0退出中断
}
}
if(TF2)
{
TF2 = 0;//time2需要软件清除中断标志位
ir_count = 33;
return;
}
}
/***********************************************
功能:T2遥控捕获
描述:
参数:无
返回:无
***********************************************/
void ir_succeed()
{
if(1)//(IR_SYSCODE1 == *(ir.data8 + 3)) && (IR_SYSCODE2 == *(ir.data8 + 2)))
{
if(*ir.data8 == ~(*(ir.data8 + 1)))
{
ir_code = *(ir.data8 + 1);
ir_systemcode = *(ir.data8 + 3);
ir_enable = 1;
if(ir_repeat)ir_repeat = 0;
else ir_repeat_num = 0;//连续码计数清0
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -