「51 single chip microcomputer」infrared receiving and transmitting module code

Publisher:kappa20Latest update time:2017-01-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction

Infrared transmitter module: Power supply 3.3V or 5V, transmits 38KHz infrared signal through the transmitter tube, high level drive

Infrared receiving module: Power supply 3.3V or 5V, can receive infrared signals and demodulate them into logic levels, low level is effective

 

2. Circuit Diagram

                                 Infrared transmitter circuit diagram

 

                                    Infrared receiving circuit diagram

 

3. Infrared transmitter module code


#include

#include


#define uchar unsigned char

#define uint  unsigned int


//Pin definition

sbit key = P2^0;

sbit out = P2^1;


// Function declaration

void delayms(uchar aa); //Infrared emission delay

void khz(uchar aa); //Transmit 38KHz

void IR_OUT(uchar num); //infrared emission

void keyScan(); //Read keyboard information


//Variable declaration

fly i, a, num1;


//----------------------- 

//Infrared emission delay

//-----------------------

void delays(fly aa)

{

    for(a=aa; a>0; a--)

    {

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

        _nop_();

    }

}


//----------------------- 

//Transmit 38KHz

//-----------------------

void khz(uchar aa)

{

    for(a=aa;a>0;a--) //This for statement can get the accurate 26.3 baud rate

    {

        out=0;

        i=7;            

        while(i>0) i--;    //38kHZ  

        out=1;            

    }


}


//----------------------- 

//Infrared emission

//-----------------------

void IR_OUT(uchar num)

{

    khz(116); //transmit 3ms 38khz

    delayms(125);

    for(num1=8; num1>0; num1--) 

    {

        khz(40);

        if(num&0x01)

            delayms(93); //delay 1.5ms

        else

            delayms(65); //delay 1ms

        num=num>>1;

    }

    khz(20);

}


//----------------------- 

//Read keyboard information

//-----------------------

void keyScan()

{

    if(key==0)

    {

        _nop_(); _nop_(); _nop_();

        _nop_(); _nop_(); _nop_();

        if(key==0)

            IR_OUT(0xC0);

     key = 1;

    }

}


//----------------------- 

//Main function

//-----------------------

void main()

{

    key = 1;

    while(1)

    {

        keyScan();    

    }

}

 


4. Infrared receiving module code



#include

#include


#define uchar unsigned char

#define uint  unsigned int


//Pin definition

sbit in = P3^2; //Infrared interrupt reception

//P2 connects to static digital tube


// Function declaration

void init(); //initialization function

void delayms(uchar j); //delay function


//Variable declaration

fly i, num, a;

bit fleg;


//----------------------- 

// Initialization function

//-----------------------

void init()

{

    fleg = 1; //flag

    in = 1; //infrared receiving

    EA = 1; //Interrupt setting

    EX0  = 1;

    IT0  = 1;

}


//----------------------- 

//Delay function

//-----------------------

void delays(fly j)

{

    for(i=j; i>0; i--)

    {

        _nop_(); _nop_(); _nop_();

        _nop_(); _nop_(); _nop_();

    }    

}//----------------------- 

//Main function

//-----------------------

void main()

{

    heat();

    while(1);

}


//----------------------- 

//Receive interrupt

//-----------------------

void sieasdf() interrupt 0

{

    EX0 = 0;

    for(a=5; a>0; a--)

    {

        delayms(35); //delay 0.5ms, judge 5 times 5*0.5=2.5ms

        if(in) fleg=0;

    }

    if(fleg)

    {

        delayms(72); // Delay 1ms to determine whether it is a high level

        if(in)

        {

            delayms(115); //delay to make it longer than 2ms; 2.5+1+1.623=5.123ms to start reading data

            delayms(118); //If it is offset by one bit, it can be removed.

            for(a=8; a>0; a--)

            {

                while(!in);

                delayms(86); //Delay 1.188ms to judge IO high or low, and get 0 or 1

                num = num>>1;

                if(in)

                    {

                        num = num|0x80;

                        delayms(31); // Delay 0.6ms because the delay above is 1.2ms+0.6, just skipping 1.5ms

                    }

            }    

            P2 = whether;        

        }

    }

    flag = 1;

    EX0 = 1;

}


Reference address:「51 single chip microcomputer」infrared receiving and transmitting module code

Previous article:「51 MCU」Basic usage of Bluetooth slave
Next article:「51 single chip microcomputer」transmitter and receiver ultrasonic ranging module analysis + code

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号