您的位置:首页 > 其它

基于51单片机的蓝牙hc05遥控小车

2018-04-28 16:47 429 查看

//该程序可以实现蓝牙小车的前进后退,左拐右拐,以及加速减速。

#include"reg51.h"


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


sbit IN1=P1^1;
sbit IN2=P1^2;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit EWA=P1^0;
sbit EWB=P1^5;
sfr AUXR=0X8E;
sfr IPH=0xb7;
u8 b=0,a=3,temp,c=5,d=0;


void UsartInit()
{
AUXR=0X10;
SCON=0X50; 
TMOD=0X20; 
PCON=0X00; 
TH1=0XFD; 
TL1=0XFD;
ES=1; 
EA=1; 
TR1=1; 
}


void delay(u16 a)
{
while(a--);



void go_forward()
{
IN1=1;
IN2=0; 
IN3=0;
IN4=1;
}


void go_behind()
{
IN1=0;
IN2=1;
IN3=1;
IN4=0; 
}


void go_right()
{
IN1=0;
IN2=1;
IN3=0;
IN4=1;



void go_left()
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
}


void stop()
{
IN1=0;
IN2=0;
IN3=0;
IN4=0; 
}


void main()

UsartInit(); 
while(1)

  b++;
  if(b>=10)b=0;
  if(b>a){EWA=1;}
  else{EWA=0;}  
  d++;
  if(d>=10)d=0;
  if(d>c){EWB=1;}
  else{EWB=0;}  

}


void Usart() interrupt 4 
{
u8 receiveData;
temp=receiveData;
receiveData=SBUF;
switch(receiveData)
{
case'1': go_forward(); break; 
case'2': go_behind();  break;
case'3': go_left();  break;
case'4': go_right();  break;
case'0': stop();  break;
case'5': a--;if(a<=0)a=1;receiveData=0;delay(50000);break;
case'6': a++;if(a>=10)a=10;receiveData=0;delay(50000);break;
case'7': c--;if(c<=0)c=1;receiveData=0;delay(50000);break;
case'8': c++;if(c>=10)c=10;receiveData=0;delay(50000);break;
}
RI = 0;

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: