Infrared remote control and decoding of PIC microcontroller

Publisher:喜悦的38号Latest update time:2016-07-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Play infrared remote control decoding 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 various household appliances such as TVs, VCDs, DVDs, air conditioners, etc. It is cheap and easy to buy on the market. If many buttons on the remote control can be decoded and used as inputs of the single-chip system, it will solve the problems of conventional matrix keyboard circuit boards being too large, wiring complex, and occupying too many I/O ports. Moreover, by using the remote control, the separation of people and equipment can be achieved during operation, making it more convenient to use.

Pin diagram of infrared receiver

  1. Coding format
  1. Coding of 0 and 1
  The signal transmitted by the remote control consists of a string of binary codes of 0 and 1. Different chips have different encodings for 0 and 1. Usually there are Manchester encoding and pulse width encoding. The 0 and 1 of TC9012 are encoded using the PWM method, that is, pulse width modulation. Its 0 code and 1 code are shown in Figure 1 (taking the waveform of the remote control receiving output as an example). The 0 code is composed of a 0.56ms low level and a 0.56ms high level. The pulse width is 1.12ms. The 1 code is composed of a 0.56ms low level and a 1.69ms high level. The pulse width is 2.25ms. When writing a decoding program, you can get 0 or 1 by judging the width of the pulse.
  2. Key coding

When a key is pressed for more than 36ms, the oscillator activates the chip and transmits a set of 108ms coded pulses. The 108ms transmission code consists of a start code (9ms), a result code (4.5ms), a low 8-bit address code (9ms~18ms), a high 8-bit address code (9ms~18ms), an 8-bit data code (9ms~18ms) and the inverse code of the 8-bit data (9ms~18ms). If the key is pressed for more than 108ms and is not released, the next transmitted code (continuous code) will only consist of the start code (9ms) and the end code (2.5ms).

  When we press the button of the remote control, the remote control will send out a string of binary codes as shown in Figure 2, which we call a frame of data. According to the function of each part, they can be divided into 5 parts, namely, the guide code, address code, address code, data code, and data inverse code. When the remote control transmits the code, the low bit is in front and the high bit is in the back. From the analysis of Figure 2, we can get that The high level of the boot code is 4.5ms, and the low level is 4.5ms. When this
code is received, it indicates the beginning of a frame of data. The single chip can prepare to receive the following data. The address code consists of 8 bits of binary, with a total of 256 types. The address code is repeated once in the figure. It is mainly to enhance the reliability of the remote control. If the two address codes are different, it means that the data of this frame is wrong and should be discarded. Different devices can have different address codes. Therefore, remote controls with the same encoding will not interfere with each other as long as the address codes are set differently. The address code in the figure is hexadecimal 0EH (note that the low bit is in front). In the same remote control, the address code sent by all buttons is the same. The data code is 8 bits and can encode 256 states, representing the actual keys pressed. The data inverse code is the inverse of each bit of the data code. By comparing the data code with the data inverse code, it can be determined whether the received data is correct. If the relationship between the data code and the data inverse code does not meet the opposite relationship, the remote control reception is wrong and 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 hexadecimal 0CH, and the data inverse code is hexadecimal 0F3H (note that the low bit is in the front). The sum of the two should be 0FFH.

Infrared encoding timing

Continuous code

Continuous code
    2. Single-chip remote control receiving circuit
    Infrared remote control receiving can adopt the earlier infrared receiving diode plus a dedicated infrared processing circuit method. Such as CXA20106, this method has a complex circuit and is generally not used now. A better receiving method is to use an integrated infrared receiving head, which combines infrared receiving diodes, amplification, demodulation, shaping and other circuits together, with only three pins. They are +5V power supply, ground, and signal output. The appearance and pins of the commonly used integrated receiving head are shown in Figures 3 and 4. The signal output of the infrared receiving head is
connected to the INTO or INT1 pin of the single-chip microcomputer. The typical circuit is shown in Figure 5. A PNP transistor is added in the figure to amplify the output signal.

Infrared reception

 

 For the remote control end (decoding end, the receiving end can just invert this)
Guide code: 9ms high + 4.5ms low
System code: System code 1 + System code 2 (consisting of "0" and "1" that define high and low levels)
Data code: Data code 1 + Data inverse code (consisting of "0" and "1" that define high and low levels)
Remaining code: 108ms - Guide code occupied time - System code occupied time - Data code occupied time.
If the "key" is pressed for more than 108ms, the remote control will also send a retransmission code:
The format of the retransmission code (108ms) is as follows:
9ms high + 2.25ms low + 0.56ms high + 96.19ms low

Connection circuit diagram:

Infrared decoding circuit diagram

Let's start writing the program:

#ifndef MAIN_H
#define MAIN_H
#include

#define bitset(var,bitno)(var |=1< #define bitclr(var,bitno)(var &=~(1<


#define uint unsigned int 
#define uchar unsigned char

//Define the receiving frame 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 delay function
//void Delay10US(uint us);//10 microsecond delay function
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)//Prevent interference
     frame.ir_state=IR_START ;
    break ;
   case IR_START :
    if(frame.ready==1)//No processing, waiting
    {
     frame .ir_state=IR_IDLE ;
     return ;
    }
    if((frame.count>ms_125)&&(frame.count     {
     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     {
     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 bits 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 bits 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 bits finished?
    {
     frame.temp=frame.temp<<1;
     int_delay(75);//Delay waiting for the level of the last bit
     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;//Turn on 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 serial port
 while(1) 
 {

  if(frame.ready==1)
  {
   put_char(frame.data) ;
   frame.ready=0 ;
  }

 } 
}

Reference address:Infrared remote control and decoding of PIC microcontroller

Previous article:PIC drives JM240128 LCD
Next article:PIC16F877A drives DS18B20 temperature acquisition chip

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号