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)

}

给你一段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=0k<17k++)

{

CounterIR=0

while((P2IN&BIT0)&&(CounterIR<50))

{

DelayIR()

CounterIR++

}

CounterIR=0

while((!(P2IN&BIT0))&&(CounterIR<10))

{

DelayIR()

CounterIR++

}

}

for(char j=0j<8j++)

{

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|=runflagbreak

case 0x68 : Tar[4]=0flag|=dataflagbreak

case 0x30 : Tar[4]=1flag|=dataflagbreak

case 0x18 : Tar[4]=2flag|=dataflagt2++break

case 0x7a : Tar[4]=3flag|=dataflagbreak

case 0x10 : Tar[4]=4flag|=dataflagbreak

case 0x38 : Tar[4]=5flag|=dataflagbreak

case 0x5a : Tar[4]=6flag|=dataflagbreak

case 0x42 : Tar[4]=7flag|=dataflagbreak

case 0x4a : Tar[4]=8flag|=dataflagbreak

case 0x52 : Tar[4]=9flag|=dataflagbreak

default : break

}

}

}

}

IR=0

}

}

for(int m=0m<250m++)

DelayIR()

P2IES|=BIT0

}

#endif

请采纳。

以下汇编程序,delay的时间你需要自己调整一下,再加个2重循环吧,现在的时间是很短的。

P1.3为开门信号,P1.4为关门信号,输入输出低电平有效。

org 00h

ljmp main

org 30h

main:

jb p1.0,next1

setb 00h

clr 01h

next1:

jb p1.1,next2

clr 00h

next2:

jb p1.2,next3

clr 01h

next3:

mov c,00h

cpl c

mov p1.3,c

mov c,01h

cpl c

mov p1.4,c

jnb 02h,next4

jb 00h,next4

lcall delay

setb 01h

clr 00h

next4:

mov c,00h

mov 02h,c

ljmp main

delay:

mov r7,#00h

lp:

mov r6,#00h

djnz r6,$

djnz r7,lp

ret


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存