c语言红外解码程序,红外遥控解码,中断方式,51 C语言程序

#include        //管脚定义头文件

//#include

#define uc unsigned char

#define ui  unsigned int

#define OSC 12000000  //晶振频率,按实际修改

#define MS  4 //一毫秒的次数

sbit  bat = P3^3 ;  //嗡鸣器引脚

sbit  power=P3^7;

#define ID 239  //遥控ID号.

code  unsigned char d[]=

{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,

0x88,0x83,0xc6,0xa1,0x86,0x8e

};

bit pid_if,sleep_en,ir_if,int_if;

uc bb,t0,buf[6],wei;

uc shu[8];

uc tc;

void process_ir(void);

void process_beep(void);

void init(void);

void display(void);

void process_int(void);

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

void process_int(void)

{

static uc yw ;

int_if=0;

if( tc< 6 ){yw>>=1;wei++;} //脉宽0

else

if(  tc<=12   ){yw>>=1;yw|=0x80;wei++;} //脉宽1

else wei=0;

if( (wei&7)==0 )buf[(wei>>3) ]=yw; //有8个BIT,记录到BUF内

if(wei==32)EX0=0;//收到32个位,也就是四个字节.数据够了.停止接收,关外部中断

}

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

void display(void)

{

static uc n=0;

P2=0XFF;

P0= d[shu[n]] ;

P2=~(1<<7-n);

n++;

n%=8;

}

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

void process_ir(void)

{static uc i;

ui id ;

id = (buf[1]<<8) + buf[2];

if( (buf[3]|buf[4])==0xff) //收到遥控信息,并且验证码正确

{

bb=1;

shu[0] =buf[4]&0x0f;

shu[1] =buf[4]>>4;

shu[2] =buf[3]&0x0f;

shu[3] =buf[3]>>4;

shu[4] =buf[2]&0x0f;

shu[5] =buf[2]>>4;

shu[6] =buf[1]&0x0f;

shu[7] =buf[1]>>4;

if (  id ==ID  || id ==255 )

{

bb=2;//嗡鸣器叫一声

switch(buf[3])

{

case 0:power=!power;

break;

case 10:power=1;

break;

case 2:power=1;

break;

case 3:power=0;

break;

case 12:power=0;

break;

}

}

}

for(i=0;i<6;i++)buf[i]=0xff;

wei=0;

EX0=1;

t0=0;

}

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

void process_beep(void)

{

static uc n=0;

pid_if=0;

if(bb>3)bb=0;

if(bb>0)

{

if(n==0)

{

bat=!bat;

if(bat)bb--;

}

n++;

if(n>11)n=0;

}

else n=0;

}

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

void init(void)

{

EX0 = 1;                    //使能INT0外部中断

IT0 = 1;                    //下降沿触发

TMOD=0x11 ;

RCAP2H=TH2=(65535-3*1000)/256 ;            //脉宽调节

RCAP2L=TL2=(65535-3*1000)%256 ;

EA=1;

ET0=1;

ET1=1;

ET2=1;

TR2=1;

TR1=1;

TR0=1;

power=1;

}

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

void  main(void)

{

init();

while(1)

{

if(int_if)process_int();

if(EX0==0)process_ir();

if(pid_if)

{

pid_if=0;

process_beep();

display();

}

if(power==1)

{

if(sleep_en)

{

sleep_en=0;

//睡眠前要处理的指令加在这里.关灯什么的

//          P0=0XFF;

//          PCON=2;

//_nop_();

//_nop_();

}

}

;

} //while(1);

}

void IR_IN()interrupt 0 using 0

{

TR0=0;

tc = TH0;

TH0=

TL0=0;                      //清定时器0

t0=0;                             //睡眠时间清0

int_if=1;

TR0=1;

}

/*********************************************************

定时器0中断服务程序

*********************************************************/

void timer0() interrupt 1 using 1

{

wei=0;

}

/*********************************************************

定时器1中断服务程序

*********************************************************/

void timer1() interrupt 3  using 3

{

t0++;

if(t0==500/65)sleep_en=1;

//wei=0;

}

void timer2() interrupt 5  using 3

{

TF2=0;

pid_if=1;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值