1709【毕设课设】基于单片机PID算法电机转速控制系统设计

基于单片机PID算法电机转速控制系统设计,通过PID算法对电机的速度进行控制,资料包含仿真和程序等软件资料。

展示部分程序:

#include<reg52.h>
#include"lcd1602.h"

sfr T2MOD = 0x0c9;
#define uchar unsigned char
#define uint unsigned int

sbit Q0 = P2^4;
sbit Q1 = P2^5;
sbit Q2 = P2^6;
sbit Q3 = P2^7;

sbit PWM	 	= P1^7;
sbit UP		 	= P1^0;
sbit DOWM	 	= P1^1;
sbit GORB		= P2^3; //换相
sbit ADDSPEED 	= P1^2;
sbit SUBSPEED	= P1^3;

uint tuint = 65535;
uint tpwm = 1;	//pwm周期为10000us tpwm变量表示pwm高电平时间,也相当于占空比 (仿真时,频率高时,电机反应慢。在实物上要加大频率)
uchar t1_flag = 0;

uint pulse = 0;
uint t0_flag = 0;
uchar t2_flag = 0;
bit t2_over = 0;
bit Just_Get = 1;


#define 	ZZ 		{ Q0 = 0;Q1 = 0;Q2 = 1;Q3 = 1;}	//正转
#define 	FZ 		{ Q0 = 1;Q1 = 1;Q2 = 0;Q3 = 0;}	//反转
#define 	STOP	{ Q0 = 1;Q1 = 0;Q2 = 1;Q3 = 0;}	//停止
//禁止出现 Q0 = 0;Q1 = 1;Q2 = 0;Q3 = 1; 不然会烧掉mos管

//************************ PID *************************************
float now = 0,bef = 0,bbef = 0; 	//本次采样值,上次采样值,上上次采样值
float err_now,err_bef,err_bbef;		//当前偏差,上次偏差,上上次偏差
float error_add = 0;				//所有偏差之和
float set = 25;						//设定值

float kp = 25;
float ki = 25;
float kd = 0;