用C语言编写一个红外发送和接收的程序???
2个回答
展开全部
给你一段430单片机遥控器解码的程序吧,也就是接收部分
毕竟世界太现实只能这么帮你
#ifndef _IR_
#define _IR_
char t0,t1,t2;
uchar IR;
void DelayIR()
{
uint i=130;
while(i--);
}
void init_port2(void)
{
P2DIR&=~BIT0;
P2SEL&=~BIT0;
P2IES|=BIT0;
P2IE|=BIT0;
}
#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR(void)
{
char CounterIR;
P2IES&=~BIT0;
if(P2IFG&BIT0)
{
P2IFG&=~BIT0;
}
CounterIR=0;
while(!(P2IN&BIT0))
{
DelayIR();
CounterIR++;
}
if(CounterIR>85)
{
if(CounterIR<95)
{
t1++;
for(char k=0;k<17;k++)
{
CounterIR=0;
while((P2IN&BIT0)&&(CounterIR<50))
{
DelayIR();
CounterIR++;
}
CounterIR=0;
while((!(P2IN&BIT0))&&(CounterIR<10))
{
DelayIR();
CounterIR++;
}
}
for(char j=0;j<8;j++)
{
CounterIR=0;
while((P2IN&BIT0)&&(CounterIR<25))
{
DelayIR();
CounterIR++;
}
IR<<=1;
if(CounterIR>11)
IR|=BIT0;
CounterIR=0;
while((!(P2IN&BIT0))&&(CounterIR<10))
{
DelayIR();
CounterIR++;
}
}
if((IR!=0xff)&&(IR!=0x00))
{
if(!(flag&dataflag))
{
if(IR==0x22)
{
flag|=runflag;
}
else
{
Tar[0]=Tar[1];
Tar[1]=Tar[2];
Tar[2]=Tar[3];
Tar[3]=Tar[4];
switch (IR)
{
//case 0x22 : flag|=runflag;break;
case 0x68 : Tar[4]=0;flag|=dataflag;break;
case 0x30 : Tar[4]=1;flag|=dataflag;break;
case 0x18 : Tar[4]=2;flag|=dataflag;t2++;break;
case 0x7a : Tar[4]=3;flag|=dataflag;break;
case 0x10 : Tar[4]=4;flag|=dataflag;break;
case 0x38 : Tar[4]=5;flag|=dataflag;break;
case 0x5a : Tar[4]=6;flag|=dataflag;break;
case 0x42 : Tar[4]=7;flag|=dataflag;break;
case 0x4a : Tar[4]=8;flag|=dataflag;break;
case 0x52 : Tar[4]=9;flag|=dataflag;break;
default : break;
}
}
}
}
IR=0;
}
}
for(int m=0;m<250;m++)
DelayIR();
P2IES|=BIT0;
}
#endif
毕竟世界太现实只能这么帮你
#ifndef _IR_
#define _IR_
char t0,t1,t2;
uchar IR;
void DelayIR()
{
uint i=130;
while(i--);
}
void init_port2(void)
{
P2DIR&=~BIT0;
P2SEL&=~BIT0;
P2IES|=BIT0;
P2IE|=BIT0;
}
#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR(void)
{
char CounterIR;
P2IES&=~BIT0;
if(P2IFG&BIT0)
{
P2IFG&=~BIT0;
}
CounterIR=0;
while(!(P2IN&BIT0))
{
DelayIR();
CounterIR++;
}
if(CounterIR>85)
{
if(CounterIR<95)
{
t1++;
for(char k=0;k<17;k++)
{
CounterIR=0;
while((P2IN&BIT0)&&(CounterIR<50))
{
DelayIR();
CounterIR++;
}
CounterIR=0;
while((!(P2IN&BIT0))&&(CounterIR<10))
{
DelayIR();
CounterIR++;
}
}
for(char j=0;j<8;j++)
{
CounterIR=0;
while((P2IN&BIT0)&&(CounterIR<25))
{
DelayIR();
CounterIR++;
}
IR<<=1;
if(CounterIR>11)
IR|=BIT0;
CounterIR=0;
while((!(P2IN&BIT0))&&(CounterIR<10))
{
DelayIR();
CounterIR++;
}
}
if((IR!=0xff)&&(IR!=0x00))
{
if(!(flag&dataflag))
{
if(IR==0x22)
{
flag|=runflag;
}
else
{
Tar[0]=Tar[1];
Tar[1]=Tar[2];
Tar[2]=Tar[3];
Tar[3]=Tar[4];
switch (IR)
{
//case 0x22 : flag|=runflag;break;
case 0x68 : Tar[4]=0;flag|=dataflag;break;
case 0x30 : Tar[4]=1;flag|=dataflag;break;
case 0x18 : Tar[4]=2;flag|=dataflag;t2++;break;
case 0x7a : Tar[4]=3;flag|=dataflag;break;
case 0x10 : Tar[4]=4;flag|=dataflag;break;
case 0x38 : Tar[4]=5;flag|=dataflag;break;
case 0x5a : Tar[4]=6;flag|=dataflag;break;
case 0x42 : Tar[4]=7;flag|=dataflag;break;
case 0x4a : Tar[4]=8;flag|=dataflag;break;
case 0x52 : Tar[4]=9;flag|=dataflag;break;
default : break;
}
}
}
}
IR=0;
}
}
for(int m=0;m<250;m++)
DelayIR();
P2IES|=BIT0;
}
#endif
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询