用AT89c52单片机控制2个驱动模块,一个驱动模块控制两个电机,并且包含pwm调速的蓝牙小车代码
时间: 2023-05-23 08:07:34 浏览: 112
89C52蓝牙小车程序(PWM调速)
5星 · 资源好评率100%
A:这里提供一份基于AT89C52单片机控制的蓝牙小车代码,包含PWM调速功能。
```c
#include <reg52.h>
#define FORWARD 0x06 //电机正转
#define BACKWARD 0x09 //电机反转
#define STOP 0x00 //电机停止
#define LEFT 0x05 //小车左转
#define RIGHT 0x0A //小车右转
#define SPEED_1 0x20 //电机速度1(20%)
#define SPEED_2 0x40 //电机速度2(40%)
#define SPEED_3 0x60 //电机速度3(60%)
#define SPEED_4 0x80 //电机速度4(80%)
#define SPEED_5 0xA0 //电机速度5(100%)
sbit enA=P1^0; //使能A
sbit in1=P1^1; //控制A1
sbit in2=P1^2; //控制A2
sbit enB=P1^3; //使能B
sbit in3=P1^4; //控制B1
sbit in4=P1^5; //控制B2
struct Gear{
unsigned char dir; //电机方向
unsigned char speed; //电机速度
}gearA,gearB; //每个电机一个结构体
void delay(int time) //延迟函数
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<120;j++);
}
void motor(unsigned char motor_num, struct Gear gear) //控制电机
{
if(motor_num == 1)
{
if(gear.dir == FORWARD) //正转
{
in1 = 1;
in2 = 0;
}
else if(gear.dir == BACKWARD) //反转
{
in1 = 0;
in2 = 1;
}
else //停止
{
in1 = 0;
in2 = 0;
}
enA = 1;
PWMOUT = gear.speed; //控制pwm输出比例来控制电机速度
}
else if(motor_num == 2) //同上
{
if(gear.dir == FORWARD)
{
in3 = 1;
in4 = 0;
}
else if(gear.dir == BACKWARD)
{
in3 = 0;
in4 = 1;
}
else
{
in3 = 0;
in4 = 0;
}
enB = 1;
PWMOUT = gear.speed;
}
}
void main()
{
TMOD = 0x01; //定时器模式:16位定时器模式0
TH0 = 0xFF; //16位初始值
TL0 = 0x9C;
ET0 = 1; //打开定时器0的中断
EA = 1; //打开总中断
TR0 = 1; //启动定时器0
PWMOUT = 0; //pwm初始值为0,即100%占空比
gearA.dir = STOP; //电机初始方向为停止
gearA.speed = SPEED_3;//电机初始速度为60%
gearB.dir = STOP;
gearB.speed = SPEED_3;
while(1)
{
//接收蓝牙数据
if(RI)
{
RI = 0;
switch(SBUF)
{
case 'F': //前进
{
gearA.dir = FORWARD;
gearB.dir = FORWARD;
motor(1, gearA);
motor(2, gearB);
break;
}
case 'B': //后退
{
gearA.dir = BACKWARD;
gearB.dir = BACKWARD;
motor(1, gearA);
motor(2, gearB);
break;
}
case 'L': //左转
{
gearA.dir = FORWARD;
gearB.dir = BACKWARD;
motor(1, gearA);
motor(2, gearB);
break;
}
case 'R': //右转
{
gearA.dir = BACKWARD;
gearB.dir = FORWARD;
motor(1, gearA);
motor(2, gearB);
break;
}
case 'S': //停止
{
gearA.dir = STOP;
gearB.dir = STOP;
motor(1, gearA);
motor(2, gearB);
break;
}
case '1': //调速,速度1
{
gearA.speed = SPEED_1;
gearB.speed = SPEED_1;
motor(1, gearA);
motor(2, gearB);
break;
}
case '2': //速度2
{
gearA.speed = SPEED_2;
gearB.speed = SPEED_2;
motor(1, gearA);
motor(2, gearB);
break;
}
case '3': //速度3
{
gearA.speed = SPEED_3;
gearB.speed = SPEED_3;
motor(1, gearA);
motor(2, gearB);
break;
}
case '4': //速度4
{
gearA.speed = SPEED_4;
gearB.speed = SPEED_4;
motor(1, gearA);
motor(2, gearB);
break;
}
case '5': //速度5
{
gearA.speed = SPEED_5;
gearB.speed = SPEED_5;
motor(1, gearA);
motor(2, gearB);
break;
}
}
}
}
}
void Timer0_ISR() interrupt 1 //定时器0的中断服务函数
{
static unsigned char loop_count = 0;
static unsigned char flag = 0;
TL0 = 0x9C;
TH0 = 0xFF;
loop_count++;
if(loop_count == 20) //每1ms进一次中断服务函数
{
loop_count = 0;
flag++;
if(flag > 100) //每101ms改变一次pwm输出比例,模拟pwm调速功能
{
flag = 0;
if(PWMOUT == gearA.speed)
{
PWMOUT = 0;
}
else
{
PWMOUT = gearA.speed;
}
}
}
}
```
该代码可以实现蓝牙控制小车前进、后退、左右转弯、停止以及PWM调速等功能。其中,定时器0的中断服务函数每1ms进一次,并在每101ms改变一次PWM输出比例,来控制电机速度。用户可以通过蓝牙发送不同的控制命令来控制小车的行驶,并可以通过发送数字“1”到“5”来调节小车的速度。
阅读全文