#include <REG51.h> sfr AUXR = 0x8E; static bit OP; //红外发射管的亮灭 static unsigned int count; //延时计数器 static unsigned int endcount; //终止延时计数 static unsigned char flag; //红外发送标志 sbit P3_4=P0^0; char iraddr1; //十六位地址的第一个字节 char iraddr2; //十六位地址的第二个字节 void delay() { int i,j; for(i=0;i<400;i++) { for(j=0;j<100;j++) { } } } void SendIRdata(char usercode1,char usercode2,char keycode) { int i; char irdata=usercode1; //发送9ms的起始码 endcount=223; flag=1; count=0; do{}while(count<endcount); //发送4.5ms的结果码 endcount=117; flag=0; count=0; do{}while(count<endcount); //发送十六位地址的前八位 irdata=usercode1; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } // for(i=0;i<8;i++) // { // //先发送0.56ms的38KHZ红外波(即编码中0.56ms的低电平) // endcount=10; // flag=1; // count=0; // do{}while(count<endcount); ////停止发送红外信号(即编码中的高电平) // if(irdata-(irdata/2)*2) //判断二进制数个位为1还是0 // { // endcount=41; //1为宽的高电平 // } // else // { // endcount=15; //0为窄的高电平 // } // flag=0; // count=0; // do{}while(count<endcount); // irdata=irdata>>1; // } //发送十六位地址的后八位 irdata=usercode2; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } //发送八位数据 irdata=keycode; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } //发送八位数据的反码 irdata=~keycode; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata-(irdata/2)*2) { endcount=41; } else { endcount=15; } flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } endcount=10; flag=1; count=0; do{}while(count<endcount); flag=0; } void main(void) { count = 0; flag = 0; OP = 0; P3_4 = 0; EA = 1; //允许CPU中断 TMOD = 0x11; //设定时器0和1为16位模式1 ET0 = 1; //定时器0中断允许 TH0 = 0xFF; TL0 = 0xE6; //设定时值0为38K 也就是每隔26us中断一次 TR0 = 1;//开始计数 do{ delay(); SendIRdata(0x48,0x77,0x11); delay(); SendIRdata(0x48,0x77,0x12); delay(); SendIRdata(0x48,0x77,0x13); delay(); SendIRdata(0x48,0x77,0x14); delay(); SendIRdata(0x48,0x77,0x15); delay(); SendIRdata(0x48,0x77,0x16); delay(); SendIRdata(0x48,0x77,0x17); delay(); SendIRdata(0x48,0x77,0x18); delay(); SendIRdata(0x48,0x77,0x19); delay(); SendIRdata(0x48,0x77,0x10); delay(); SendIRdata(0x48,0x77,0x00); delay(); SendIRdata(0x48,0x77,0x01); delay(); SendIRdata(0x48,0x77,0x02); delay(); SendIRdata(0x48,0x77,0x03); delay(); SendIRdata(0x48,0x77,0x1F); }while(1); } //定时器0中断处理 void timeint(void) interrupt 1 { TH0=0xFF; TL0=0xE6; //设定时值为38K 也就是每隔26us中断一次 count++; if (flag==1) { OP=~OP; } else { OP = 0; } P3_4 = OP; }
时间: 2024-09-29 10:02:58