Proteus8 simulation: the use of 51 single-chip IrLink infrared sending and receiving modules

The use of 51 single-chip microcomputer IrLink infrared

Note: There is a problem with the password acceptance in this article. If it is not enough, it can still be used. If you want to optimize it, you can read my latest article: The latest optimization . This is a bug-free version.

components

components name
51 MCU AT89C51
Infrared transceiver IRLINK
button BUTTON
led LED-RED
Clock Excitation Source DCLOCK
AND gate 74LS08
oscilloscope

Schematic part

insert image description here
Regarding the use of IRLINK:
Proteus is a module that integrates infrared transmitter and receiver, and there is a demodulation function on this module's infrared receiver. As shown in the figure below, area 1 is the infrared emitting area, area 2 is the receiving area of ​​infrared, and area 3 is the area for demodulation after receiving infrared.
insert image description here

Then, regarding the frequency of the carrier, you can choose the requirement for the carrier frequency in IRLINK, double-click the component. The default is 40KHz.
insert image description here
The frequency used here is the Proteus excitation source and then modulated with the signal from the single chip microcomputer through the AND gate. The figure below is the result of observing the signal with an oscilloscope.
insert image description here
Regarding the launch protocol, Sony's SonySIRC protocol is used online. This experiment uses a similar method, with 2.4ms high level plus 600us low level as the starting signal, 600us as the interval, 600us as the high level means 0, 1200us as the interval, 600us as the high level means 1, the data frame It is: the frame header is 0x80, the data frame is 0x40, and the frame tail is 0x20. If 0x40 is received and the frame header and frame tail are not wrong, light up. This time the irlink carrier frequency is 20kHz.

the code

In this experiment, the single-chip microcomputer controls the IrLink infrared through the buttons to communicate and light the lights.

MCU 1 sends main.c

code show as below:

#include "reg51.h"
#include "intrins.h"

typedef unsigned char uint_8;
typedef unsigned int uint_16;

sbit TX=P2^0;
sbit KEY=P1^0;

//600us
#define High_Low_Wide_H 0XFD 
#define High_Low_Wide_L 0xA8 

//600us
#define Low_Gap_H 0XFD
#define Low_Gap_L 0xA8

//1200us
#define High_Gap_H 0xFB
#define High_Gap_L 0x50

//2400us
#define Start_High_H 0xF6
#define Start_High_L 0xA0

void scan_Key(void); //按键扫描函数
void Ir_Send_Bit(uint_16 us_h,uint_16 us_l,bit Status);
void Ir_Send_Data(uint_8 Data);
void TimerInit(void);
void main(void)
{
    
    
	bit key=0;
	TimerInit();TX = 0;
	while(1)
	{
    
    
		scan_Key();
	}
}

//按键扫描函数
void scan_Key(void)
{
    
    
	if(KEY==0)
	{
    
    
		TX = 0;
		Ir_Send_Bit(Start_High_H,Start_High_L,1); //起始信号2.4ms
		Ir_Send_Bit(Low_Gap_H,Low_Gap_L,0); //间隔输出0.6ms
		Ir_Send_Data(0x80);//起始帧0x80
		Ir_Send_Data(0x40); //数据
		Ir_Send_Data(0x20);//终止帧
	}
}

//一位发送
void Ir_Send_Bit(uint_16 us_h,uint_16 us_l,bit Status) 
{
    
    
	TH0 = us_h;		//设置定时初值
	TL0 = us_l;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	if(Status==1)
	{
    
    
		TX = 1;
		while(TF0==0);
	}
	else if(Status==0)
  {
    
    
		TX = 0;
		while(TF0==0);
	}
	TF0 = 0;
	TR0 = 0;  //关闭定时器
	TX = 0;
}

//八位发送 从最低位往高位发送
void Ir_Send_Data(uint_8 Data)
{
    
    
	uint_8 i=0;
	for(i=0;i<8;i++)
	{
    
    
		if(Data&0x01) //高电平
		{
    
    
			Ir_Send_Bit(High_Low_Wide_H,High_Low_Wide_L,1); //脉宽输出
			Ir_Send_Bit(High_Gap_H,High_Gap_L,0); //间隔输出
		}
		else
		{
    
    
			Ir_Send_Bit(High_Low_Wide_H,High_Low_Wide_L,1); //脉宽输出
			Ir_Send_Bit(Low_Gap_H,Low_Gap_L,0); //间隔输出			
		}
		Data >>=1;
	}
}

void TimerInit(void)		//12微秒@12.000MHz
{
    
    
	TMOD=0X01;		//设置定时器模式
}

MCU 2 accepts main.c

#include "reg51.h"
#include "intrins.h"

typedef unsigned char uint_8;
typedef unsigned int uint_16;

sbit RX=P2^1;
sbit LED=P1^0;

void Ir_Receive(void);
uint_8 Ir_Receive_8Bit(void);
void TimerInit(void);
uint_8 All_Data[3]={
    
    0,0,0};
uint_16 time=0;

void main(void)
{
    
    
	TimerInit();
	while(1)
	{
    
    	
		if((All_Data[1]==0x40)&&(All_Data[0]==0x80)&&(All_Data[2]==0x20))LED=1;
		else LED=0;
		Ir_Receive();
	}
}


//接受数据
void Ir_Receive(void) 
{
    
    
	uint_8 i=0;
	if(RX==0)
	{
    
    
		time=0;
		TR0 = 1;		//定时器0开始计时
		while(RX==0);
		time=TH0*256+TL0; //计算时间
		TR0 = 0;
		
		if((time>2000)&&(time<3000)) 
		{
    
    
			
			TH0=0;TL0=0;	
			time=0;
			TR0 = 1;		//定时器0开始计时
			while(RX==1);
			time=TH0*256+TL0; //计算时间			
			TR0 = 0;
			
			if((time>300)&&(time<1000)) 
			{
    
    
				
				time=0;
				while(i<3)
				{
    
    
					All_Data[i]=Ir_Receive_8Bit();
					i++;
				}
			}
		}
	}
}

//8位接受
uint_8 Ir_Receive_8Bit(void)
{
    
    
	uint_8 Data=0;
	uint_8 i=0;
	while(i<8)
	{
    
    
		if(RX==1)
		{
    
    
			TH0=0;TL0=0;	
			time=0;
			TR0=1;
			while(RX==1);
			time=TH0*256+TL0; //计算时间
			TR0 = 0;
			
			TF0=0;
				
			if((time>300)&&(time<1000)) Data>>=1;
			else if(time>1000) Data |=0x80;
			i+=1;
		}
	}
	return Data;
}

void TimerInit(void)		//12微秒@12.000MHz
{
    
    
	TMOD=0X01;		//设置定时器模式
	TH0=TL0=0;
	TR0 = 0;
}

The running effect is as shown in the figure, and the led is normally lit:
insert image description here

Project Files

Project Files

Guess you like

Origin blog.csdn.net/darlingqx/article/details/128371855