紅外解碼求助!高手請(qǐng)進(jìn)!:程序如下:
/**************************************************
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:測(cè)量引導(dǎo)碼長(zhǎng)度,2:顯示機(jī)器碼和鍵碼正反碼,3:顯示鍵值碼
sbit ir = P3^3; //外部中斷1
//定義一些燈,調(diào)試用。
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; //測(cè)量高低電平時(shí)間
uint common_code_time; //引導(dǎo)碼計(jì)數(shù)
uint common_high_time; //引導(dǎo)碼高電平計(jì)數(shù)
uint common_low_time; //引導(dǎo)碼低電平計(jì)數(shù)
uchar new_key; //是否有新的接收
uchar key_code; //鍵值的數(shù)值
uchar a[4]; //存放接收到的4個(gè)自己數(shù)據(jù):用戶碼 + 用戶碼 + 鍵碼 + 鍵碼反碼
void ired_init(void) //初始化
{
ir=1; //紅外端口寫1
TMOD=0x10; //定時(shí)器1,工作方式1,16位定時(shí)器
IT1=1; //INT1下降沿觸發(fā)
EX1=1; //允許外部中斷1
EX0 = 0;
// ET1=1; //允許定時(shí)器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:
{
//此段用于測(cè)量引導(dǎo)碼的高低電平,實(shí)際時(shí)間值等于數(shù)值*一個(gè)機(jī)器周期時(shí)長(zhǎng)。對(duì)于12M晶振來(lái)說(shuō)就是1us。
lcd_1602_clr();
lcd_1602_write_string_xy(1,1,"low_time:");
lcd_1602_write_uint(common_low_time); //觀察低電平時(shí)長(zhǎng)
lcd_1602_write_string_xy(2,1,"high_time:");
lcd_1602_write_uint(common_high_time); //觀察高電平時(shí)長(zhǎng)
break;
}
case 2:
{
//此段用于顯示兩個(gè)機(jī)器碼和數(shù)據(jù)碼的正反碼。
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; //給定時(shí)器裝初值,11.0592MHZ下機(jī)器周期是 12/11.0592 = 1.085us
TL1 = 0;
TR1 = 1;
while(ir == 0);
TR1 = 0;
low_time = (TH1 << 8) + TL1;//保存低電平時(shí)間
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(ir == 1);
TR1 = 0;
high_time = (TH1 << 8) + TL1;//保存低電平時(shí)間
common_low_time = low_time;
common_high_time = high_time;
common_code_time = common_low_time + common_high_time;
//P2 = common_high_time;
//在此顯示一下引導(dǎo)碼的長(zhǎng)度。
if((low_time > 7800) && (low_time < 8800) && (high_time > 3600) && (high_time < 4700)){ //判斷是否引導(dǎo)碼
uchar i,j;
uchar temp;
for(i = 0;i < 4;i ++){ //接收四個(gè)字節(jié)
for(j = 0;j < 8;j ++){ //每個(gè)字節(jié)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;
}
主程序就這些,現(xiàn)在P2的led燈能正常顯示碼值,但主程序就是接收不到new_key信號(hào),進(jìn)不了1602顯示環(huán)節(jié),1602只顯示“---IR DECODE---”
全局變量在中斷里改變后主程序不能接到嗎?請(qǐng)高手指點(diǎn)。