init0=0;init1=0;init2=0;init3=0;init4=1; } lcdnumdisplays(0x80+0x40,scantemp); if(ok==1)KD=scantemp; } } } void mypid(float Kp,float Ki,float Kd,uint count,uint point) { static float Uk; ek=point-count; // if(ek>=5&&ek<=-5) //积分分离 {Uk=Kp*(ek-ek1)+Ki*ek+Kd*(ek-2*ek1+ek2);} //增量PID
// else // Uk=Kp*ek; pwm=pwm+Uk; if(pwm>freq)pwm=freq; if(pwm<=0){pwm=0;in1=0;in2=1;} if(pwm>0) {in1=1;in2=0;} ek2=ek1; ek1=ek; } uint read_pul() { uint t1,th1,th2; uint val; while(1) { th1=TH1; t1=TL1; th2=TH1; if(th1==th2) break; } val=th1*256+t1; return val; } void delay(uint n) { uint i,j; for(i=n;i>0;i--) for(j=1;j>0;j--); } void Init(void)//初始化函数 { TMOD=0x51; TH0=(65536-10)/256; TL0=(65536-10)%6; EA=1; ET0=1; TR0=1; //T1计数 TH1=0;
TL1=0; TR1=1; } void Timer_0(void) interrupt 1//中断函数{ static ulong t_count=0; static uint num_count=0; TR0=0; TH0=(65536-10)/256; TL0=(65536-10)%6; TR0=1; num_count++; // t_count++; // if(t_count==2320) // { // t_count=0; // TR1=0; // pul_count=read_pul(); // TH1=0; // TL1=0; // TR1=1; // } if(num_count>freq)num_count=0; //1khz; if(num_count else output=0; } void writecom(uchar com) { lcdrs=0; P0=com; delay(1); lcden=1; delay(1); lcden=0; } void writedata(uchar date) { lcdrs=1; P0=date; delay(1); lcden=1; delay(1); lcden=0; } void initlcd() { lcdrw=0; writecom(0x38);delay(1); writecom(0x0c);delay(1); writecom(0x06);delay(1); writecom(0x01);delay(5); } uint adcread(uchar port) { uint ad=0,i; CLOCK=0; _CS=0; port<<=4; for(i=0;i<12;i++) { if(D_OUT) ad|=0x01; D_IN=(bit)(port&0x80); CLOCK=1; delay(1); CLOCK=0; delay(1); port<<=1; ad<<=1; } _CS=1; ad>>=1; return(ad); } void lcdnumdisplays(uchar pos,float f) //(0.001-99999) 精度低 但方便数据更新 { uchar i; writecom(pos); if(f>65535&&f<0.001) for(i=0;i<5;i++)writedata(0x23);//超出范围 显示# else if(f==0){writedata(0x30);for(i=0;i<4;i++)writedata(0x20);} else { if((uint)f/10000!=0) { writedata((uint)f/10000+0x30); writedata((uint)f000/1000+0x30); writedata((uint)f00/100+0x30); writedata((uint)f0/10+0x30); writedata((uint)f+0x30); } else { if((uint)f/1000!=0) { writedata(0+0x30); writedata((uint)f/1000+0x30); writedata((uint)f00/100+0x30); writedata((uint)f0/10+0x30); writedata((uint)f+0x30); } else { if((uint)f/100!=0) { writedata((uint)f/100+0x30); writedata((uint)f0/10+0x30); writedata((uint)f+0x30); writedata(0x2e); writedata((uint)(f*10)+0x30); } else { if((uint)f/10!=0) { writedata((uint)f/10+0x30); writedata((uint)f+0x30); writedata(0x2e); writedata((uint)(f*10)+0x30); writedata((uint)(f*100)+0x30); } else { writedata((uint)f+0x30); writedata(0x2e); writedata((uint)(f*10)+0x30); writedata((uint)(f*100)+0x30); writedata((uint)(f*1000)+0x30); } } } } } } void lcdnumdisplay(uchar pos,float f0) //(0.00001-99999.99999) { uchar temp; ulong f; writecom(pos); f=(ulong)f0; temp=f/10000; //整数部分 if(temp!=0) { writedata(temp+0x30); writedata(f000/1000+0x30); writedata(f00/100+0x30); writedata(f0/10+0x30); writedata(f+0x30); } else { temp=f000/1000; if(temp!=0) { writedata(temp+0x30); writedata(f00/100+0x30); writedata(f0/10+0x30); writedata(f+0x30); } else { temp=f00/100; if(temp!=0) { writedata(temp+0x30); writedata(f0/10+0x30); writedata(f+0x30); } else { temp=f0/10; if(temp!=0) { writedata(temp+0x30); writedata(f+0x30); } else { temp=f; if(temp!=0) { writedata(temp+0x30); } else writedata(0+0x30); } } } } if((ulong)(f0*10)!=0||(ulong)(f0*100)!=0||(ulong)(f0*1000)!=0||(ulong)(f0*10000)!=0) //小数部分 { writedata(0x2e); temp=(ulong)(f0*10000); if(temp!=0) { writedata((ulong)(f0*10)+0x30); writedata((ulong)(f0*100)+0x30); writedata((ulong)(f0*1000)+0x30); writedata(temp+0x30); } else { temp=(ulong)(f0*1000); if(temp!=0) { writedata((ulong)(f0*10)+0x30); writedata((ulong)(f0*100)+0x30); writedata(temp+0x30); } else { temp=(ulong)(f0*100); if(temp!=0) { writedata((ulong)(f0*10)+0x30); writedata(temp+0x30); } else { temp=(ulong)(f0*10); if(temp!=0) writedata(temp+0x30); } } } } } uchar keyscans(void)//矩阵键盘扫描 { uchar i, j, temp, Buffer[4] = {0xef, 0xdf, 0xbf, 0x7f}; for(j = 0; j < 4; j++) //循环四次 { P1 = Buffer[j]; //在P1高四位分别输出一个低电平 temp = 0x01; //计划先判断P1.0位 for(i = 0; i < 4; i++) { if(!(P1 & temp)) //从P1低四位,截取1位 return (i + j * 4); //返回取得的按键值 temp <<= 1; //判断的位,左移一位 } } return 16; //判断结束,没有键按下,返回 } case 0xd0: {k=2;if(dianflag==0)keytemp=keytemp*10+k ;if(dianflag==1){ float keyscan(void)//矩阵键盘扫描 { static float keytemp=0; diancount++; static uchar dianflag=0; static uchar diancount=0; static uchar k=0; uchar temp; P1=0xfe; temp=P1; temp=temp&0xf0; if(temp!=0xf0) { delay(1); if(temp!=0xf0) { switch(temp) { case 0xe0: {k=1;if(dianflag==0)keytemp=keytemp*10+k;if(dianflag==1){ diancount++; if(diancount==1)keytemp=keytemp+k*0.1; if(diancount==2)keytemp=keytemp+k*0.01; if(diancount==3)keytemp=keytemp+k*0.001; if(diancount==4)keytemp=keytemp+k*0.0001; if(diancount==5)keytemp=keytemp+k*0.00001; }} break; if(diancount==1)keytemp=keytemp+k*0.1; if(diancount==2)keytemp=keytemp+k*0.01; if(diancount==3)keytemp=keytemp+k*0.001; if(diancount==4)keytemp=keytemp+k*0.0001; if(diancount==5)keytemp=keytemp+k*0.00001; }} break; case 0xb0: {k=3;if(dianflag==0)keytemp=keytemp*10+k;if(dianflag==1){ diancount++; if(diancount==1)keytemp=keytemp+k*0.1; if(diancount==2)keytemp=keytemp+k*0.01; if(diancount==3)keytemp=keytemp+k*0.001; if(diancount==4)keytemp=keytemp+k*0.0001;