完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
*******************************************************************/ /* 程序名: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反转 ***it Key_add=P2 ^ 0; //电机减速 ***it Key_dec=P2 ^ 1; //电机加速 ***it Key_turn=P2 ^ 2; //电机换向 ***it PWM1=P2^6;//PWM 通道 1,反转脉冲 ***it PWM2=P2^7;//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 <= 0) { count0 = 0; } } while(!Key_dec ); } } /************电机正反向控制**************/ void Motor_turn(void) { if(Key_turn == 0) { Delay(10); if(Key_turn == 0) { Flag = ~Flag; } while(!Key_turn); } } /***********定时器0初始化***********/ void Timer0_init(void) { TMOD=0x01; //定时器0工作于方式1 TH0=TH0_TL0/256; TL0=TH0_TL0%256; TR0=1; ET0=1; EA=1; } /*********主函数********************/ void main(void) { Timer0_init(); while(1) { Motor_turn(); Motor_speed_high(); Motor_speed_low(); } } /**************定时0中断处理******************/ void Timer0_int(void) interrupt 1 using 1 { TR0 = 0;//设置定时器初值期间,关闭定时器 TL0 = TH0_TL0 % 256; TH0 = TH0_TL0 / 256 ;//定时器装初值 TR0 = 1; if(Time_delay != 0)//延时函数用 { Time_delay--; } if(Flag == 1)//电机正转 { PWM1 = 0; if(++count1 < count0) { PWM2 = 1; } else PWM2 = 0; if(count1 >= 100) { count1=0; } } else //电机反转 { PWM2 = 0; if(++count1 < count0) { PWM1 = 1; } else PWM1 = 0; if(count1 >= 100) { count1=0; } } } //----------------------------------------------------------------------------- #include #include // Function Prototypes //----------------------------------------------------------------------------- #define CMD_RESET 0xA4 //HD7279复位 #define DECODE1 0xc8 //方式0译码 ***it cs=P1^3; ***it clk=P1^2; ***it dat=P1^1; ***it key=P1^0; ***it led_D1003=P0^7; ***it sw1=P1^7; ***it sw2=P1^6; ***it sw3=P1^5; ***it sw4=P1^4; 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 value unsigned int tmr,Speed_evaluate; unsigned char num,num1,num2,num3,a; unsigned int Speed,pi,Speed2; unsigned int Speed1[10]; 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 timer led_D1003=0; PORT_Init (); // Initialize cros***ar and GPIO OSCILLATOR_Init (); // Initialize oscillator PCA0_Init (); // Initialize PCA0 IP=0x82; //定时器中断0高于外部中断0 Timer0_Init(); Ext_Interrupt_Init (); for (tmr=0;tmr<0x2000;tmr++); send_byte(CMD_RESET);//HD7279复位// Globally enable interrupts EA = 1; sPID.SetPoint=70; while (1) { delay10ms(100); //键盘程序 ------------------------------------------------------------- if(sw1==0) //按键1是否按下 { if(sw1==0)//再次检查按键 { num++; //若按键按下,num加1 if(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); } |
|
相关推荐 |
|
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 19:43 , Processed in 0.632450 second(s), Total 41, Slave 32 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号