我自己编了个红外发射和接收程序,可是硬件调试时发现接收不了信号,,查了好久都没有查出来,,请高手帮帮忙啊!
下面是我的发射信号和接收程序:请帮忙指点指点,谢谢了!!
发射信号程序:
//晶振24MHz,
//38kHz,周期T=26us,
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit hongwai=P1^0;
void delay_12us(uchar g)
{
while (g--);
}
void delay_1ms(uint k)
{
uint i,j;
for (i=k;i>0;i--)
for (j=247;j>0;j--); //j取247时,1ms调用时相差1us
}
xinhao_fashe()
{
uint m; //195.50-194.50=1us
for (m=1;m<=720;m++)
{ hongwai=~hongwai; //196.50-195.50=1us
delay_12us(1); //205.50-196.50=9us,返回去后有需要2.5us时间,故共定时9+2.5+1=12.5us
_nop_();
}
hongwai=0; //9408.0-9407.5=0.5us
delay_1ms(4); //13392.5-9408.0=3883.5us
for (m=1;m<=77;m++) // 4201.00-4200.00=1us
{
hongwai=~hongwai; //4201.5-4201.00=0.5us
delay_12us(1); //4210.00-4201.5=8.0us
}
hongwai=0;
delay_1ms(1);
for (m=1;m<=77;m++) //第二个1ms信号
{
hongwai=~hongwai;
delay_12us(1);
}
hongwai=0; //1ms低电平间隔
delay_1ms(1);
for (m=1;m<=77;m++) //第三个1ms信号
{
hongwai=~hongwai;
delay_12us(1);
}
hongwai=0; //2ms低电平间隔
delay_1ms(2); //1995.5us=2ms
for (m=1;m<=77;m++) //第四个1ms信号
{
hongwai=~hongwai;
delay_12us(1);
}
hongwai=0; //1ms低电平间隔
delay_1ms(1);
for (m=1;m<=320;m++) //4ms截止码
{
hongwai=~hongwai;
delay_12us(1);
}
delay_1ms(100);//26ms低电平间隔
}
void main()
{
while (1)
{
xinhao_fashe();
}
}
接收解码程序:
//12mhz
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar wb=0,irr_b=0,da=0,p=0,s_flag=0;
uint a=0;
sbit beep=P3^4;
void delay_1ms(uint k)
{
uint i,j;
for (i=k;i>0;i--)
for (j=112;j>0;j--);
}
void ext_int0_isr(void) interrupt 0
{
a=TH1*256+TL1; //读取定时器的值
if(irr_b==0) //第一个中断
{
irr_b=1; //状态为:开始接收
TH1=0x00; //定时器清零
TL1=0x00;
}
else if(irr_b==1) //第二个以后的中断
{
//a=TCNT1L;
//判断是引导,还是数据
if((11000<a)&&(a<14000)) //引导
{
wb=0; //数据计数清零
da=0; //数据清零
TH1=0x00; //定时器清零
TL1=0x00;
}
else if((1500<a)&&(a<2500)) //数据0
{
wb++;
da=(da<<1);
TH1=0x00;
TL1=0x00;
}
else if((2500<a)&&(a<3500)) //数据1
{
wb++;
da=(da<<1)+1;
TH1=0x00;
TL1=0x00;
}
}
if(wb==4) //数据长度够4位
{
irr_b=0; //状态为停止接收
wb=0; //数据计数清零
p=da;
if (da==0x01) s_flag=1; //1号信号
else if (da==0x02) s_flag=2; //2号信号
p=~p;
}
}
//*****************************************************************************
void init()
{
TMOD=0x10;
ET1=1;
TR1=1;
TH1=0;TL1=0;
TF1=0;
EX1=1;
IT1=1;
EA=1;
}
//*****************************************************************************
/*void fmq()
{
beep=0;
delay_1ms(100);
beep=1;
delay_1ms(100);
} */
void main()
{
P3=0xff;
P2=0xff;
P1=0xff;
init();
while(1)
{
if (s_flag==1)
{
P2=p;
s_flag=0;
EX1=0;
EA=0;
//fmq();
EX1=1;
EA=1;
}
else
{
if (s_flag==2)
{
P2=p;
s_flag=0;
EX1=0;
EA=0;
//fmq();
beep=0;
delay_1ms(100);
beep=1;
delay_1ms(100);
EX1=1;
EA=1;
}
}
P2=0xff;
}
}
beep是接蜂鸣器的,接收时发现蜂鸣器不响,这是什么缘故呢??请多指教啊!! |