📄 红外遥控.c
字号:
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit hw=P3^2;
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
const uchar table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar hwx[66];
uint num1=50;
uchar date[4];
void delay1(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void delay(void) //红外解码用的延时
{
unsigned char i=13;
while(i)
i--;
}
void HC138(uchar i)
{
a=i&0x01;
b=i&0x02;
c=i&0x04;
}
void display(uchar flag,uchar num)
{
uchar ge,shi;
shi=num/16;
ge=num%16;
P2=table[ge];
HC138(flag);
delay1(2);
P2=table[shi];
HC138(flag+1);
delay1(2);
}
void main()
{
// uchar i;
EA=1;
EX0=1;
IE0=1;
while(1)
{
display(0,date[3]);
display(2,date[2]);
display(4,date[1]);
display(6,date[0]);
}
}
void hongwai() interrupt 0
{
uchar i,j,temp;
uint temp2;
EX0=0;
j=33;
i=0;
temp2=0;
while(!hw) //检测是否有信号
{
temp2++;
if(temp2==1000)
break;
}
if(temp2<1000) //如果信号时间小于1000,则为干扰信号,重新检查
{
EX0=1;
return;
}
while(j) //进行33次循环,进行解码
{
temp=0;
hwx[i]=1;
while(!hw) //P3^3引脚为低电平时
{
hwx[i]++;
delay();
temp++;
if(temp==250) //防止干扰进入死循环
break;
}
i++;
temp=0;
hwx[i]=1;
while(hw) //P3^3引脚为高电平时
{
hwx[i]++;
delay();
temp++;
if(temp==250) //防止干扰进入死循环
break;
}
i++;
j--;
}
i=255;
while(i) //延时函数
{
temp=255;
while(temp)
{
//_nop_();
temp--;
}
i--;
}
temp=0;
for(i=3;i<19;i++,i++){ //处理地址位前8位,这是3是因为去掉前面的引导码。对低电平时间数据的数理,
temp>>=1; //右移一位,接收低电平在前
if(hwx[i]>30) //检测低电平时间超过30就确认为1
temp+=0x80;
}
date[0]=temp;
temp=0;
for(i=19;i<35;i++,i++){ //同上,这里处理地址位后8位
temp>>=1;
if(hwx[i]>30)
temp+=0x80;
}
date[1]=temp;
temp=0;
for(i=35;i<51;i++,i++){ //同上,这里处理控制位前8位
temp>>=1;
if(hwx[i]>30)
temp+=0x80;
}
date[2]=temp;
temp=0;
for(i=51;i<67;i++,i++){ //同上,这里处理控制位后8位
temp>>=1;
if(hwx[i]>30)
temp+=0x80;
}
date[3]=temp;
EX0=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -