?? 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 + -