目录
- 基于STM32的直流电机调速系统
- 一、原理图
- 二、部分代码
- 三、视频演示
基于STM32的直流电机调速系统
功能:
1.通过LCD屏幕显示实时两个电机的占空比
2.通过按键调整电机1和2的加减速
3.通过L298N驱动两个直流电机完成调速
一、原理图
二、部分代码
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd1602.h"
#include "key.h"
#include "dht11.h"
#include "stdio.h"
#include "timer.h"
#define RELAY_ON GPIO_SetBits(GPIOA, GPIO_Pin_8)
#define RELAY_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_8)
extern unsigned int Motor1_Pwm;
extern unsigned int Motor2_Pwm;
unsigned char Flag;
unsigned int key_value;
unsigned char buffer1[15];
unsigned char buffer2[15];
void Relay_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RELAY_OFF;
}
int main(void)
{
LCD_init();
KEY_Init();
TIM_Config();
Relay_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
while(1)
{
sprintf((char*)buffer1, "Motor1_Pwm:%d %% ", Motor1_Pwm/10);
LCD_write_string(0, 0, (char*)buffer1);
sprintf((char*)buffer2, "Motor2_Pwm:%d %% ", Motor2_Pwm/10);
LCD_write_string(0, 1, (char*)buffer2);
key_value = KEY_Scan(0);
if(key_value == 1) {if(Motor1_Pwm <= 900) Motor1_Pwm+=100;}
else if(key_value == 2) {if(Motor1_Pwm >= 100) Motor1_Pwm-=100;}
else if(key_value == 3) {if(Motor2_Pwm <= 900) Motor2_Pwm+=100;}
else if(key_value == 4) {if(Motor2_Pwm >= 100) Motor2_Pwm-=100;}
else if(key_value == 5)
{
if(Flag == 0)
{
Flag = 1;
RELAY_ON;
}
else if(Flag == 1)
{
Flag = 0;
RELAY_OFF;
}
}
}
}
三、视频演示
哔哩哔哩视频演示链接跳转,请直接点击!