51单片机红外线接收程序
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit IR=P3^2;
sbit led1=P3^5;
sbit led2=P3^6;
sbit led3=P3^7;
uchar a[4];
uint i;
void ext0() interrupt 0
{
uint count;
TH1=(65536-4500)/256;
TL1=(65536-4500)%256;
while(!IR); //判断是否过了4.5ms的低电平
for(i=0;i<4;i++)
{
for(count=0;count<8;count++)
{
while(!IR); //根据高电平时间判断0和1 等待高电平的到来
TH1=(65536-800)/256l; //延时0.8ms比较准确
TL1=(65536-800)%256;
if(IR)
{
a[i]=(a[i]<<1)+1;
TH1=(65536-1000)/256;
TL0=(65536-1000)%256;
}
else
{
a[i]=a[i]<<1;
}
}
}
}
void main()
{
IT0=1;
EX0=1;
EA=1;
if(a[2]==0x16||a[0]==0x16||a[1]==0x16||a[3]==0x16)
{
led1=0;
led2=0;
led3=0;
}
}
遥控器其中有一位的编码是0x16
#define uint unsigned int
#define uchar unsigned char
sbit IR=P3^2;
sbit led1=P3^5;
sbit led2=P3^6;
sbit led3=P3^7;
uchar a[4];
uint i;
void ext0() interrupt 0
{
uint count;
TH1=(65536-4500)/256;
TL1=(65536-4500)%256;
while(!IR); //判断是否过了4.5ms的低电平
for(i=0;i<4;i++)
{
for(count=0;count<8;count++)
{
while(!IR); //根据高电平时间判断0和1 等待高电平的到来
TH1=(65536-800)/256l; //延时0.8ms比较准确
TL1=(65536-800)%256;
if(IR)
{
a[i]=(a[i]<<1)+1;
TH1=(65536-1000)/256;
TL0=(65536-1000)%256;
}
else
{
a[i]=a[i]<<1;
}
}
}
}
void main()
{
IT0=1;
EX0=1;
EA=1;
if(a[2]==0x16||a[0]==0x16||a[1]==0x16||a[3]==0x16)
{
led1=0;
led2=0;
led3=0;
}
}
遥控器其中有一位的编码是0x16
编译试试。