基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)

一:硬件的选择

1.底盘和电机
底盘选择两驱底盘套装,2个直流减速电机
2.电机驱动模块
L9110电机驱动模块单H桥电路
3.单片机
51单片机,芯片选择的是:STC89C52芯片
4.蓝牙模块
HC-05蓝牙
5.温湿度感应器
DHT-11温湿度感应器
6.循迹和自动避障
4路红外循迹模块
7.电源我们选择的是小型充电宝
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第1张图片
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第2张图片

二:各模块的实现

1.自动避障
当模块检测到前方障碍物信号时,电路板上红色指示灯点亮,同时 OUT端口持续输出低电平信号
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第3张图片

基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第4张图片

#include
sbit left1=P1^0;
sbit left0=P1^1;
sbit right1=P1^2;
sbit right0=P1^3;
sbit out1 = P1^4;
sbit out2 = P1^5;
void forward()
{
	left1=1; 
	left0=0; 
	right1=1; 
	right0=0;
}
void back()
{
	left1=0; 
	left0=1; 
	right1=0; 
	right0=1; 
}
void left()
{
	left1=0; 
	left0=1; 
	right1=1; 
	right0=0;
}
void right()
{
	left1=1; 
	left0=0; 
	right1=0; 
	right0=1;
}
void stop()
{
	left1=0;
	left0=0;
	right1=0;
	right0=0;
}

int cnt;

void time0_init()
{
	TMOD = 1;
	TH0 = 0xFC;
	TL0 = 0x18;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}

void time0_isr() interrupt 1
{
	TH0 = 0xfc;
	TL0 = 0x18;
	if(cnt>0)
{
  	cnt--;
}
}

void delay(int x)
{
    cnt = x;
	time0_init();
    while(cnt>0);
}
void main()
{
	while(1)
 {
	if(out1 == 1 && out2 == 1) 
		{
			forward();
			delay(100);
		}
	else if(out1 == 0 && out2 == 0)
		{
			back();
			delay(500);
		}
	else if(out2 == 0 && out1 == 1) 
		{
			right();
			delay(500);
		}
	else if(out1 == 0 && out2 == 1) 
		{
			left();
			delay(500); 
		}
 }
}

2.自动循迹
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第5张图片

#include 
typedef unsigned int u16;
typedef unsigned char u8;

sbit left1=P1^0;
sbit left0=P1^1;
sbit right1=P1^2;
sbit right0=P1^3;

u16 flag;
sbit l1=P2^4; 

sbit l2=P2^3;
sbit r2=P2^2;
sbit r1=P2^1;

void forward() 
{
	left1=1; 
	left0=0; 
	right1=1; 
	right0=0;
}

void turnLeft1()
{
	left1=0; 
	left0=0; 
	right1=1; 
	right0=0;
}

void turnLeft2()
{
	left1=0; 
	left0=1; 
	right1=1; 
	right0=0;
}

void turnRight1()
{
	left1=1; 
	left0=0; 
	right1=0; 
	right0=0;
}

void turnRight2()
{
	left1=1; 
	left0=0; 
	right1=0; 
	right0=1;
}

void stop() 
{
	left1=0;
	left0=0;
	right1=0;
	right0=0;
}

void follow() 
{
	if((r2 == 0)&&(r1 == 0)&&(l1 == 0)&&(l2 == 0)
	||(r2 == 1)&&(r1 == 1)&&(l1 == 1)&&(l2 == 1)
	||(r2 == 1)&&(r1 == 0)&&(l1 == 0)&&(l2 == 1))
	{
		flag = 0;
	}
	if((r2 == 1)&&(r1 ==0)&&(l1 == 0)&&(l2 == 0))
	{
		flag = 1;
	}
	if((r2 == 0)&&(r1 == 0)&&(l1 == 0)&&(l2 == 1))
	{
		flag = 2;
	}
	if((r2 == 0)&&(r1 == 0)&&(l1 == 1)&&(l2 == 0)
	||(r2 == 0)&&(r1 == 0)&&(l1 == 1)&&(l2 == 1))
	{
		flag = 3;
	}
	if(r2 == 0)&&(r1 == 1)&&(l1 == 0)&&(l2 == 0)
	||(r2 == 1)&&(r1 == 1)&&(l1 == 0)&&(l2 == 0))
	{
		flag = 4;
	}
	switch(flag)
	{
		case 0:forward();break;
		case 1:stop();turnRight1();break;
		case 2:stop();turnLeft1();break;
		case 3:stop();turnLeft2();break;
		case 4:stop();turnRight2();break;
		default:stop();break;
	}
}
void main()
{
	TMOD=0x02;                     
	TH0=220;              
	TL0=220;
	TR0=1;
	ET0=1;  
	EA=1;
	while(1)
	{
	follow(); 
	}
}

3.蓝牙控制
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第6张图片
百度网盘HC-05蓝牙资料、app:https://pan.baidu.com/s/1_5Tr4enAQ1eP34PuzYZSvw
(提取码1027)

4.#include<reg52.h>
#include
#define uchar unsigned char
#define uint  unsigned int
sbit left1=P1^0;
sbit left0=P1^1;
sbit right1=P1^2;
sbit right0=P1^3;
void delay_ms(uint time)
{
	uchar i;
	while(time--)
	{
		for(i = 0;i < 113;i++);
	} 
}

void forward()
{
	left1=1; 
	left0=0; 
	right1=1; 
	right0=0;
}
void back()
{
	left1=0; 
    left0=1; 
	right1=0; 
	right0=1; 
}
void left()
{
	left1=0; 
	left0=1; 
	right1=1; 
	right0=0;
}
void right()
{
	left1=1; 
	left0=0; 
	right1=0; 
	right0=1;
}
void stop()
{
	left1=0;
	left0=0;
	right1=0;
	right0=0;
}
void UsartConfiguration();
void Com_Int(void) interrupt 4
{
  uchar receive_data;
  if(RI == 1) 
{  
	RI=0;
	left0=0;
	left1=0;
	right1=0;
	right0=0;
	receive_data = SBUF;
	if(receive_data == '1')  
	{
		forward();
	} 
	else if(receive_data == '2')
	{
		back();
	}
	else if(receive_data == '3')
	{
		left();
		delay_ms(300);
		forward();
	}
	else if(receive_data == '4')
	{
		right();
		delay_ms(300);
		forward();
	}
	else if(receive_data == '0')
	{
		stop();
	}
}
EA = 1;
}
void main() 
{
	UsartConfiguration();
	while(1); 
}
void UsartConfiguration()
{ 
	SCON=0X50; 
	TMOD=0X20; 
	PCON=0X00; 
	TH1=0XFd;     
	TL1=0XFd;
	TR1=1; 
	ES = 1;        
    EA = 1;         
}

4.温湿度显示
主机通过拉高拉低电平,给DHT11一个起始信号,然后DHT11通过拉低拉高电平,反馈主机可以开始接受数据,接受数据开始后,DHT11先拉低电平再拉高电平,通过高电平的持续时间来判断是“0”还是“1”
基于51单片机的蓝牙智能小车(循迹、避障、温湿度显示)_第7张图片
百度网盘DHT-11资料:(提取码1027)
https://pan.baidu.com/s/1oXMK1_ZSH9V60UlckmQULg

#include 
#include
#define uchar unsigned char 
#define uint unsigned int
sbit DHT11_DATA = P3^7;
sbit LED_ERROR = P1^7;
sbit LED_RIGHT = P1^6;
sbit WEI0 = P1^0;
sbit WEI1 = P1^1;
sbit WEI2 = P1^2;
sbit WEI3 = P1^3;
uint flag=0;
uint t;
uchar over_time_counter;
uchar RH_data_H,RH_data_L,TEM_data_H,TEM_data_L,check_data;
uchar TEM_data_H_temp,TEM_data_L_temp,RH_data_H_temp,RH_data_L_temp,check_data_temp;
uchar byte_value;
char table[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x7f,0xC6,0x89};
void init_int(void)
{
	IT1 = 1;
	EX1 = 1;
	EA = 1;
}
void init_time0(void)
{
	TMOD = TMOD & 0xF0;
	TMOD = TMOD | (1 << 0);
	TH0 = (65536 - 50000) / 256;
	TL0 = (65536 - 50000) % 256;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}
void UsartConfiguration()
{	
	SCON=0X50;			
	TMOD=0X20;			
	PCON=0X00;		
	TH1=0XFd;		    
	TL1=0XFd;
	TR1=1;					
	ES = 1;        
    EA = 1;         
}

void delay_ms(uint time)
{
	uchar i;
	while(time--)
	{
		for(i = 0;i < 113;i++);
	}	
}
void delay_10us(uchar n)
{
	while(n--);
}
void display(int q,int b,int s,int g)
{
	P0 = table[g];
	WEI0 = 0;
	delay_ms(1);
	WEI0 = 1;
	
	P0 = table[s];
	WEI1 = 0;
	delay_ms(1);
	WEI1 = 1;
	
	P0 = table[b];
	WEI2 = 0;
	delay_ms(1);
	WEI2 = 1;
	
	P0 = table[10];
	WEI2 = 0;
	delay_ms(1);
	WEI2 = 1;
	
	P0 = table[q];
	WEI3 = 0;
	delay_ms(1);
	WEI3 = 1;
}
void Read_Byte(void)
{
	uint i;
	uchar bit_value;
	for(i = 0;i < 8;i++)
	{
		over_time_counter = 2;
		DHT11_DATA = 0;
		while((!DHT11_DATA) && over_time_counter++);
		delay_10us(3);
		if(DHT11_DATA)
		{
			bit_value = 1;
		}
		else
		{
			bit_value = 0;
		}
		over_time_counter = 2;
		while(DHT11_DATA && over_time_counter++);
		if(over_time_counter == 1)
		{
			LED_ERROR = 0;
			LED_RIGHT = 1;
			break;
		}
		byte_value = byte_value << 1;
		byte_value = byte_value | bit_value;	
	}
}

void read_dht11(void)
{
	uchar calc_check_data; 
	DHT11_DATA = 0;
	delay_ms(20);
	DHT11_DATA = 1;
	delay_10us(4);
	DHT11_DATA = 1;
	if(!DHT11_DATA)
	{
		over_time_counter = 2;
		while((!DHT11_DATA) && over_time_counter++);
		over_time_counter = 2;
		while((DHT11_DATA) && over_time_counter++);
		Read_Byte();
		RH_data_H_temp = byte_value;
		Read_Byte();
		RH_data_L_temp = byte_value;
		Read_Byte();
		TEM_data_H_temp = byte_value;
		Read_Byte();
		TEM_data_L_temp = byte_value;
		Read_Byte();
		check_data_temp = byte_value;
		DHT11_DATA = 1;
		calc_check_data = (RH_data_H_temp + RH_data_L_temp + TEM_data_H_temp + TEM_data_L_temp);
		if(calc_check_data == check_data_temp)
		{
			RH_data_H = RH_data_H_temp;
			RH_data_L = RH_data_L_temp;
			TEM_data_H = TEM_data_H_temp;
			TEM_data_L = TEM_data_L_temp;
			check_data = check_data_temp;
			LED_ERROR = 1;
			LED_RIGHT = 0;
		}
	}	
}

void main()
{
	int i;
	uchar TS,TG,TSF;
	uchar RS,RG,RSF;
	read_dht11();
	UsartConfiguration();
	init_int();
	init_time0();
	while(1)
	{
		//read_dht11();
		RS=RH_data_H/10;
		RG=RH_data_H%10;
		RSF=RH_data_L/10;
		TS=TEM_data_H/10;
		TG=TEM_data_H%10;
		TSF=TEM_data_L/10;
		for(i=0;i<500;i++)
		{
			if(flag==0)
				display(TS,TG,TSF,11);
			else 
				display(RS,RG,RSF,12);
		}
	}
}
void TIME0_SERVER() interrupt 1
{
	TR0 = 0;
	TH0 = (65536 - 50000)/256;
	TL0 = (65536 - 50000)%256;
	t++;
	if(t == 30)
	{
		read_dht11();
		t = 0;
	}
	TR0 = 1;1
}

你可能感兴趣的:(硬件,单片机,蓝牙)