📄 红外接收.txt
字号:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
/*********************************************
红外编码_红外通信
*********************************************/
data uchar tab[80];
uchar i=0,l=0;
bit sentok=0;
/******************************************/
void initTimer(void)
{
TMOD=0x1; //计数方式1
TH0=0x0; //从0开始计数
TL0=0x0;
}
/**********************************************/
void initUart(void)
{
TMOD|=0x20; //再开计数方式2
SCON=0x50; //8(10)位串口波特率方式1
PCON|=0x80; //波特率加倍
TH1=0xfa; //波特率设定为 :57600 //4.8K
TL1=0xfa;
TR1=1; //开定时计数器1
ES=1; //允许串口中断
}
/***************************************/
void sentstr()
{
i=0;
l=0;
IE0=0; //外部0中断标志位
EX0=1; //允许外部中断中断
}
////////////////////////////////////////////
void check()
{
uchar n,p=0;
if(tab[0]==0x20)
{
for(n=34;n<49;n++,n++)
{
if(tab[n]==0x03 | tab[n]==0x04 | tab[n]==0x05)
p=p>>1;
else
if(tab[n]==0x07 | tab[n]==0x08 | tab[n]==0x09)
{
p=p>>1;
p|=0x80;
}
else break;
}
return(p);
}
}
/////////////////////////////////////////////////////////////
void main(void)
{
char KEY;
IT0=1; //外部下降沿边沿触发中断
EX0=1; //允许外部INT0中断
initTimer();
initUart();
EA=1; //开总中断
while(1)
{
if(sentok)
{
sentok=0;
KEY=check();
sentstr();
}
}
}
///////////////////////////////////////////////////
void int0(void) interrupt 0 //外部中断0
{
if(l==0)
{
TR0=1; //定时计数开始
ET0=1; //允许定时器T0溢出中断
}
else
if(l<33)
{
tab[i++]=TH0;
tab[i++]=TL0;
TH0=0x0;
TL0=0x0;
}
else
if(l==33)
{
tab[i++]=TH0;
tab[i++]=TL0;
TH0=0x0;
TL0=0x0;
TR0=0;
ET0=0;
EX0=0;
sentok=1;
}
l++;
}
////////////////////////////////////////////////
void timer0(void) interrupt 1 //定时计数中断
{
ET0=0; //允许定时器溢出中断
TR0=0; //T0停止计数
i=0;
l=0;
TH0=0x0;
TL0=0x0;
}
/**********************************************/
void uart(void) interrupt 4 //串口中断
{
if(RI)
{
RI=0; //接收中断标志,只能由软件清零
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -