#include <reg52.h> sbit P2_2=P2^2;sbit signalBit=P2^1; //
发送信号标志位sbit LED=P3^7; //
使能信号位,将times New Roman">E位定义为P2.2引脚unsigned char irData[]={0x14,0xeb,0xf0,0x0f}; //
存放所有的控制码unsigned char mazhi[]={0x33,0x22,0xeb,0x14};unsigned char key,send_flag=0;void SendIRdata(unsigned char irdata[4]); //
红外发射函数void anjian_send(unsigned char flag);void delay(unsigned int tt) //
延时函数 { while(--tt);}void main(void) { signalBit=0; P2_2= 0; TMOD=0x12;//
设定定时器1和0工作方式为1和2 EX1=1; IT1=1; ET0 =1; //
定时器0中断允许 ET1 =1; TH0= 0xf4; TL0= 0xf4; //
设定时值0为38K 也就是每隔13us中断一次 周期为26us TR0= 0;//
先关闭定时器 0 TR1= 0; IP=0X01; EA=1; //
允许CPU中断 LED=0; while(1) { P1=0XF0; if(send_flag==1) { anjian_send(key); send_flag=0; } }}void anjian_send(unsigned char flag){ unsigned char zhi=flag; switch(zhi) { case 0xee: SendIRdata(irData); break; case 0xbe: SendIRdata(irData); break; case 0xde: SendIRdata(irData); break; case 0x7e: SendIRdata(irData); break; case 0xed: SendIRdata(irData); break; case 0xdd: SendIRdata(irData); break; case 0xbd: SendIRdata(irData); break; case 0x7d: SendIRdata(irData); break; case 0xeb: SendIRdata(irData); break; case 0xdb: SendIRdata(irData); break; case 0xbb: SendIRdata(mazhi); break; case 0x7b: SendIRdata(mazhi); break; case 0xe7: SendIRdata(mazhi); break; case 0xd7: SendIRdata(mazhi); break; case 0xb7: SendIRdata(mazhi); break; case 0x77: SendIRdata(mazhi); break; } }/*******
定时器1中断处理 ***********/void isr_T1(void) interrupt 3{ }/*******
定时器0中断处理 38K载波发生器***********/void timeint(void) interrupt 1 { P2_2=~P2_2; //
产生38k的载波信号}/****************
中断1 键盘的扫描 ********************/ void Int1(void) interrupt 2 {unsigned char l,h; P1=0XF0; l=P1; l=l&0xf0; if(l!=0xf0) { delay(10); if(l!=0xf0) { l=P1&0xf0; l=l|0x0f; P1=l; h=P1; h=h&0x0f; l=l&0xf0; key=h+l; send_flag=1; LED=~LED; } }} /*********
红外发射函数 ************/void SendIRdata(unsigned char *mData) { unsigned char i,j; unsigned char test[4]; for(i=0;i<4;i++) test
=mData; TR0=1; //开定时器T0 输出38K载波/********* 发送9ms的高电平 ************/ TH1=0XDF; //装入初值设置波延时为9ms TL1=0XE3; TR1=1; //启动T1工作 定时所需时间 signalBit=1; //发送信号 while(!TF1); TF1=0;/********* 发送4.5ms的低电平 *************/ TH1=0xef; TL1=0xf0; signalBit=0; while(!TF1); TF1=0;/********* 发送 用户码 控制码 控制反码 ***********/ for(j=0;j<4;j++) //发送十六位地址的前八位 { for(i=0;i<8;i++) { //先发送0.565ms的38KHZ红外波(即编码中0.565ms的高电平) TH1=0XFE; TL1=0X70; signalBit=1; while(!TF1); TF1=0; //停止发送红外信号(即编码中的高电平) if(test[j]&0x01) //判断二进制数个位为1还是0 { TH1=0XFA; // //1为宽的低电平,持续时间1.685ms TL1=0X30; } else { TH1=0XFE; //0为窄的低电平,持续时间 0.56ms TL1=0x50; } signalBit=0; while(!TF1); TF1=0; test[j]=test[j]>>1; } } TR1=0; //发送停止位 关闭定时器T0 signalBit=1; delay(100); signalBit=0; TR0=0; //关闭定时器T1 }
-
一周热门 更多>