
楼主又是我,这回我带着程序来的,请楼主教教我。我觉得数组的方式每次都要清一下数据才能不影响下一个遥控按键的控制,而且其实只有Ir_Buf[3]才是有用的,前面两个都是光占空间没用。程序是我按照教程上的改的也没有完全理解,我c语言基础太差。。。
楼主帮帮我啊!
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
//LED
sbit P1_0 = P1^0;
sbit P1_1 = P1^1;
sbit P1_2 = P1^2;
sbit P1_3 = P1^3;
sbit P1_4 = P1^4;
sbit P1_5 = P1^5;
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;
//BUZZ
sbit buzz = P3^6;
//IR
sbit Ir_Pin = P3^3;
uint8 Ir_Buf[4]={0xff,0xff,0xff,0xff}; //用于保存解码结果
void Delay(uint16 n)
{
while(n--);
}
void noise_buzz()
{
buzz = ~buzz;
Delay(5000);
buzz = ~buzz;
}
void display()
{
if(0xE9==Ir_Buf[3])
{
P1_0 =~P1_0;
Ir_Buf[3]=0xff;
noise_buzz();
}
if(0xF2==Ir_Buf[3])
{
P1_1 =~P1_1;
Ir_Buf[3]=0xff;
noise_buzz();
}
if(0xE6==Ir_Buf[3])
{
P1_2 = ~P1_2;
Ir_Buf[3]=0xff;
noise_buzz();
}
}
//外部中断初始化
void int1_init(void)
{
IT1 = 1; //下降沿有效
EX1 = 1;
EA = 1;
}
// 获取低电平时间
unsigned int Ir_Get_Low()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (!Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
// 获取高电平时间
unsigned int Ir_Get_High()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
main()
{
int1_init();
P1_0 = 1;
P1_1 = 1;
P1_2 = 1;
while (1)
{
display();
}
}
/****************************************
****************************************/
void int1_isr() interrupt 2
{
unsigned int temp;
char i,j;
temp = Ir_Get_Low();
if ((temp < 7833) || (temp > 8755)) //引导脉冲低电平8500~9500us
return;
temp = Ir_Get_High();
if ((temp < 3686) || (temp > 4608)) //引导脉冲高电平4000~5000us
return;
for (i=0; i<4; i++) //4个字节
{
for (j=0; j<8; j++) //每个字节8位
{
temp = Ir_Get_Low();
if ((temp < 184) || (temp > 737)) //200~800us
return;
temp = Ir_Get_High();
if ((temp < 184) || (temp > 1843)) //200~2000us
return;
Ir_Buf[i] >>= 1;
if (temp > 1032) //1120us
Ir_Buf[i] |= 0x80;
}
}
}