[51-based multi-functional smart car]

51-based multifunctional smart car


foreword

》 It's the school smart car competition once again in the semester. I remembered that the last time I participated in it was really good and I directly burned the code of the merchant and went straight to it. It can be known that the final result is also... (because the case code given by the merchant is just according to him I have adjusted my own needs, there are some differences on the track given by the school, and I still need to modify it slowly)
"I am a senior now, and I also learned to cherish every competition, no matter what the result is, I have to do my best. Face it well, so this competition is also based on the existing part of the merchants. I seriously modified and added other functions, and realized sub-module programming in the code writing process to better improve the portability of the code.
》 Again, although most of the code merchants of these functions are provided, but you can only practice and integrate all of them and realize the desired functions, you will find that it is not that simple.
》In the shared code, you can use the timer to change the duty cycle by adding a timer without implementing other functions.


1. Introduction of multi-functional smart car

The smart car mainly has the following functions:
》Tracking function (tracking according to the specified black line track)
》Ultrasonic obstacle avoidance
》Infrared remote control
》Bluetooth control

Second, the software part

2.1 开发平台 keil5
insert image description here

2.2 下载软件
Using the usb to ttl download method (the download needs to be used in conjunction with this software)
insert image description here

2.3 Notepad++代码查看工具
insert image description here

Second, the hardware part

Bluetooth module 1
USB downloader 1
A set of smart trolley kits (spread) 1

Because the smart car kit purchased by the school includes all the hardware used, not all of them are listed.

3. Programming

主程序

/*******************************************************
*	多功能智能小车
*	Author:小殷同学
*	Date:2022-04-4
********************************************************/

#include "reg52.h"
#include "motor.h"
#include "uart.h"	 
#include "csb.h"
#include "delay.h"
#include "key.h"
#include "smg.h"
#include "track.h"		
//#include "time.h"


sbit IRIN=P3^3;                           //定义红外接收端口
extern unsigned char cmd; 						 //蓝牙接收命令变量
extern unsigned char keyvalue;				 //键值
unsigned char cnt = 0;								 //计时
extern unsigned char speed;							//速度
unsigned char code  RecvData[]={
    
    0x19,0x46,0x15,0x43,0x44,0x40,0x0D,0x0E,0x00,0x0F};//红外数据
unsigned char IRCOM[7];



/********************************主函数***********************************/
void main(void)
{
    
    
	Init_Uart();	 //串口初始化
	Time0Init();   //定时器初始化
	Init_Key();		 //按键初始化
 	while(1)
	{
    
    
		 
		SMG_Display(keyvalue);	 //获取按键键值
		switch(keyvalue)
		{
    
    
		  case 0:ControlCar(8);break;		 //停止
		 	case 1:CSB_Tackle();EX1 = 0;break;	 //超声波避障
			case 2:Car_Traction();EX1 = 0;break;	//循迹
			case 3:Cmd_Tackle();EX1 = 0;break;	//蓝牙
			case 4:EX1 = 1;break;								//红外
		}
	}
}



/******************************串口中断处理函数***************************/
void Server_Uart() interrupt 4
{
    
    
 	if(RI == 1) //接收完成
	{
    
    
		RI = 0; //清除标志
		cmd = SBUF;
	}
}



/*******************************外部中断0**********************************/
void Server_Exit() interrupt 0
{
    
    
 	keyvalue++; 
	if(keyvalue > 4)
	{
    
    
	 	keyvalue = 0;
	}
}


/*
//中断处理函数
void Server_Timer1() interrupt 3
{
	//重装初值
 TL0 = 0x33;
 TH0 = 0xFE;
 cnt++;
 if(cnt < speed)
 {
		//前进
		GoForward();
 }
 else
 {
		//停止
		Stop();
 }

 if(cnt == 40) //20ms
 {
		cnt = 0;
 }
}
*/

//----------红外遥控-------------------------------------------------------------
void IR_IN() interrupt 2                     //定义INT2外部中断函数
{
    
    
  unsigned char j,k,N=0;                              //定义临时接收变量
   
  EX1 = 0;                                            //关闭外部中断,防止再有信号到达   
  delayms(15);                                        //延时时间,进行红外消抖
  if (IRIN==1)                                        //判断红外信号是否消失
  {
    
      
     EX1 =1;                                          //外部中断开
	 return;                                          //返回
  } 
                           
  while (!IRIN)                                       //等IR变为高电平,跳过9ms的前导低电平信号。
  {
    
    
      delayms(1);                                     //延时等待
  }

  for (j=0;j<4;j++)                                   //采集红外遥控器数据
  {
    
     
    for (k=0;k<8;k++)                                 //分次采集8位数据
    {
    
    
       while (IRIN)                                   //等 IR 变为低电平,跳过4.5ms的前导高电平信号。
       {
    
    
         delayms(1);                                  //延时等待
       }
       
       while (!IRIN)                                  //等 IR 变为高电平
       {
    
    
         delayms(1);                                  //延时等待
       }
   
       while (IRIN)                                   //计算IR高电平时长
       {
    
    
         delayms(1);                                  //延时等待
         N++;                                         //计数器加加
         if (N>=30)                                   //判断计数器累加值
	     {
    
     
           EX1=1;                                     //打开外部中断功能
	       return;                                    //返回
         }                   
       }
                                       
      IRCOM[j]=IRCOM[j] >> 1;                         //进行数据位移操作并自动补零
     
      if (N>=8)                                       //判断数据长度 
      {
    
    
         IRCOM[j] = IRCOM[j] | 0x80;                  //数据最高位补1
      } 
      N=0;                                            //清零位数计录器
    }
  }
   
  if (IRCOM[2]!=~IRCOM[3])                            //判断地址码是否相同
  {
    
     
     EX1=1;                                           //打开外部中断
     return;                                          //返回
  }

  for(j=0;j<10;j++)                                   //循环进行键码解析
   {
    
    
      if(IRCOM[2]==RecvData[j])                       //进行键位对应
      {
    
    
       // ControlCar(j);
		ControlCar_yaokong(j);                               //数码管显示相应数码
      }
   } 
   EX1 = 1;                                           //外部中断开 
} 

Engineering link:
https://pan.baidu.com/s/1JlimBOELy1eYHz0p1veeSg
Extraction code: f3ka
– sharing from Baidu Netdisk Super Member V4

Guess you like

Origin blog.csdn.net/boybs/article/details/123524590