📄 irm.c
字号:
#include "irm.h"
unsigned char irm_code;//红外解码键值返回全局变量
bit flag ;// 上一位编码位
void int2_init(void)
{
EA = 1;
IT2 = 1; //下降沿中断
EX2 = 1;
}
static unsigned char check_bit(unsigned char bit_num)
{
//最大检测7位
unsigned char irm_code0 = 0;
unsigned char i ;
unsigned int count ;
#define ERROR 0x10;
for(i=0;i<bit_num;i++)
{
count = 0 ;
irm_code0 = irm_code0<<1 ;
if(flag)
{
while(IRM_DQ)if(count++>10000) return ERROR;
if(count>150)
{
flag = 0 ;// 结束码为 10
}
else
{
flag = 1 ;// 结束码为 01
irm_code0 |= 0x01;
while(!IRM_DQ)if(count++>10000) return ERROR;
}
}
else // 结束码为 10
{
while(!IRM_DQ)if(count++>10000) return ERROR;
if(count>150)
{
flag = 1 ;// 结束码为 01
irm_code0 |= 0x01;
}
else
{
flag = 0 ;// 结束码为 10
while(IRM_DQ)if(count++>10000) return ERROR;
}
}
}
return irm_code0 ; //解码成功 !!
}
//STC单片机特有的外部中断INT2实现红外解码
//解码结果由数据缓冲区返回
void IRMint(void) interrupt 6
{
unsigned int count;
unsigned char temp_code[2];
EA = 0;/*解码过程禁止其它中断发生*/
IRM_DQ = 1 ;
{/*跳过1.5bit 起始码*/
count = 0;
while(!IRM_DQ)if(count++>1000) goto return_main;
count = 0;
while(IRM_DQ)if(count++>1000) goto return_main;
}
{/*跳过1bit控制码和5bit用户码*/
flag = 0 ; //起始码以10结束
temp_code[0] = check_bit(6);
}
/*数据码*/
temp_code[1] = check_bit(6);
irm_code = temp_code[1] ; //返回键值
return_main:
IE2 = 0 ;//清除中断标志位
EA = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -