欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

蓝牙小车

程序员文章站 2022-06-08 20:55:53
...

实物图:
蓝牙小车
蓝牙小车代码:

#include<reg52.h>
typedef unsigned int u16;
typedef unsigned char u8;

sbit IN1 = P2^1;
sbit IN2 = P2^2;
sbit IN3 = P2^3;
sbit IN4 = P2^4;
sbit ENA = P2^0;  
sbit ENB = P2^5;

sbit RUN = P1^3;  //前进灯

sbit SF1 = P1^6;  //左转向灯
sbit SF2 = P1^5;
sbit SF3 = P1^4;

sbit SR1 = P1^0;  //右转向灯
sbit SR2 = P1^1; 
sbit SR3 = P1^2; 

u8 bluetooth;
u16 PWMCnt1 = 0;
u16 PWMCnt2 = 0;
u16 cntPWM1 = 0;
u16 cntPWM2 = 0;
bit forward_way=0,backward_way=0,left_way=0,right_way=0,stop_way=0;

void Timer0Init()
{
	TH0 = 0xFF;//定时0.1ms
	TL0 = 0xA3;
	TMOD &= 0xF0;
	TMOD |= 0X01;
	EA = 1;
	ET0 = 1;
	TR0 = 1;	
}

void UartInit()
{	
	TH1 = 0XFA;	 		
	TL1 = 0XFA;
	SCON = 0X50;
	PCON = 0x80;
	TMOD &= 0x0F;
	TMOD |= 0X20;				
	ES = 1;					
	EA = 1;					
	TR1 = 1;						
}

void Forward()                                          //前进
{
	IN1 = 1;   //左轮正传
	IN2 = 0;
	IN3 = 1;   //右轮正传
	IN4 = 0;
	cntPWM2 = 50;
	cntPWM1 = 50;

	RUN = 1;   //前进灯亮
	SF1 = 0; SF2 = 0; SF3 = 0;	//左转向灯不亮
	SR1 = 0; SR2 = 0; SR3 = 0;  //右转向灯不亮
}
void Backward()	                                        //后退
{
	IN1 = 0;  //左轮反转
	IN2 = 1;
	IN3 = 0;  //右轮反传
	IN4 = 1;
	cntPWM1 = 50;
	cntPWM2 = 50;

	RUN = 1;  //前进灯亮
	SF1 = 0; SF2 = 0; SF3 = 0;	 //左转向灯不亮
	SR1 = 0; SR2 = 0; SR3 = 0;	 //右转向灯不亮
}
void Turnleft()	                                        //左转
{
	SF1 = ~SF1; SF2 = ~SF2; SF3 = ~SF3;//左转向灯闪烁

	IN1 = 0;  //左轮不转
	IN2 = 0;
	IN3 = 1;  //右轮正传
	IN4 = 0;
	cntPWM1 = 10;
	cntPWM2 = 50;

	RUN = 0;   //前进灯灭
	SR1 = 0; SR2 = 0; SR3 = 0;	  //右转向灯灭
}
void Turnright()                                        //右转
{
	SR1 = ~SR1; SR2 = ~SR2; SR3 = ~SR3;	//右转向灯闪烁

	IN1 = 1; //左轮正转
	IN2 = 0;
	IN3 = 0; //右轮不传
	IN4 = 0;
	cntPWM1 = 50;
	cntPWM2 = 10;

	RUN = 0;  //前进灯灭
	SF1 = 0; SF2 = 0; SF3 = 0;	  //左转向灯灭
}
void Stop()	                                           //停止
{
	IN1 = 0;  //左轮不转
	IN2 = 0;
	IN3 = 0;  //右轮不转
	IN4 = 0;
	cntPWM1=0;
	cntPWM2=0;

	RUN = 0;
	SF1 = 0; SF2 = 0; SF3 = 0;
	SR1 = 0; SR2 = 0; SR3 = 0;	  //所有灯灭
}
void Display()
{
	if(forward_way==1)
	{
		Forward();
	}
	else if(backward_way==1)
	{
		Backward();
	}
	else if(left_way==1)
	{
		Turnleft();
	}
	else if(right_way==1)
	{
		Turnright();
	}
	else if(stop_way==1)
	{
		Stop();
	}	
	
}
void main()
{
	Timer0Init();
	UartInit();
	while(1)
	{
  		Display();
	}
}
void Link()
{
	if(bluetooth=='a')
	{
		forward_way = 1;
		backward_way = 0;
		left_way = 0;
		right_way = 0;
		stop_way = 0;
	}
	else if(bluetooth=='b')
	{
		forward_way = 0;
		backward_way = 1;
		left_way = 0;
		right_way = 0;
		stop_way = 0;
		
	}
	else if(bluetooth=='c')
	{
		forward_way = 0;
		backward_way = 0;
		left_way = 1;
		right_way = 0;
		stop_way = 0;
	}
	else if(bluetooth=='d')
	{
		forward_way = 0;
		backward_way = 0;
		left_way = 0;
		right_way = 1;
		stop_way = 0;
	}
	else if(bluetooth=='e')
	{
		forward_way = 0;
		backward_way = 0;
		left_way = 0;
		right_way = 0;
		stop_way = 1;
	}
}
void InterruptTime0() interrupt 1
{
	PWMCnt1++;
	PWMCnt2++;
	if(PWMCnt1  == 200)
	{
		PWMCnt1 = 0;
	}
	if(PWMCnt1 <= cntPWM1)
	{
		ENA = 1;
	}
	else
	{
		ENA = 0;
	}

	if(PWMCnt2 == 200)
	{
		PWMCnt2 = 0;
	}
	if(PWMCnt2 <= cntPWM2)
	{
		ENB = 1;
	}
	else
	{
		ENB = 0;
	}

	TH0 = (65536 - 50)/256;
	TL0 = (65536 - 50)%256;
}

void InterruptUart() interrupt 4
{
	bluetooth = SBUF;
	RI = 0;
	SBUF = bluetooth;
	while(!TI);			 
	TI = 0;	

	Link();
}

键盘遥控器代码:

#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;

sbit KEY1 = P1^3;	    //上
sbit KEY2 = P1^1;	    //下
sbit KEY3 = P1^0;		//左
sbit KEY4 = P1^4;		//右
sbit KEY5 = P1^2;		//停
bit flag = 0;

u8 T0RH = 0;
u8 T0RL = 0;
u8 KeySta[] = {1, 1, 1, 1, 1, 1};
u8 KeyCodeMap[] = {'1', '2', '3', '4', '5'};

void KeyDriver();
void ConfigTimer0(u16 ms);
void UartInit();

void main()
{
	EA = 1;
	ConfigTimer0(2);
	UartInit();

	while(1)
	{
	
		KeyDriver();	
	}
}
void ConfigTimer0(u16 ms)  //配置定时器0
{
	u32 tmp;

	tmp = 11059200/12;
	tmp = (tmp * ms)/1000;
	tmp = 65536 - tmp;
	T0RH = (u8)(tmp >> 8);
	T0RL = (u8)tmp;
	TH0 = T0RH;
	TL0 = T0RL;
	TMOD &= 0xF0;
	TMOD |= 0x01;
	ET0 = 1;
	TR0 = 1;
}
void KeyScan()	//按键扫描函数
{
	u8 i;
	static u8 KeyBuff[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

	KeyBuff[0] = (KeyBuff[0] << 1) | KEY1;
	KeyBuff[1] = (KeyBuff[1] << 1) | KEY2;
	KeyBuff[2] = (KeyBuff[2] << 1) | KEY3;
	KeyBuff[3] = (KeyBuff[3] << 1) | KEY4;
	KeyBuff[4] = (KeyBuff[4] << 1) | KEY5;

	for(i=0; i<5; i++)
	{
		if((KeyBuff[i]&0x0F) == 0x0F)
		{
			KeySta[i] = 1;
		}
		else if((KeyBuff[i]&0x0F) == 0x00)
		{
			KeySta[i] = 0;
		}
		else
		{}
	}
}
void KeyAction(u8 keycode)	//按键功能函数
{
	if(keycode == '1')
	{
		SBUF = 'a';
		while(!TI);
		TI = 0;	
	}	
	else if(keycode == '2')
	{
		SBUF = 'b';
		while(!TI);
		TI = 0;
	}
	else if(keycode == '3')
	{
		SBUF = 'c';
		while(!TI)
		TI = 0;
	}
	else if(keycode == '4')
	{
		SBUF = 'd';
		while(!TI);
		TI = 0;
	}
	else
	{
		SBUF = 'e';
		while(!TI);
		TI = 0;
	}
}
void KeyDriver()
{
	u8 i;
	static u8 backup[] = {1, 1, 1, 1, 1};

	for(i=0; i<5; i++)
	{
		if(KeySta[i] != backup[i])
		{
			if(backup[i] != 0)
			{
				KeyAction(KeyCodeMap[i]);
			}
			backup[i] = KeySta[i];
		}
	}
}
//void UartInit()
//{	
//  TH1 = 0xFD;
//	TL1 = 0xFD;
//	TMOD &= 0x0F;
//	TMOD |= 0x20;
//	SCON = 0x50;
//	PCON &= 0x7F;
//	ET1 = 0;
//	TR1 = 1;
//}

void UartInit()//aaa@qq.com
{
PCON &= 0x7F;
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TL1 = 0xFD;
TH1 = 0xFD;
ET1 = 0;
TR1 = 1;
}

void InterruptTimer0() interrupt 1
{
	TH0 = T0RH;
	TL0 = T0RL;

	KeyScan();
}


		


相关标签: 51单片机