红外解码求助!高手请进!:程序如下:
/**************************************************
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---”
全局变量在中断里改变后主程序不能接到吗?请高手指点。