基于51单片机及L298N的直流电机调速系统proteus仿真

硬件设计

(末尾附文件)

代码设计

#include
#include	
#include

#include "lcd.h"
#include "Key.h"
#include "Motor.h"
#include "LCD12864.h"
#include "KeyBoard.h"
#include "LCD12864.h"
#include "LcdShow.h"
#include "PID.h"

extern uchar State_Flag;
extern uchar Fwd_Flag;
extern uchar Rev_Flag;

unsigned int  KeyValue;

unsigned int idata pwm_t;//周期
unsigned int idata Impluse,Time_Count,ImpluseR;
uchar idata Stop_Flag,Run_Flag,Curve_Flag;

uint idata Set_Speed;  //  设定转速
unsigned char idata Sz_Flag,Sr_Flag;  // 记录A相触发后  ,B相高低电平的标志
float idata L_Speed,R_Speed,ReaSpeed;

uint idata PWM_Out,R_Duty,L_Duty,Turn_now;
sbit LSB_L = P1^0;    // LSB_L 
sbit LSB_R = P1^1;    // LSB_R

void TimerInit(void)		//@11.0592MHz
{
	
	TMOD=0x01;		//定时器模式

	TL0 = 0x18;		//初值      1ms  
	TH0 = 0xFC;		//初值
	
	IT0 = 1;	 //设置下降沿触发 还是低电平触发 0低电平
	IT1 = 1;	 //设置下降沿触发 还是低电平触发 0低电平
  EA  = 1;
	EX0 = 1;
	EX1 = 1;
	
	ET0 = 1;
	TR0 = 1;		

}

void main()
{
		  LED = 0;	
	
	    Stop_Flag  = 0;
	    Lcd_Initial();
			TimerInit();
			Set_Speed = 0;
	 Lcd_Clear();
		while(1)
		{
			KeyValue = Get_Keyvalue();   //  矩阵键盘扫描
			if(KeyValue == 14)   // 输入速度
      {
			  Set_Speed = SpeedInput();	
				KeyValue = 16;
	    }
			Key_Process();
			if(Run_Flag)
			{
				Set_Speed = 80;
				Run_Flag = 0;
			}
		
			if(State_Flag && ~Stop_Flag)	
			{
			  PWM_Out = PID_Calculate(Set_Speed,abs(ReaSpeed));
				R_Duty = PWM_Out + Turn_now;
				L_Duty = PWM_Out - Turn_now;
			}
			else 
				PWM_Out = 0;  // 停止  
			Lcd_Show();
	 }	   
}

	
//定时器0中断
void timer0() interrupt 1
{
	static int i;
	TL0 = 0x18;		//初值
	TH0 = 0xFC;		//初值
	
	
	pwm_t++;
  Time_Count++;;
	if(pwm_t == 500)   //500ms
	{
		pwm_t = 0;
		if(Stop_Flag)
		{
			i++;
			L_Duty = 0;
			R_Duty = 0;
			if(i<31)
			{
		     LED = ~LED;
				 if(i == 30)  
				 {
					 Stop_Flag = 0;
					 i = 0;
					 Set_Speed = 30;
				 }
			}
		}
	}		
	if( pwm_t

链接:https://pan.baidu.com/s/1W43ZK5Y66Zgp6vmCE2h58A
提取码:vzqn

你可能感兴趣的:(基于51单片机及L298N的直流电机调速系统proteus仿真)