当前位置:网站首页>基于51单片机的直流电机调速系统(L298的使用)
基于51单片机的直流电机调速系统(L298的使用)
2022-07-29 05:22:00 【半生烟火一世迷离】
设计要求
一个按键控制启动/停止
一个按键控制正反转
两个按键控制结束减速
四位数码管前一位显示正反转、1表示正转、0表示反转,后三位显示速度值
采用AT89C51单片机,以及直流电机驱动器L298
L298的IN1、IN2控制OUT1、OUT2输出高低电平
IN3、IN4控制OUT3、OUT4输出高低电平
使电机两端有一个电压差
可以外接两路直流电机。
ENA、ENB使能两路电机控制端口高电平有效。
用到的模块有数码管显示、按键获取、定时器0.
数码管显示部分
#include <REGX52.H>
#include "Delay.h"
sbit we2=P2^1;
sbit we3=P2^2;
sbit we4=P2^3;
void Nixie(Number)
{
switch(Number) //位码输出
{
case 0:
we2=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we2=1;
we3=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we3=1;
we4=0;
P0=0x3f;Delay(1);we4=1; break; //段码输出
case 1:
we2=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we2=1;
we3=0;
P0=0x5b; //段码输出
Delay(1); //显示一段时间
we3=1;
we4=0;
P0=0x6d;Delay(1);we4=1;
break;
case 2:
we2=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we2=1;
we3=0;
P0=0x6d; //段码输出
Delay(1); //显示一段时间
we3=1;
we4=0;
P0=0x3f;Delay(1);we4=1;break; //段码输出
case 3:
we2=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we2=1;
we3=0;
P0=0x07; //段码输出
Delay(1); //显示一段时间
we3=1;
we4=0;
P0=0x6d;Delay(1);we4=1;break; //段码输出
case 4:
we2=0;
P0=0x06; //段码输出
Delay(1); //显示一段时间
we2=1;
we3=0;
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we3=1;
we4=0;
P0=0x3f;Delay(1);we4=1;break; //段码输出
}
}
按键获取部分
#include <REGX52.H>
#include "Delay.h"
unsigned char Key()//按键消抖和松手检测
{
unsigned char KeyNumber=0;
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=1;}//获取按键值
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=3;}
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=4;}
return KeyNumber;
}
定时器配置部分
#include <REGX52.H>
void Timer0_Init(void)
{
TMOD &= 0xF0; //配置定时器模式
TMOD |= 0x01; //设置定时器0模式
TL0 = 0x06; //设置定时初值250us产生一次中断
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
PT0=0;
}
主函数以及中断服务函数
#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "Nixie.h"
#include "Timer0.h"
sbit IN1=P2^4;
sbit IN2=P2^5;
sbit EN=P3^7;
sbit we1=P2^0;
unsigned char Counter,Compare=0;
unsigned char KeyNum;
unsigned int x=0,Speed=0;
void main()
{
Timer0_Init();
while(1)
{
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);x++;if(x==2){x=0;}}
KeyNum=Key();
if(KeyNum==3)
{
Speed++;
if(Speed==1){Compare=25;} //设置比较值,改变PWM占空比
if(Speed==2){Compare=50;}
if(Speed==3){Compare=75;}
if(Speed==4){Compare=100;}
}
if(KeyNum==4)
{
Speed--;
if(Speed==3){Compare=75;}//设置比较值,改变PWM占空比
if(Speed==2){Compare=50;}
if(Speed==1){Compare=25;}
if(Speed==0){Compare=0;}
}
we1=0;//打开位选
if(x==1)
{
P0=0x3f; //段码输出
Delay(1); //显示一段时间
we1=1;
}
else
{
P0=0x06; //段码输出
Delay(1); //显示一段时间
we1=1;
}
Nixie(Speed);
}
}
void Timer0_Routine() interrupt 1
{
TL0 = 0x06; //重装载值设置250us
TH0 = 0xFF;
Counter++;
if(Counter==100); //计数值变化范围限制在0~99
{
Counter=0;
}
if(Counter<Compare) //计数值小于比较值
{
if(KeyNum==1)
{
EN=~EN;
}
if(x==1)
{
IN1=0;
IN2=1;
}
else
{
IN1=1;
IN2=0;
}
}
else//计数值大于比较值时设置
{
IN1=1;
IN2=1;
// IN1=0;
// IN2=0;
}
}
在这里我要感谢某站的UP主是他的视频让我学会单片机
边栏推荐
- 迁移学习——Transitive Transfer Learning
- Error in installing pyspider under Windows: Please specify --curl dir=/path/to/build/libcurl solution
- 一、PyTorch Cookbook(常用代码合集)
- 基于FPGA:多目标运动检测(手把手教学①)
- 虚假新闻检测论文阅读(二):Semi-Supervised Learning and Graph Neural Networks for Fake News Detection
- 预训练语言模型的使用方法
- 六、基于深度学习关键点的指针式表计识别
- Wechat applet source code acquisition (download with tools)
- 1、 Multiprocessing.pool.remotetraceback
- ML6自学笔记
猜你喜欢
Hal library learning notes-10 overview of Hal library peripheral driver framework
一、常见损失函数的用法
ML9自学笔记
Continue the new journey and control smart storage together
D3.js vertical relationship diagram (with arrows and text description of connecting lines)
一、Focal Loss理论及代码实现
充电桩应用方案
HAL库学习笔记- 9 DMA
华为云14天鸿蒙设备开发-Day3内核开发
Reading papers on fake news detection (2): semi supervised learning and graph neural networks for fake news detection
随机推荐
Tf.get in tensorflow_ Detailed explanation of variable() function
QT学习笔记-Qt Model/View
Transformer回顾+理解
二、深度学习数据增强方法汇总
ML15-神经网络(1)
ML6自学笔记
4、 Application of one hot and loss function
二、如何保存MNIST数据集中train和test的图片?
Migration learning robot visual domain adaptation with low rank reconstruction
Configuration and use of Nacos external database
Discussion on the design of distributed full flash memory automatic test platform
[semantic segmentation] overview of semantic segmentation
ReportingService WebService form authentication
2、 How to save the images of train and test in MNIST dataset?
ML自学笔记5
ML17-神经网络实战
基于AD9850的多功能信号发生器
Wechat built-in browser prohibits caching
Hal learning notes - Basic timer of 7 timer
虚假新闻检测论文阅读(三):Semi-supervised Content-based Detection of Misinformation via Tensor Embeddings