I. Introduction
Infrared transmitter module: Power Supply 3.3v or 5 V. It emits a 38 kHz infrared signal through the Transmitting Tube and is driven at a high level.
Infrared receiving module: the power supply is 3.3v or 5 V. It can receive infrared signals and demodulated them into a logic level. The low level is effective.
Ii. Circuit Diagram
Infrared Emission circuit diagram
Infrared circuit diagram
Iii. Code of the infrared emission module
# Include <reg51.h> # include <intrins. h> # define uchar unsigned char # define uint unsigned int // pin definition sbit key = P2 ^ 0; sbit out = P2 ^ 1; // function declaration void delayms (uchar aa); // void kHz (uchar aa) for infrared emission delay; // 38 khzvoid ir_out (uchar num) for transmitting ); // infrared emission void keyscan (); // read keyboard information // variable declaration uchar I, A, num1; // ----------------------- // infrared emission delay // --------------------------- void delayms (uchar aa) {for (a = AA; a> 0; A --) {_ NOP _(); _ NOP _(); _ NOP _ () ;}// ----------------------- // transmit 38 kHz // ------------------------- void kHz (uchar aa) {for (a = AA; a> 0; --) // This for statement can obtain an accurate 26.3 baud rate {out = 0; I = 7; while (I> 0) I --; // 38 kHz out = 1 ;}} // ----------------------- // infrared emission // --------------------------- void ir_out (uchar num) {kHz (116); // transmit 3 ms 38 kHz delayms (125); For (num1 = 8; num1> 0; num1 --) {kHz (40); If (Num & 0x01) delayms (93); // delay1.5 Ms else delayms (65 ); // delay 1 ms num = num> 1;} kHz (20);} // --------------------- // read the keyboard information // ------------------------- void keyscan () {If (Key = 0) {_ NOP _(); _ NOP _ (); If (Key = 0) ir_out (0xc0 );
Key = 1 ;}/// ----------------------- // main function // ----------------------- void main () {key = 1; while (1) {keyscan ();}}
Iv. Code of the infrared receiving module
# Include <reg51.h> # include <intrins. h> # define uchar unsigned char # define uint unsigned int // pin definition sbit in = P3 ^ 2; // infrared interrupt reception // P2 static digital tube // function declaration void Init (); // initialization function void delayms (uchar J ); // latency function // variable declaration uchar I, num, A; bit FLEG; // --------------------- // initialization function // --------------------------- void Init () {FLEG = 1; // flag in = 1; // IR receiver Ea = 1; // interrupt settings ex0 = 1; it0 = 1;} // ----------------------- // delay function //------------------ ----- Void delayms (uchar J) {for (I = J; I> 0; I --) {_ NOP _(); _ NOP _ (); _ NOP _ () ;}// ----------------------- // main function // ----------------------- void main () {Init (); while (1);} // --------------------- // receive interrupt // ------------------------- void sieasdf () interrupt 0 {ex0 = 0; for (a = 5; a> 0; --) {delayms (35); // latency 0.5 ms 5 times 5*0.5 = 2.5 ms if (in) FLEG = 0;} If (FLEG) {delayms (72 ); // 1 ms latency High level if (in) {delayms (115); // delay it over 2 ms; 2.5 + 1 + 1.623 = 5.123 Ms start to read data delayms (118 ); // If the offset is one bit, you can remove it. For (A = 8; a> 0; A --) {While (! In); delayms (86); // The latency is 1.188 ms to determine the IO level, so that 0 or 1 num = num> 1; if (in) {num = num | 0x80; delayms (31); // The latency is 0.6 Ms because the latency is 1.2 ms + 0.6, and 1.5 ms is skipped.} P2 = num ;}} FLEG = 1; ex0 = 1 ;}
"51 Single-Chip Microcomputer" infrared receiving and transmitting module code