全部资料下载地址:
直流马达pid调速液晶显示.zip
(68.64 KB, 下载次数: 80)
单片机源程序:
- #include "12864Serial.h"
- #include "PWM.h"
- uint data h_count=0,freq10=0,Speed=0,Speed1=0; //h_count高频脉冲个数 (高)
- uint data num1=0,num2=0,num3=0,num4=0;
- //float data p=4.0,i=0.9,d=1.0;
- float xdata p=3.0,i=20,d=3.0; //P:3 I:20 D:3
- static data ErrorNum=0.0,en=0.0,en1=0.0,en2=0.0;
- static int data output=0;
- float data pwm;
- uint temp40,temp50,temp60;
-
- bit Flag=0;
- uint data config=5,error=0,input=0,adjust=20;
- long int data duty=50;
- uint data TempConfig=0; //定义暂时的一个转速,P、I、D参数
- float xdata TempP=0,TempI=0,TempD=0; //在退出参数调整界面时将这些暂时的数据赋给真实的值
- uchar xdata ConfigTable[3]=0; //设定参数数组
- uchar xdata ErrorTable[3]=0; //误差参数数组
- uchar xdata SpeedTable[6]=0; //速度参数数组
- uchar xdata InputTable[3]=0; //输入参数数组
- uchar xdata DutyTable[4]=0; //占空比参数数组
- uchar xdata AdjustTable[3]=0; //调整参数数组
- uchar xdata PTable[3]=0; //P参数数组
- uchar xdata ITable[3]=0; //I参数数组
- uchar xdata DTable[3]=0; //D参数数组
- uchar xdata PTable1[3]=0;
- uchar xdata ITable1[4]=0;
- uchar xdata DTable1[3]=0;
- //定义控制板的按键值
- sbit SW1= P3^1;
- sbit SW= P3^4;
- sbit K2= P3^5;
- sbit K3= P3^6;
- sbit K4= P3^7;
- sbit D0 = P1^0;
- sbit D1 = P1^1;
- sbit D2 = P1^2;
- sbit D3 = P1^3;
- sbit D4 = P1^4;
- sbit D5 = P1^5;
- sbit D6 = P1^6;
- sbit D7 = P1^7;
- unsigned char KeyScan(void);
- void delay(unsigned int ms)
- {
- unsigned int De_Cnt;
- while( (ms--) != 0)
- {
- for(De_Cnt = 0; De_Cnt < 950; De_Cnt++);
- }
- }
- //****************************************************
- //****************高频测量定时计数器初始化*********** 高频的测量方法较准确
- void init_port()
- {
- TMOD=0x11; //T1计时工作方式1
- IT0=1; //INT0下降沿触发
- IT1=1; //INT1下降沿触发
- EX1=1; //外部中断1打开
- TH1=0xb1;
- TL1=0xe0; //定时初值的计算:50ms*20=1s
- EA=1; //全局中断允许
- ET1=1; //T1中断允许
- TH0=0; //清零
- TL0=0; //清零
-
- ET0=1; //定时器0中断允许
- TR0=0; //关T0中断
- TR1=0; //关T1中断
- }
- //******************高频率测量*****************
- void hight_frequency()
- {
- h_count=0; //脉冲计数初值为0,
- EX0=1; //INT0中断允许,开始计数
- TR1=1; //启动定时器1
- while(EX0); //等待INT0中断关闭
- TR1=0; //关T1中断
- h_count=(h_count-1)*50; //得实际频率
- Speed=h_count/334; //此处采用200线编码器
- if((Speed>300)||(Speed<0))
- Speed=0;
-
- SpeedTable[0]=0x30+(Speed/100); //百位
- SpeedTable[1]=0x30+(Speed%100/10); //十位
- SpeedTable[2]=0x30+(Speed%10); //个位
- SpeedTable[3]=0x20;
-
- }
- void display()
- {
- //显示设定的速度值
- ConfigTable[0]=(0x30+(config/100));
- ConfigTable[1]=(0x30+(config%100/10));
- ConfigTable[2]=(0x30+(config%10));
-
- LCD_Write_String(0,0,"Set Sp");
- LCD_Write_Com(0x80+3);
- LCD_Write_Data(':');
- LCD_Write_Data(ConfigTable[0]);
- LCD_Write_Data(ConfigTable[1]);
- LCD_Write_Data(ConfigTable[2]);
- //显示实际测得的速度
- LCD_Write_Com(0x80+10);
- LCD_Write_Data(':');
- LCD_Write_Data(SpeedTable[0]);
- LCD_Write_Data(SpeedTable[1]);
- LCD_Write_Data(SpeedTable[2]);
-
- LCD_Write_String(0,1,"Err Da");
- //显示设定值与实测值的误差
- if(config>Speed)
- {
- error=config-Speed;
- ErrorTable[0]=(0x30+(error/100));
- ErrorTable[1]=(0x30+(error%100/10));
- ErrorTable[2]=(0x30+(error%10));
- LCD_Write_Com(0xC0+3);
- LCD_Write_Data(':');
- LCD_Write_Data('-');
- LCD_Write_Data(ErrorTable[0]);
- LCD_Write_Data(ErrorTable[1]);
- LCD_Write_Data(ErrorTable[2]);
- }
- else if((Speed>config)||(Speed==config))
- {
- error=Speed-config;
- ErrorTable[0]=(0x30+(error/100));
- ErrorTable[1]=(0x30+(error%100/10));
- ErrorTable[2]=(0x30+(error%10));
- LCD_Write_Com(0xC0+3);
- LCD_Write_Data(':');
- LCD_Write_Data('+');
- LCD_Write_Data(ErrorTable[0]);
- LCD_Write_Data(ErrorTable[1]);
- LCD_Write_Data(ErrorTable[2]);
- }
- //显示调整参数数据
- if((Speed>Speed1)||(Speed==Speed1))
- {
- adjust=Speed-Speed1;
- AdjustTable[0]=(0x30+(adjust/100));
- AdjustTable[1]=(0x30+(adjust%100/10));
- AdjustTable[2]=(0x30+(adjust%10));
- LCD_Write_Com(0xC0+11);
- LCD_Write_Data(':');
- LCD_Write_Data('+');
- LCD_Write_Data(AdjustTable[0]);
- LCD_Write_Data(AdjustTable[1]);
- LCD_Write_Data(AdjustTable[2]);
- }
- else if(Speed1>Speed)
- {
- adjust=Speed1-Speed;
- AdjustTable[0]=(0x30+(error/100));
- AdjustTable[1]=(0x30+(error%100/10));
- AdjustTable[2]=(0x30+(error%10));
- LCD_Write_Com(0xC0+11);
- LCD_Write_Data(':');
- LCD_Write_Data('-');
- LCD_Write_Data(AdjustTable[0]);
- LCD_Write_Data(AdjustTable[1]);
- LCD_Write_Data(AdjustTable[2]);
- }
复制代码
|