找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1232|回复: 0
打印 上一主题 下一主题
收起左侧

基于51单片机的蓝牙小车

[复制链接]
跳转到指定楼层
楼主
ID:723182 发表于 2020-4-22 19:29 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
目前蓝牙小车还在调试
#include <STC89C5xRC.H>

#define Left_moto2_pwm     P0_4          
#define Right_moto2_pwm    P0_5          
#define Left_moto_pwm     P1_4          
#define Right_moto_pwm    P1_5          
#define uchar unsigned char
#define uint unsigned int

sbit P0_4=P0^4;                 
sbit P0_5=P0^5;                 
sbit P1_4=P1^4;               
sbit P1_5=P1^5;               
sbit IN1 = P1^2;
sbit IN2 = P1^3;
sbit IN3 = P1^6;
sbit IN4 = P1^7;
sbit IN5 = P0^2;
sbit IN6 = P0^3;
sbit IN7 = P0^6;
sbit IN8 = P0^7;

bit Right_moto_stop=1;          
bit Left_moto_stop =1;               
unsigned  int  time=0;               
int pwm=1;

#define left_motor_en        EN1 = 1       
#define left_motor_stops        EN1 = 0       
#define right_motor_en        EN2 = 1       
#define right_motor_stops        EN2 = 0

#define left_motor2_en        EN3 = 1
#define left_motor2_stops        EN3 = 0       
#define right_motor2_en        EN4 = 1       
#define right_motor2_stops        EN4 = 0       

#define left_motor_go        IN1 = 0, IN2 = 1
#define left_motor_back        IN1 = 1, IN2 = 0
#define right_motor_go        IN3 = 1, IN4 = 0
#define right_motor_back        IN3 = 0, IN4 = 1

#define left_motor2_go        IN5 = 0, IN6 = 1
#define left_motor2_back        IN5 = 1, IN6 = 0
#define right_motor2_go        IN7 = 1, IN8 = 0
#define right_motor2_back        IN7 = 0, IN8 = 1

unsigned char pwm_val_left  =0;
unsigned char push_val_left =0;
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;
void delay(uint z)
{
        uint x,y;
        for(x = z; x > 0; x--)
        for(y = 114; y > 0 ; y--);
}
void UART_INIT()
{
        SM0 = 0;
        SM1 = 1;
        REN = 1;                                                                                                                 
        EA = 1;
        ES = 1;
        TMOD = 0x20;
        TH1 = 0xfd;
        TL1 = 0xfd;
        TR1 = 1;
}
     void  run(void)       
{
     push_val_left  =pwm;  
         push_val_right =pwm;         
         if(pwm==10) pwm=0;
         if(pwm==0&&pwm<0) pwm=0;
}
                void pwm_out_left_moto(void)
{  
   if(Left_moto_stop)
   {
    if(pwm_val_left<=push_val_left)
             {  Left_moto_pwm=1;
                    Left_moto2_pwm=1;  }
        else
              { Left_moto_pwm=0;Left_moto2_pwm=0;                 }

        if(pwm_val_left>=10)
        pwm_val_left=0;
   }
   else { Left_moto_pwm=0;Left_moto2_pwm=0;          }
}
   void pwm_out_right_moto(void)
{
  if(Right_moto_stop)
   {
    if(pwm_val_right<=push_val_right)
             {  Right_moto_pwm=1;
                   Right_moto2_pwm=1;           }
        else
               {Right_moto_pwm=0;
                    Right_moto2_pwm=0;}
        if(pwm_val_right>=10)
            pwm_val_right=0;
   }
   else    {Right_moto_pwm=0;Right_moto2_pwm=0;        }
}
        void timer0()interrupt 1   using 2
{
     TH0=0XF8;         
         TL0=0X30;
         time++;
         pwm_val_left++;
         pwm_val_right++;
         pwm_out_left_moto();
         pwm_out_right_moto();
}
void forward()
{
        ET0 = 1;
        run();                         
        left_motor_go;
        right_motor_go;
        left_motor2_go;
        right_motor2_go;
}
void left_go()          
{
        ET0 = 1;
        run();
        left_motor_back;
        right_motor_go;
        left_motor2_back;
        right_motor2_go;
        delay(700);
        forward();
}
void right_go()
{
        ET0 = 1;
        run();
        delay(50);
        right_motor_back;
        left_motor_go;
        right_motor2_back;
        left_motor2_go;
        delay(700);
        forward();
}
void left()
{
        ET0 = 1;
        run();
        delay(50);
        right_motor_go;                  
        left_motor_back;       
        right_motor2_go;                  
        left_motor2_back;       
}
void right()
{
        ET0 = 1;
        run();
        left_motor_go;
        right_motor_back;  
        left_motor2_go;
        right_motor2_back;
}
void back()
{
        ET0 = 1;
        run();
        left_motor_back;
        right_motor_back;       
        left_motor2_back;
        right_motor2_back;
}


void stop()
{
        ET0 = 0;
        P1=0;
        P0=0;
}

void UART_SER() interrupt 4
{
        if(RI)
        {
                RI = 0;
                switch(SBUF)
                {
                        case 'g': forward(); break;
                        case 'b': back(); break;
                        case 'l': left(); break;
                        case 'r': right(); break;
                        case 's': stop(); break;
                        case 'z': left_go(); break;
                         case 'y': right_go(); break;
                        case 'p': pwm++;break;               
                        case 'c': pwm--;break;               
                }
        }
}
void main()
{
        TMOD=0X01;
        TH0= 0XF8;                 
        TL0= 0X30;
        TR0= 1;
        ET0= 1;
        EA = 1;
        UART_INIT();
        while(1);
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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