找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1360|回复: 0
收起左侧

蓝牙小车单片机源程序

[复制链接]
ID:276753 发表于 2018-1-16 21:28 | 显示全部楼层 |阅读模式
  1. #include <REGX51.H>
  2. #define godata 0x31;
  3. #define backdata 0x32;
  4. #define rightdata 0x33;
  5. #define leftdata 0x34;
  6. //#define P0=0;

  7. sbit PWM0=P1^0;                /*IN1*/
  8. sbit PWM1=P1^1;                /*IN2*/
  9. sbit PWM2=P1^2;                   /*IN3*/
  10. sbit PWM3=P1^3;                   /*IN4*/

  11. sbit P_0=P0^0;                /*LED1*/
  12. sbit P_1=P0^1;                /*LED*/
  13. sbit P_2=P0^2;                   /*LED*/
  14. sbit P_3=P0^3;                   /*LED*/
  15. sbit P_4=P0^4;                   /*LED*/
  16. sbit P_5=P0^5;                   /*LED*/

  17. unsigned char receive ,receivedata;
  18. unsigned char go,back,right,left,stop,LED;
  19. //串口波特率的定时发生器,用的是定时器1 方式2,波特率是9600
  20. void init()
  21. {
  22.     TMOD=0x20;                /* 定时器工作在方式2*/
  23.         TL1=0XFD;                /*9600波特率 */
  24.         TH1=0XFD;                  /*启动T1*/
  25.         SCON=0X50;                /*串行方式1,允许接收*/
  26.         PCON=0X00;
  27.         TR1=1;                        /*启动T1*/
  28.         EA=1;                          /*开总中断*/
  29.         ES=1;                        /*允许串行中断*/


  30. }


  31. void signal() interrupt 4        /*串口接收中断函数*/
  32. {

  33.          
  34.              RI=0;
  35.                 receivedata=SBUF;
  36.             receive=1;


  37. }
  38. //控制信号的接收处理
  39. void signalfuntion()
  40. {
  41.         if(receive==1)
  42.         {
  43.                 receive=0;
  44.                 go=0;
  45.                 back=0;
  46.                 right=0;
  47.                 left=0;
  48.                 stop=0;
  49.                 LED=0;

  50.                         switch(receivedata)
  51.                         {
  52.                                         case 0x01:go=1;                        //前进
  53.                                                         break;
  54.                                         case 0x02:back=1;                //后退
  55.                                                                 break;
  56.                                         case 0x03:right=1;         //右转
  57.                                                                 break;
  58.                                         case 0x04:left=1;          //左转
  59.                                                                 break;
  60.                                         case 0x05:stop=1;        //停
  61.                                         case 0x06:LED=1;
  62.                                 //        default:stop=1;
  63.                                         default:break;
  64.                        
  65.                         }

  66.         }
  67.         else
  68.         {
  69.                   go=0;
  70.                 back=0;
  71.                 right=0;
  72.                 left=0;
  73.                    stop=0;
  74.         }

  75. }
  76.         //电机控制输出
  77. void Motorcontrol()
  78. {              

  79.      if(go==1)
  80.                  {
  81.                          PWM0=1;
  82.                         PWM1=0;
  83.                         PWM2=1;
  84.                         PWM3=0;


  85.                  }
  86.                  else if(back==1)
  87.                  {
  88.                          PWM0=0;
  89.                         PWM1=1;
  90.                         PWM2=0;
  91.                         PWM3=1;

  92.                  }

  93.                  else if(right==1)
  94.                  {   
  95.                                          PWM0=1;
  96.                                         PWM1=0;
  97.                                         PWM2=1;
  98.                                         PWM3=1;
  99.                   
  100.                  }
  101.                  else if(left==1)
  102.                  {
  103.                  
  104.                                  PWM0=1;
  105.                                 PWM1=1;
  106.                                 PWM2=1;
  107.                                 PWM3=0;
  108.                  
  109.                  }
  110.                    else if(stop==1)
  111.                  {
  112.                            PWM0=1;
  113.                         PWM1=1;
  114.                         PWM2=1;
  115.                         PWM3=1;
  116.                  
  117.                  }
  118.                  
  119.           }


  120. void main()
  121. {
  122.         init();         //初始化


  123.   while(1)
  124.         {  
  125.          
  126.                
  127.              signalfuntion()        ;
  128.                 Motorcontrol();
  129.          }

  130. }

复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表