标题: 基于stc89c52蓝牙小车代码(一) [打印本页]

作者: 小烽forgot    时间: 2017-9-17 23:52
标题: 基于stc89c52蓝牙小车代码(一)
#include<reg52.h>

sbit IN1=P1^0;  //单片机引脚的定义
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;

#define Left_moto_go      {IN1=0,IN2=1;}           //小车运动的正反转电平设置
#define Left_moto_back    {IN1=1,IN2=0;}        
#define Left_moto_Stop    {IN1=0,IN2=0;}                       
#define Right_moto_go     {IN3=0,IN4=1;}       
#define Right_moto_back   {IN3=1,IN4=0;}       
#define Right_moto_Stop   {IN3=0,IN4=0;}         

unsigned  int  mode;  //用数字或字符来作为运行的指令
unsigned  int  flag;

   void delay(unsigned int k)
{   
     unsigned int x,y;
         for(x=0;x<k;x++)
           for(y=0;y<2000;y++);
}

  void  run(void)                                                 //与前面循迹小车项目类似定义小车运动的函数
{   
         Left_moto_go ;   
         Right_moto_go ;         
}
     void  backrun(void)
{

         Left_moto_back;   
         Right_moto_back;  
}

     void  leftrun(void)
{             
         Right_moto_go ;
     Left_moto_Stop ;  
}

   void  rightrun(void)
{
         
     Left_moto_go  ;  
         Right_moto_Stop   ;         
}

     void  stop(void)
{
         Right_moto_Stop   ;  
         Left_moto_Stop ;         
}

     void  time1init()   //        选择使用定时器1;
{
     TMOD=0x20;                        //并且工作模式2,8位
         TH1=0xfd;                //产生9600波特率,与蓝牙模块同步 ,因为当时购买的蓝牙模块出厂设置就是9600波特率。
         TL1=0xfd;
         SM0=0;                   //设置串口的工作模式为
         SM1=1;                           //传送8位数据
         REN=1;                           //串口通信允许接收
         TR1=1;                           //开定时器1
         EA=1;                           //允许总中断
         ES=1;                           //允许串口中断
}

     void init() interrupt 4   //4模式是串行口中断:串行通信完成数据发送或接收
                                     //并且引起中断
{
     RI=0;                  //接收中断标志
         mode=SBUF;          //接收到的有手机app发送的缓冲区数据SBUF复制给mode
         flag=1;          //说明已经接收数据完毕
}

     int main()
{
     time1init();
         while(1)
         {
            if(flag)
                {
                   flag=0;
                   if(mode==1)        //现在大部分都是用字符,即字母作为串口通信的数据,这里我采用了数字作为通信,          
                   {                        //大大简化了代码
                     run();
                         delay(20);         //通过不断调试,最后确定的延迟函数
                   }
                   if(mode==3)
                   {
                     backrun();
                         delay(20);
                   }
                   if(mode==4)
                   {
                     leftrun();
                         delay(10);
                   }
                   if(mode==5)
                   {
                     rightrun();
                         delay(10);
                   }
                   if(mode==2)
                   {
                     stop();
                         delay(40);
                   }

                }
         }
}


  后面还会陆续更新蓝牙手机app软件的制作方法,敬请留意~






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1