目录
一、主要功能
** 基于AT89C51,L298N驱动两个电机,因为是平台,最后用两个电机驱动,然后第一个按键控制所有电机停止,第二个按键按下,电机顺时针转动,第三个按键按下,电机逆时针转动。然后加入pwm调速,分为高速和低速,并且LCD1602显示屏显示当前的平台状态,比如一开始所有电机是停止的,电机顺时针转动,LCD1602显示平台前翻,逆时针转动显示平台后翻,如果按第一个按键,即所有电机停止转动,则LCD1602显示平台静止。**
仿真图:
编辑原理图:
编辑
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
本系统包含51单片机最小系统、LCD1602显示模块、L298N翻转模块、按键模块。
三、程序编程
#include <REGX52.H>
#include<intrins.h>
#include<stdio.h>
#include "Delay.h"
#include "LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
/*引脚定义*/
sbit Motor1_IN1 = P3^3; //电机IN1口
sbit Motor1_IN2 = P3^4;
sbit Motor1_EN = P3^2; //电机使能端1
sbit key1 = P1^4; //按键定义
sbit key2 = P1^5;
sbit key3 = P1^6;
sbit key4 = P2^4;
sbit key5 = P2^5;
/*变量定义*/
uchar count = 0; //定时中断次数
uchar count1 = 0;
uchar a = 0; //标志位
uchar flag;
/*函数定义*/
void anjiansm();
void Timer0_Init(void) //200微秒@11.0592MHz
{
//AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x48; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 0;
EA = 0;
}
void Car_RightMove(uchar speed) //小车右转前进
{
uchar Car_Speed = speed;
count++;
if(count <= Car_Speed)
{
Motor1_IN1 = 1;
}
else
{
Motor1_IN1 = 0;
}
if(count >= 100)
{
count = 0;
}
}
void Car_LeftMove(uchar speed) //小车左转前进
{
uchar Car_Speed = speed;
count1++;
if(count1 <= Car_Speed)
{
Motor1_IN2 = 1;
}
else
{
Motor1_IN2 = 0;
}
if(count1 >= 100)
{
count1 = 0;
}
}
void Timer0_isr(void) interrupt 1
{
TL0 = 0x48; //设置定时初值
TH0 = 0xFF; //设置定时初值
switch(a)
{
case 1:Car_RightMove(50);break; //小车右转低速前进
case 2:Car_RightMove(100);break; //小车右转高速前进
case 3:Car_LeftMove(50);break; //小车左转低速前进
case 4:Car_LeftMove(100);break; //小车左转高速前进
default:break;
}
}
void main() //主函数
{
Timer0_Init(); //定时器初始化
LCD_Init(); //显示屏初始化
Motor1_EN = 0; //停止转动
Motor1_IN1 = 0;
Motor1_IN2 = 0;
LCD_ShowString(1,1," Ping Tai ");//显示平台
LCD_ShowString(2,1," LEVEL ");//显示LEVEL
while(1)
{
anjiansm(); //按键扫描
}
}
void anjiansm() //按键扫描
{
if(key1 == 0)
{
Delay(100);
{
if(key1 == 0)
{
ET0 = 0;
EA = 0;
Motor1_EN = 0; //停止转动
Motor1_IN1 = 0;
Motor1_IN2 = 0;
LCD_ShowString(2,1," LEVEL ");//显示LEVEL
}
}
}
if(key2 == 0)
{
Delay(100);
{
if(key2 == 0)
{
ET0 = 1;
EA = 1;
Motor1_EN = 1; //顺时针转动
Motor1_IN1 = 1;
Motor1_IN2 = 0;
a = 2;
flag = 0;
LCD_ShowString(2,1," Qian Fan ");//显示qianfan
}
}
}
if(key3 == 0)
{
Delay(100);
{
if(key3 == 0)
{
ET0 = 1;
EA = 1;
Motor1_EN = 1; //逆时针转动
Motor1_IN1 = 0;
Motor1_IN2 = 1;
a = 4;
flag = 1;
LCD_ShowString(2,1," Hou Fan ");//显示qianfan
}
}
}
if(key4 == 0) //低速按钮
{
Delay(100);
{
if(key4 == 0)
{
ET0 = 1;
EA = 1;
if(flag == 0)
{
a = 1;
}
else
{
a= 3;
}
}
}
}
if(key5 == 0) //高速按钮
{
Delay(100);
{
if(key5 == 0)
{
ET0 = 1;
EA = 1;
if(flag == 0)
{
a = 2;
}
else
{
a= 4;
}
}
}
}
}
四、实现现象
具体动态效果看B站演示视频:
基于51单片机的翻转平台和PWM调速_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、原理图、演示视频):
链接:https://pan.baidu.com/s/1rpwPLw9No9toaUq-Tp2kIA
提取码:qmjn
–来自百度网盘超级会员V4的分享