C语言怎么写红外程序?实现遥控控制八个led。求程序和详细解释。

C语言怎么写红外程序?实现遥控控制八个led。求程序和详细解释。,第1张

下面是一个用C写的遥控器漏巧漏程序.能在数码管上显示键码.

#include <reg52.h>

#define c(x) (x*110592/120000)

sbit Ir_Pin=P3^3

unsigned char code Led_Tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,

0xf8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E} //共阳极数码显示码0-F.

unsigned char code Led_Sel[]={0xe,0xd,0xb,0x7}

unsigned char Led_Buf[4]//宽桐显示缓冲区

char Led_Index //位选

unsigned char Ir_Buf[4]//用于保存解码结果

//==============================================================

//数码管扫描

timer0() interrupt 1 using 1

{

TL0=65536-1000

TH0=(65536-1000)/256//定时器0设定约1000us中断一次,用于数码管扫描

P0=0xff

P2=Led_Sel[Led_Index] //位选

P0=Led_Tab[Led_Buf[Led_Index]] //段选

if(++Led_Index>3) Led_Index=0 //四个扫描完了,到第一个数码管

}

//==============================================================

unsigned int Ir_Get_Low()

{

TL1=0

TH1=0

TR1=1

while(!Ir_Pin &&(TH1&0x80)==0)

TR1=0

return TH1*256+TL1

}

//=============================================================

unsigned int Ir_Get_High()

{

TL1=0

TH1=0

TR1=1

while(Ir_Pin &&(TH1&0x80)==0)

TR1=0

return TH1*256+TL1

}

//==============================================================

main()

{

unsigned int temp

char i,j

Led_Index=1

TMOD=0x11

TL0=65536-1000

TH0=(65536-1000)/256//定时器0设定约1000us中断一次,用于数码管扫描

EA=1

ET0=1

TR0=1

Led_Buf[0]=0

Led_Buf[1]=0

Led_Buf[2]=0

Led_Buf[3]=0//显示区返烂设成0

do{

restart:

while(Ir_Pin)

temp=Ir_Get_Low()

if(temp<c(8500) || temp>c(9500)) continue//引导脉冲低电平9000

temp=Ir_Get_High()

if(temp<c(4000) || temp>c(5000)) continue//引导脉冲高电平4500

for(i=0i<4i++) //4个字节

for(j=0j<8j++) //每个字节8位

{

temp=Ir_Get_Low()

if(temp<c(200) || temp>c(800)) goto restart

temp=Ir_Get_High()

if(temp<c(200) || temp>c(2000)) goto restart

Ir_Buf[i]>>=1

if(temp>c(1120)) Ir_Buf[i]|=0x80

}

Led_Buf[0]=Ir_Buf[2]&0xf

Led_Buf[1]=(Ir_Buf[2]/16)&0xf

Led_Buf[2]=Ir_Buf[3]&0xf

Led_Buf[3]=(Ir_Buf[3]/16)&0xf//显示结果

}while(1)

}

这个简单就是一个解码函灵敏而已!

int jiema()

{

unsigned int data,temp

RESET:

for( i=0i<8i++ ) //**9ms

{

delay_nms(1)

if((PIND&0B00000100)==0B00000100 )

goto RESET //是检测在8毫秒内如果出现高电平就退出解码程序

}

temp = 8000

while( ( (PIND&0B00000100)==0B00000000 ) &&(temp!=0) )

{temp--}

//等待4.5ms高电平的到来,避开9毫秒低电平引导脉冲

//temp--需0.25us, temp=8000可实现2ms延时,防止干扰脉冲造成的死机。

delay_nus(1804) //**4.5ms

for( j=0j<4j++)

{

for( i=0i<8i++ )

{

temp = 4000

while( ((PIND&0B00000100)==0B00000000 ) &&(temp!=0) )

{temp--}

delay_nus(334)

if((PIND&0B00000100)==0B00000100 ) //检测到高电平1的话说明该位为1,延时1毫秒氏并等待歼蔽迹脉冲高电平结束

{

data |= (1<<i)//dataH= ~data dataL= data

delay_nus(379)

}

else data &= ~(1<<i) //检测到低电平0的话,说明该位为0,继续检测下一位

}

IR_BUF[j]=data

}

if(IR_BUF[2] = ~IR_BUF[3]) /并斗/校验

{

PORTA=IR_BUF[2]

delay_nms(500)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12468578.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存