#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
uchar code table[10]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共陰數碼管顯示碼(0-9)
sbit xiaoshudian=P0^7;
sbit wei1=P2^4; //數碼管位選定義
sbit wei2=P2^5;
sbit wei3=P2^6;
sbit wei4=P2^7;
sbit beep=P2^3; //蜂鳴器控制端
sbit motor = P1^0; //電機控制
sbit s1_jiasu = P1^4; //加速按鍵
sbit s2_jiansu= P1^5; //減速按鍵
sbit s3_jiting=P1^6; //停止/開始按鍵
uint pulse_count; //INT0接收到的脈衝數
uint num=0; //num相當於佔空比調節的精度
uchar speed[3]; //四位速度值存儲
float bianhuasudu; //當前速度(理論計算值)
float reallyspeed; //實際測得的速度
float vv_min=0.0;vv_max=250.0;
float vi_Ref=60.0; //給定值
float vi_PreError,vi_PreDerror;
uint pwm=100; //相當於佔空比標誌變量
int sample_time=0; //採樣標誌
float v_kp=1.2,v_ki=0.6,v_kd=0.2; //比例,積分,微分常數
void delay (uint z)
{
uint x,y;
for(x=z;x>0;x--)
for (y=20;y>0;y--);
}
void time_init()
{
ET1=1; //允許定時器T1中斷
ET0=1; //允許定時器T0中斷
TMOD = 0x15; //定時器0計數,模式1;定時器1定時,模式1
TH1 = (65536-100)/256; //定時器1值,負責PID中斷 ,0.1ms定時
TL1 = (65536-100)%6;
TR0 = 1; //開定時器
TR1 = 1;
IP=0X08; //定時器1為高優級
EA=1; //開總中斷
}
void keyscan()
{
float j;
if(s1_jiasu==0) //加速
{
delay(20);
if(s1_jiasu==0)
vi_Ref+=10;
j=vi_Ref;
}
while(s1_jiasu==0);
if(s2_jiansu==0) //減速
{
delay(20);
if(s2_jiansu==0)
vi_Ref-=10;
j=vi_Ref;
}
while(s2_jiansu==0);
if(s3_jiting==0)
{
delay(20);
motor=0;
P1=0X00;
P3=0X00;
P0=0x00;
}
while(s3_jiting==0);
}
float v_PIDCalc(float vi_Ref,float vi_SpeedBack)
{
register float error1,d_error,dd_error;
error1=vi_Ref-vi_SpeedBack; //偏差的計算
d_error=error1-vi_PreError; //誤差的偏差
dd_error=d_error-vi_PreDerror; //誤差變化率
vi_PreError=error1; //存儲當前偏差
vi_PreDerror=d_error;
bianhuasudu=(v_kp*d_error+v_ki*vi_PreError+v_kd*dd_error);
return (bianhuasudu);
}
void v_Display()
{
uint sudu;
sudu=(int)(reallyspeed*10); //乘以10之後強制轉化成整型
speed[3]=sudu/1000; //百位
speed[2]=(sudu00)/100; //十位
speed[1]=(sudu0)/10; //個位
speed[0]=sudu; //小數點後一位
wei1=0; //第一位打開
P0=table[speed[3]];
delay(5);
wei1=1; //第一位關閉
wei2=0;
P0=table[speed[2]];
delay(5);
wei2=1;
wei3=0;
P0=table[speed[1]];
xiaoshudian=1;
delay(5);
wei3=1;
wei4=0;
P0=table[speed[0]];
delay(5);
wei4=1;
}
void BEEP()
{
if((reallyspeed)>=vi_Ref+5||(reallyspeed
{
beep=~beep;
delay(4);
}
}
void main()
{
time_init();
motor=0;
while(1)
{
v_Display();
BEEP();
}
if(s3_jiting==0) //對按鍵3進行掃描,增強急停效果
{
delay(20);
motor=0;
P1=0X00;
P3=0X00;
P0=0x00;
}
while(s3_jiting==0);
}
void timer0() interrupt 1
{
}
void timer1() interrupt 3
{
TH1 = (65536-100)/256; //1ms定時
TL1 = (65536-100)%6;
sample_time++;
if(sample_time==5000) //採樣時間0.1ms*5000=0.5s
{
TR0=0; //關閉定時器0
sample_time=0;
pulse_count=TH0*255+TL0; //保存當前脈衝數
keyscan(); //掃描按鍵
reallyspeed=pulse_count/(4*0.6); //計算速度
pwm=pwm+v_PIDCalc(vi_Ref,reallyspeed);
if(pwm
if(pwm>100)pwm=100;
TH0=TL0=0;
TR0=1; //開啟定時器0
}
num++;
if(num==pwm) //此處的num值,就是佔空比
{
motor=0;
}
if(num==100) //100相當於佔空比調節的精度
{
num=0;
motor=1;
}
}