单片机的GND接红外接收头的GND
单片机的IO接红外接收头的TTL输出引脚
/* 89C52 @12M */ #include <REG51.h> sfr AUXR = 0x8E; static bit RealOutput; //红外发射管的亮灭 static unsigned int count; //延时计数器 static unsigned int endcount; //终止延时计数 static unsigned char flag; //红外发送标志 sbit Send_ISR=P2^0; sbit Runled =P2^1; char iraddr1; //十六位地址的第一个字节 char iraddr2; //十六位地址的第二个字节 unsigned char code key_0=0x10; unsigned char code key_1=0x11; unsigned char code key_2=0x12; unsigned char code key_3=0x13; unsigned char code key_4=0x14; unsigned char code key_5=0x15; unsigned char code key_6=0x16; unsigned char code key_7=0x17; unsigned char code key_8=0x18; unsigned char code key_9=0x19; unsigned char code key_up=0x19; unsigned char code key_down=0x19; unsigned char code key_left=0x19; unsigned char code key_right=0x19; unsigned char code key_menu=0x19; unsigned char code key_ok=0x19; unsigned char code key_back=0x1C; void delay(unsigned int a){ //[email protected] 1ms unsigned int i; while ( --a != 0 ) for (i=0;i<=20;i++); } void SendCode(unsigned char Code){ unsigned char irdata,i; irdata=Code; for(i=0;i<8;i++) { endcount=10; flag=1; count=0; do{}while(count<endcount); if(irdata%2) {endcount=41;} else{endcount=15;} flag=0; count=0; do{}while(count<endcount); irdata=irdata>>1; } } void SendIRdata(char keycode){ char usercode1=0x11; char usercode2=0x11; char irdata=usercode1; endcount=223; //发送9ms的起始码,. flag=1; count=0; do{}while(count<endcount); endcount=117; //发送4.5ms的结果码 flag=0; count=0; do{}while(count<endcount); SendCode(usercode1); SendCode(usercode2); SendCode(keycode); SendCode(~keycode); endcount=100; flag=1; count=0; do{}while(count<endcount); flag=0; } void main(void) { count = 0; flag = 0; RealOutput = 0; Send_ISR = 0; EA = 1; //允许CPU中断 TMOD = 0x11; //设定时器0和1为16位模式1 ET0 = 1; //定时器0中断允许 TH0 = 0xFF; TL0 = 0xE6; //设定时值0为38K 也就是每隔26us中断一次 TR0 = 1; //开始计数 while(1){ SendIRdata(key_0); delay(1000); SendIRdata(key_1); delay(1000); // Runled=!Runled; // delay(1000); } } void timeint(void) interrupt 1 { TH0=0xFF; TL0=0xE6; //设定时值为38K 也就是每隔26us中断一次 count++; // if (flag) {RealOutput=~RealOutput;} if (flag) {RealOutput=0;} else{RealOutput = 1;} Send_ISR = RealOutput; }
编译
Rebuild target ‘Target 1‘ assembling STARTUP.A51... compiling send_ISR.C... linking... Program Size: data=17.1 xdata=0 code=337 creating hex file from "Send_ISR"... "Send_ISR" - 0 Error(s), 0 Warning(s).
时间: 2024-10-10 00:20:03