PWM直流电机调速单片机程序.doc
|*/* 程序名:PWM 直流电机调速 */* 晶振:11.00592 MHz CPU 型号:AT89C51 */* 直流电机的 PWM 波控制,可以直接的调速从 0 到 20 级的调速 */*/ #include#define TH0_TL0 (65536-1000)/设定中断的间隔时长unsigned char count0 = 50;/低电平的占空比unsigned char count1 = 0;/高电平的占空比bit Flag = 1;/电机正反转标志位,1 正转,0 反转sbit Key_add=P2 0; /电机减速sbit Key_dec=P2 1; /电机加速sbit Key_turn=P2 2; /电机换向sbit PWM1=P26;/PWM 通道 1,反转脉冲sbit PWM2=P27;/PWM 通道 2,正转脉冲unsigned char Time_delay;/*函数声明*/void Delay(unsigned char x);void Motor_speed_high(void);void Motor_speed_low(void);void Motor_turn(void);void Timer0_init(void);/*延时处理*/void Delay(unsigned char x)Time_delay = x;while(Time_delay != 0);/等待中断,可减少 PWM 输出时间间隔/*按键处理加 pwm 占空比,电机加速*/void Motor_speed_high(void)/if(Key_add=0)Delay(10);|if(Key_add=0)count0 += 5;if(count0 >= 100)count0 = 100; while(!Key_add);/等待键松开/*按键处理减 pwm 占空比,电机减速*/void Motor_speed_low(void)if(Key_dec=0)Delay(10);if(Key_dec=0)count0 -= 5;if(count0 = 100)count1=0;else /电机反转PWM2 = 0; if(+count1 = 100)count1=0;/-#include / SFR declarations#include / Function Prototypes/-#define CMD_RESET 0xA4 /HD7279 复位#define DECODE1 0xc8 /方式 0 译码sbit cs=P13;sbit clk=P12;|sbit dat=P11;sbit key=P10;sbit led_D1003=P07;sbit sw1=P17;sbit sw2=P16;sbit sw3=P15;sbit sw4=P14;void long_delay(void); /延时函数void short_delay(void);void delay10ms(unsigned char);void write7279(unsigned char,unsigned char); /HD7279 写指令void send_byte(unsigned char); void delay(unsigned char);void disp1(unsigned int);void OSCILLATOR_Init (void);void PORT_Init (void);void PCA0_Init (void);void Timer0_Init(void);void Ext_Interrupt_Init (void);|/-/ Global Variables/-/-/ main() Routine/-unsigned int CEX0_Compare_Value; / Holds current PCA compare valueunsigned int tmr,Speed_evaluate; unsigned char num,num1,num2,num3,a;unsigned int Speed,pi,Speed2; unsigned int Speed110; typedef struct double SetPoint; /* 设定目标Desired Value */ double Proportion; /* 比例常数Proportional Const */ double Integral; /* 积分常数Integral Const */ double Derivative; /* 微分常数 Derivative Const */ |double LastError; /* 前一项误差*/ double PrevError; /* 前第二项误差*/double SumError; /* 误差和*/ PID; double PIDCalc( PID *pp, double NextPoint ) double dError,Error,Pout; Error = pp->SetPoint - NextPoint; /* ? */ pp->SumError += Error; /* ?/ dError = pp->LastError - pp->PrevError; /* ? */ pp->PrevError = pp->LastError; pp->LastError = Error; /* ? */ Pout= pp->Proportion * Error + pp->Integral * pp->SumError + pp->Derivative * dError ;if(Pout>1100)Pout=1000;if(Pout<100)Pout=100;return (Pout); | PID sPID; /定义 PID 结构体变量double rOut; /PID 响应输出 unsigned char rIn; /设置 PID 反馈值 double x; double sumout; unsigned char dd; /设置 PID 输出 void main (void) sPID.Proportion = 0.44; /设置 P sPID.Integral = 0.70; /设置 I sPID.Derivative = 0.0; /设置 D /sPID.SetPoint = CEX0_Compare_Value; /sPID.SetPoint = CEX0_Compare_Value; /设置 PID 输出 PCA0MD = 0x00; / Disable watchdog timerled_D1003=0;PORT_Init (); / Initialize crossbar and GPIOOSCILLATOR_Init (); / Initialize oscillator|PCA0_Init (); / Initialize PCA0IP=0x82; /定时器中断 0 高于外部中断 0Timer0_Init();Ext_Interrupt_Init ();for (tmr=0;tmr<0x2000;tmr+);send_byte(CMD_RESET);/HD7279 复位/ Globally enable interruptsEA = 1; sPID.SetPoint=70;while (1) delay10ms(100);/键盘程序 -if(sw1=0) /按键 1 是否按下if(sw1=0)/再次检查按键num+; /若按键按下,num 加 1if(num=1) /到 9 归 0 num=0;|while(sw1=0);/按键释放/ delay10ms(100);write7279(DECODE1+4,num); /将 num 写入 HD7279 第 1 位delay10ms(1);if(sw2=0)if(sw2=0)num1+;if(num1=2)num1=0;while(!sw2);/ delay10ms(100);