找回密码
 立即注册

QQ登录

只需一步,快速开始

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

蓝牙小车,pwm调速

[复制链接]
跳转到指定楼层
楼主
ID:242950 发表于 2017-10-27 15:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
uchar Buffer[4] = {0}; //从串口接收的数据
void delay(uint t);
uint i,j;
////////////////////////////
////////////电机转动
sbit P30=P2^0;
sbit P31=P2^1;
sbit P32=P2^2;
sbit P33=P2^3;
/////////pwm调试使能端
sbit ENA=P0^0;
sbit ENB=P0^1;
////////////////////////////
#define Right_moto_pwm P0^0 //接驱动模块ENA使能端输入PWM信号调节速度
#define Left_moto_pwm P0^1 //接驱动模块ENB使能端输入PWM信号调节速度
///////////////定义电机转动方向              
#define Left_moto_back {P30=1,P31=0;} //左电机后退
#define Left_moto_go {P30=0,P31=1;} //左电机前进
#define Left_moto_stop {P30=1,P31=1;} //左电机停转
#define Right_moto_back {P32=1,P33=0;} //右电机后退
#define Right_moto_go {P32=0,P33=1;} //右电机前转
#define Right_moto_stop {P32=1,P33=1;}
/////////////////////////////
uchar pwm_val_left =0;
uchar push_val_left =0; //左电机占空比N/10
uchar pwm_val_right =0;
uchar push_val_right=0; //右电机占空比N/10
bit Right_moto_stp=1;
bit Left_moto_stp =1;
uint num,i,d,j=0;
/********************************************************************
* 名称 : Delay_1ms()
* 功能 : 延时子程序,延时时间为 1ms * x
* 输入 : x (延时一毫秒的个数)
* 输出 : 无
***********************************************************************/
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void run(void) //前进函数
{
push_val_left =17; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度
push_val_right =17; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

Left_moto_go ; //左电机前进
Right_moto_go ; //右电机前进

}
void run1(void)//前进函数1
{
push_val_left =4; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

push_val_right =4; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

Left_moto_go ; //左电机前进
Right_moto_go ; //右电机前进

}
void run2(void)//前进函数1
{
push_val_left =21; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

push_val_right =21; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

Left_moto_go ; //左电机前进
Right_moto_go ; //右电机前进

}
/****************************************************************
********/
void left(void) //左转函数
{
push_val_left =19;
push_val_right =19;
Right_moto_go; //右电机继续
Left_moto_stop; //左电机停走
}
void left1(void) //左转函数 1
{
push_val_left =20;
push_val_right =20;
Right_moto_go; //右电机继续
Left_moto_stop; //左电机停走
}

/***************************************  *********************************/
void right(void) //右转函数
{
push_val_left =19;
     
push_val_right =19;
Right_moto_stop; //右电机停走
Left_moto_go; //左电机继续
}
void right1(void) //右转函数1
{
push_val_left =20;
push_val_right =20;
Right_moto_stop; //右电机停走
Left_moto_go; //左电机继续
}
///////////////////////////////////////////////////////////////停止
void stop(void)
{
Right_moto_stop; //右电机停走
Left_moto_stop; //左电机停走

//run();
//
//Delayms(100);
}
///////////////////////////////////////////
void zzhijiao()
{
   push_val_left =19;
   
   push_val_right =19;
   
   Left_moto_go ; //左电机前进
   
   Right_moto_back ;
       }
///////////////////////////////////////
void yzhijiao()
{
  push_val_left =19;
  push_val_right =19;
  
  Left_moto_back ; //左电机前进
      
  Right_moto_go ;}


void backward(void)//后退
{
push_val_left =15; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

push_val_right =15; //PWM 调节参数1-20 1为最慢20是最快 改这个值可以改变其速度

Left_moto_back; //左电机前进
Right_moto_back ; //右电机前进

}
/*************************PWM调 制 电 机 转 速
********************************/
void pwm_out_left_moto(void) //左电机调速,调节push_val_left的值改变电机转速,占空比

{
if(Left_moto_stp)
{
{if(pwm_val_left<=push_val_left)
{ ENB=1;}
else
  {ENB=0;}
  }
{if(pwm_val_left>=20)
  {pwm_val_left=0;}
  }
}
else
{ENB=0;}
}

void pwm_out_right_moto(void) //右电机调速,调节push_val_left的值改变电机转速,占空比

{
if(Right_moto_stp)
{
if(pwm_val_right<=push_val_right)
{ENA=1;}
else
{ENA=0;}
if(pwm_val_right>=20)
{pwm_val_right=0;}
}
else
{ENA=0;}

}











/********************************************************************
* 名称 : Com_Int()
* 功能 : 串口中断子函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Com_Int(void) interrupt 4
{
EA = 0;
if(RI == 1) //当硬件接收到一个数据时,RI会置位
{
Buffer[0] = SBUF - 48; //这里减去48是因为从电脑中发送过来的数据是ASCII码。
RI = 0;
}
EA = 1;
}
/********************************************************************
* 名称 : Com_Init()
* 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中断
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Com_Init(void)
{
TMOD = 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd; //设置波特率 9600
TL1 = 0xFd;
TR1 = 1; //启动定时器1
ES = 1; //开串口中断
EA = 1; //开总中断
}

void Init_Timer0()interrupt 1 using 2
{

TH0=0XFC; //2Ms定时
TL0=0X30;
  
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}

void main()
{
Delay_1ms(100);
Com_Init();//串口初始化
TMOD |=0X01;
TH0= 0XFC; //2ms定时
TL0= 0X30;
TR0= 1;
ET0= 1;
stop();
delay(5000);
delay(5000);
delay(5000);
delay(5000);
while(1)
{
switch(Buffer[0])
{
case 0: run(); break;
case 1: left(); break;
case 2: right(); break;
case 3: zzhijiao(); break;
case 4: yzhijiao(); break;
case 5: run1(); break;
case 6: stop(); break;
case 7: backward();break;
default:break;
}
}
}
void delay(unsigned int t)
{
while(--t);
}

蓝牙小车.rar

25.21 KB, 下载次数: 23, 下载积分: 黑币 -5

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

使用道具 举报

沙发
ID:479857 发表于 2022-10-26 14:55 | 只看该作者
感觉程序没多大用处
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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