找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

红外解码求助!高手请进!

[复制链接]
admin 发表于 2012-9-2 14:02:23 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
程序如下:
/**************************************************
11.0592MHZ 晶振
**************************************************/
#include <reg52.h>
#include "lcd_1602.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
#define TEST_MODE 1 //1:测量引导码长度,2:显示机器码和键码正反码,3:显示键值码
sbit ir = P3^3; //外部中断1
//定义一些灯,调试用。
sbit led1 = P0^0;
sbit led2 = P0^1;
sbit led3 = P0^2;
sbit led4 = P0^3;
sbit led5 = P0^4;
sbit led6 = P0^5;
sbit led7 = P0^6;
sbit led8 = P0^7;
uint low_time,high_time; //测量高低电平时间
uint common_code_time; //引导码计数
uint common_high_time; //引导码高电平计数
uint common_low_time; //引导码低电平计数
uchar new_key; //是否有新的接收
uchar key_code; //键值的数值
uchar a[4]; //存放接收到的4个自己数据:用户码 + 用户码 + 键码 + 键码反码
void ired_init(void) //初始化
{
ir=1; //红外端口写1

TMOD=0x10; //定时器1,工作方式1,16位定时器
IT1=1; //INT1下降沿触发
EX1=1; //允许外部中断1
EX0 = 0;
// ET1=1; //允许定时器1中断
EA=1; //开总中断
}
void main(void)
{
uchar i;
lcd_init();
ired_init();
lcd_1602_write_string_xy(1,1,"---IR DECODE---");
while(1)
{
while(!new_key);//没有新接收的键码,在此等待。
switch(TEST_MODE)
{
case 1:
{
//此段用于测量引导码的高低电平,实际时间值等于数值*一个机器周期时长。对于12M晶振来说就是1us。
lcd_1602_clr();
lcd_1602_write_string_xy(1,1,"low_time:");
lcd_1602_write_uint(common_low_time); //观察低电平时长
lcd_1602_write_string_xy(2,1,"high_time:");
lcd_1602_write_uint(common_high_time); //观察高电平时长
break;
}

case 2:
{
//此段用于显示两个机器码和数据码的正反码。
lcd_1602_clr();
lcd_1602_gotoxy(1,1);
lcd_1602_write_string("a0,a1,a2,a3");
lcd_1602_gotoxy(2,1);
for(i = 0;i < 4;i ++)
{
lcd_1602_write_uchar(a);
lcd_1602_write_data(',');
}
break;
}
case 3:
{
//用于显示键码
lcd_1602_clr();
lcd_1602_write_string_xy(1,1,"code_num:");
lcd_1602_write_uchar(key_code);
break;
}
default: lcd_1602_write_string_xy(2,1,"error");

}

new_key = 0;
}
}
void int1_serv() interrupt 2
{
EA = 0;
TH1 = 0; //给定时器装初值,11.0592MHZ下机器周期是 12/11.0592 = 1.085us
TL1 = 0;
TR1 = 1;
while(ir == 0);
TR1 = 0;
low_time = (TH1 << 8) + TL1;//保存低电平时间
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(ir == 1);
TR1 = 0;
high_time = (TH1 << 8) + TL1;//保存低电平时间
common_low_time = low_time;
common_high_time = high_time;
common_code_time = common_low_time + common_high_time;
//P2 = common_high_time;
//在此显示一下引导码的长度。
if((low_time > 7800) && (low_time < 8800) && (high_time > 3600) && (high_time < 4700)){ //判断是否引导码

uchar i,j;
uchar temp;
for(i = 0;i < 4;i ++){ //接收四个字节
for(j = 0;j < 8;j ++){ //每个字节8位
temp >>= 1;
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(ir == 0);
TR1 = 0;
low_time = (TH1 << 8) + TL1;
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(ir == 1);
TR1 = 0;
high_time = (TH1 << 8) + TL1;
if((high_time > 420) && (high_time < 620)){ //0
temp &= 0x7f;
}
if((high_time > 1300) && (high_time < 1800)){ //1
temp |= 0x80;
}

}
a = temp;
}
if(a[2] == ~a[3]){
P2 = a[3];
key_code = a[2];
new_key = 1;
}
}
EA = 1;
}

主程序就这些,现在P2的led灯能正常显示码值,但主程序就是接收不到new_key信号,进不了1602显示环节,1602只显示“---IR DECODE---”
全局变量在中断里改变后主程序不能接到吗?请高手指点。


ir_decode_test.rar (6.06 KB, 下载次数: 3)
meiguo9999 发表于 2013-3-9 13:02:00 | 显示全部楼层
下载了,学习学习
zwpkzw 发表于 2014-7-7 11:19:36 来自手机 | 显示全部楼层
不知道回复有没有m
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-12-23 16:02 , Processed in 0.063002 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表