Infrared remote control car

Infrared remote control car, you can use infrared remote control within a certain range the car forward, backward, turn left, turn right, you can also control various peripherals small car ...
achieve infrared remote control car is delivered mainly by infrared signal, car the decoded value obtained will be specific to the infrared signal received, then the car is performed based on an operation corresponding to the respective values.
Remote control car has two main modules:

  • IR receiver module
  • Motor drive module

IR receiver module

Small car An infrared receiving device, infrared receiver unit is responsible for receiving the infrared remote control signal is decoded, the car operating in accordance with the appropriate command, there are about infrared remote control related operations and how to decode bloggers are detailed in a previous blog, introduction link: infrared communication
using infrared signals received interrupt, after receiving the interrupt signal processing function, the following function is interrupted configuration (initialization):

sbit IR=P3^2;
/* 配置中断 */
void init()		 //初始化
{
IT0=0;
EX0=1;
EA=1;
IR=1;			 //相当于打开外部中断0  此处意义为红外接收器打开
}

Receives an interrupt (infrared signal) after entering the interrupt function of the decoded signal:

void hhh() interrupt 0   //执行中断:进行读取信号  最后产生键值
{
uint i,j,k;
dy1(700);
if(IR==0)
{
k=1000;
while(IR==0&&(k>0))	  //通过此while来实现计时
{
dy1(1);
k--;
}
if(IR==1)
{
k=500;
while(IR==1&&(k>0))			  //检测初始高电平
{
dy1(1);
k--;
}
for(i=0;i<4;i++)			//开始读取用户码和操作码和操作反码
{
	for(j=0;j<8;j++)
	{
	 if(IR==0)
	 {
	 k=70;
	 while(IR==0&&(k>0))
	 {
	 k--;
	 dy1(1);
	 }
	 }
	 if(IR==1)
	 {
	 time=0;
	 k=500;
	 while(IR==1&&k>0)
	 {
	 dy1(10);
	 time++;
	 k--;
	 }
	 if(time>30) return ;
	 irdata[i]>>=1;		 //移位运算,空出最高为为下一次采集做准备
	 if(time>=8)		//高电平---或运算0x80给最高位(j位)赋1
	 irdata[i]|=0x80;
	 time=0;
	 }
	}
}
}
if(irdata[2]!=~irdata[3])
return;
}
switch(irdata[2])
{
case 0x16:jz=0;break;
case 0x0c:jz=1;break;
case 0x18:jz=2;break;
case 0x5e:jz=3;break;
case 0x08:jz=4;break;
case 0x1c:jz=5;break;
case 0x5a:jz=6;break;
case 0x42:jz=7;break;
case 0x52:jz=8;break;
case 0x4a:jz=9;break;
case 0x45:jz=10;break;
default:break;
}
jy=jz;
}

jy is a global variable defined, that returns the decoded value of the infrared signal, the global variable is to rely on the program to control the car. Explain in detail about the code of bloggers have already spoken in a previous article, infrared communication, no longer cumbersome narrative here.

Motor Module

The rest is a motor-driven, relevant content related to motor module are described in detail, another link in the blog post: motor
here is to use a different switch statement, the appropriate action depending on the infrared signal codes as follows :

void dy(uint x)				  //一组循环约为0.1秒
{
	uint i,j;
	for(i=0;i<x;i++)
		for(j=11400;j>0;j--)
			;
}

void car_advance(uint x)
{
	left_en=1;
	right_en=1;
	left_advance=1;
	left_back=0;
	right_advance=1;
	right_back=0;
	dy(x);
}

void car_back(uint x)
{
	left_en=1;
	right_en=1;
	left_back=1;
	left_advance=0;
	right_back=1;
	right_advance=0;
	dy(x);
}

void car_left(uint x)						 //为什么左转时只有right_back=0 其余的都为1?
{
	left_en=0;
	right_en=1;
	left_advance=1;
	left_back=1;
	right_advance=1;
	right_back=0;
	dy(x);
}

void car_right(uint x)
{
	left_en=1;
	right_en=0;
	left_advance=1;
	left_back=0;
	right_advance=0;
	right_back=0;
	dy(x);
}

void car_stop(uint x)
{
	left_en=0;
	right_en=0;
	dy(x);
}

void main()
{
	init();
	while(1)
	{
		while(jy==jz)
		{
		switch(jz)
		{
			case 2:car_advance(3);break;
			case 8:car_back(3);break;
			case 4:car_left(3);break;
			case 6:car_right(3);break;
			case 5:car_stop(3);break;
		}
		jy--;
		}
	}
}
Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/99748079