Infrared Remote Control and decoding of PIC microcontroller

Source: Internet
Author: User

Play with the decoding of the infrared remote control today!

First, understand the principle of infrared reception:

The remote control is easy to use and has many functions. It has been widely used in TV sets, VCD, DVDs, air conditioners, and other household appliances. It is cheap and easy to buy in the market. If many buttons on the remote control can be decoded and used as the input of the single chip microcomputer system, the disadvantages of the conventional matrix keyboard circuit board being too large, complicated wiring, and occupying too many I/O ports are solved. In addition, by using the remote control, the separation between people and devices can be realized during operation, making it easier to use.

I. encoding format
1. encoding of 0 and 1
The signal sent by the remote control is composed of a string of binary codes of O and 1. Different chips have different codes for 0 and 1. Generally there are Manchester Coding and pulse width coding. The O and 1 of tc9012 are encoded by the PWM method, that is, the pulse width modulation. The O Code and 1 code 1 are shown in the example of the waveform of the remote control receiving output ). The O Code is composed of o.56ms low voltage and 0.56ms high level. The pulse width is 1.12ms.1 and is composed of 0.56ms low voltage and 1.69ms high voltage. The pulse width is 2.25 Ms. When writing a decoding program, you can get 0 or 1 by judging the pulse width.
2. Key Encoding

When a key is pressed for over 36 ms, the oscillator activates the chip and emits a set of encoded pulses of milliseconds, which are triggered by a starting code (9 ms ), one result code (4.5 ms) with a low 8-bit address code (9 ms ~ 18 ms), 8-bit high address code (9 ms ~ 18 ms), 8-bit data code (9 ms ~ 18 ms) and the 8-bit data back code (9 ms ~ 18 ms. If the key is pressed for more than milliseconds, the code to be sent (Code bursts) will only consist of the Start code (9 ms) and end code (2.5 ms.

When we press the button of the remote control, the remote control will issue a binary code of 2, which is called a frame of data. Based on the functions of each part. They can be divided into five parts: Guide code, address code, address code, data code, and data anti-code. When the remote control sends code, the system always goes low. High is behind. According to the analysis in Figure 2, the height of the Guide code is 4.5 ms, and the low level is 4.5 ms. When
When this code is received, it indicates the beginning of a frame of data. The microcontroller can prepare to receive the following data. The address code consists of 8-bit binary codes, 256 in total. In the figure, the address code is resent once. It mainly enhances the reliability of the remote control. If the two address codes are different, the data in this frame is wrong and should be discarded. Different devices can have different address codes. The remote control with the same encoding will not interfere with each other as long as the address code is set to different. The address code in the figure is the hexadecimal 0eh (note that the low position is prior ). In the same remote control, the address Code issued by all the buttons is the same. The data code is 8 bits and can be encoded in 256 states, representing the actually pressed key. The data anti-code is the inverse of the data code. By comparing the data code with the data anti-code, you can determine whether the received data is correct. If the relationship between the data code and the data anti-code does not meet the opposite relationship, the remote control reception is incorrect. The data should be discarded. On the same remote control, the data codes of all buttons are different. In Figure 2, the data code is a hexadecimal 0ch, and the data reverse code is a hexadecimal 0f3h (note that the low position is prior). The sum of the two should be 0ffh.

Continuous code


Ii. MCU Remote Control receiving circuit
The infrared remote control receiver can use an earlier infrared receiving diode and a dedicated infrared processing circuit. For example, cxa20106, the circuit of this method is complex and is not used now. A better method is to use an integrated infrared receiver, which combines infrared receiving diodes, amplification, demodulation, shaping, and other circuits with only three pins. they are + 5 V power supply, ground, and signal output. The shape and pins of common integrated receiving headers are shown in figure 3 and figure 4. Signal Transmission from the infrared receiver
Into or Intl foot of the single-chip microcomputer. Typical circuit 5 is shown. a pnp transistor is added in the figure to enlarge the output signal.

 

For the remote control end (the decoding end, the receiving end can reverse this)
Boot Code: 9 ms high + 4.5ms low
System Code: System Code 1 + System Code 2 (composed of "0" and "1" defined high and low levels)
Data Code: data code 1 + Data anticode (composed of "0" and "1" defined high and low levels)
Remaining code: 108 Ms-Boot Code occupation time-system code occupation time-data code occupation time.
If you press the "key" for more than 108 Ms, the remote control also emits the re-sending code:
The format of the resend code (108 Ms) is as follows:
9ms high + 2.25ms low + 0.56ms high + 96.19ms low

Connection circuit diagram:

Write the program as follows:

# Ifndef main_h
# Define main_h
# Include <PIC. h>

# Define bitset (VAR, bitno) (VAR | = 1 <bitno) // set one-bit operation
# Define bitclr (VAR, bitno) (VAR & = ~ (1 <bitno) // bit clearing operation

# Define uint unsigned int
# Define uchar unsigned char

// Define the frame receiving status
# Define ir_idle 0x01
# Define ir_start 0x02
# Define ir_add 0x03
# Define ir_addinv 0x04
# Define ir_data 0x05
# Define ir_datainv 0x06

# Define ms_168 0x0600
# Define ms_th 0x08ca
# Define ms_9 0x0800
# Define ms_306 0x0b00
# Define ms_125 0x1300
# Define ms_15 0x15f9
// Void delayms (uint MS); // millisecond-level latency Function
// Void delay10us (uint US); // latency function of 10 microseconds
Void init_all ();

# Endif

 
# Include "Main. H"
# Include "t232.h"

Typedef struct frame1
{
Uint add;
Uint addinv;
Uchar data;
Uchar datainv;
Uchar ir_state;
Uint count;
Uint temp;
Uchar ready;
} Ir_frame;

Void int_delay (uint count)
{
While (count --);
}
// Define global variable
Ir_frame frame;

Void interrupt main_int ()
{

If (INTF & intedg)
{
Gie = 0;
INTF = 0;
Tmr1on = 0;
Frame. Count = tmr1h * 256 + tmr1l;
Tmr1h = 0;
Tmr1l = 0;
Tmr1on = 1;

Switch (frame. ir_state)
{
Case ir_idle:
If (frame. Count> 0x2000) // prevents interference
Frame. ir_state = ir_start;
Break;
Case ir_start:
If (frame. Ready = 1) // No processing, wait
{
Frame. ir_state = ir_idle;
Return;
}
If (frame. Count> ms_125) & (frame. Count <ms_15) // guide code
{
Frame. Add = 0;
Frame. addinv = 0;
Frame. Data = 0;
Frame. datainv = 0;
Frame. ir_state = ir_add;
Frame. Temp = 1;
Frame. Ready = 0;

}
Else if (frame. Count> ms_9) & (frame. Count <ms_306) // repeat the frame
{
Frame. Ready = 1;
Frame. ir_state = ir_idle;

}
Else // noise?
{
Frame. ir_state = ir_idle;
}
Break;
Case ir_add:
If (frame. Count <0x280) // noise?
Return;
If (frame. Count> ms_168) // high level?
Frame. Add | = frame. Temp;

Frame. Temp = frame. Temp <1;
If (frame. Temp = 0x2000) // 13-bit OK?
{
Frame. ir_state = ir_addinv;
Frame. Temp = 1;
}
Break;
Case ir_addinv:
// Int_delay (75 );
// If (rb0)
If (frame. Count <0x280)
Return;
If (frame. Count> ms_168 ))
Frame. addinv | = frame. Temp;

Frame. Temp = frame. Temp <1;
If (frame. Temp = 0x2000) // 13-bit OK?
{
Frame. ir_state = ir_data;
Frame. Temp = 1;
}
Break;
Case ir_data:
If (frame. Count <0x280) // Noise
Return;
If (frame. Count> ms_168) // High Level
Frame. Data | = frame. Temp;

Frame. Temp = frame. Temp <1;
If (frame. Temp = 0x0100) // 8-bit finished?
{
Frame. ir_state = ir_datainv;
Frame. Temp = 1;
}
Break;
Case ir_datainv:
If (frame. Count <0x280)
Return;
If (frame. Count> ms_168 ))
Frame. datainv | = frame. Temp;

Frame. Temp = frame. Temp <1;
If (frame. Temp = 0x0080) // 7-bit finished?
{
Frame. Temp = frame. Temp <1;
Int_delay (75); // latency waits for the last bit level
If (rb0)
Frame. datainv | = frame. Temp;
If (frame. Data | frame. datainv = 0xff) & (frame. Add = 0x011f ))
{
Frame. Ready = 1;
}
Frame. ir_state = ir_idle;
}
Break;
Default:
Break;
}
Gie = 1;
}
}

Void init_all ()
{
Init_232 ();
// Init_hongwai ();
Intcon = 0;
Inte = 1; // open RB level interrupt
Peie = 1;
Intedg = 1;
Trisb0 = 1; // rb4 is input
T1con = 0;
Tmr1h = 0xaa;
Tmr1l = 0xbb;
Frame. ir_state = ir_idle;
Gie = 1;
Adcon0 = 0x06;
Trisa0 = 0;
}
Void main ()
{
Const char STR [] = "Hello world! ";

Init_all ();
Send_str (STR); // test the serial port
While (1)
{

If (frame. Ready = 1)
{
Put_char (frame. data );
Frame. Ready = 0;
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.