#define uchar unsigned char
#define uint unsigned int
#define GPIO_KEY P2
sbit PWM=P1^4;
sbit P10=P1^0;
sbit P12=P1^2;
uchar speed1[4]={"0000"};//设定转速
uchar speed2[3]={"000"};//占空比
uchar speed[]={"0000"};//当前转速
uchar KeyValue=0;
uint AA,count=0,flag;
float pid_p=0.003,pid_i=0.003,pid_d=0.002; //PID三个参数 初值
uint SpeedSet=3000,CurrentSpeed;//设定转速 当前转速
unsigned char pid_val_mid;//pid_val_mid脉冲宽度
unsigned int lastError=0;
long int sumError=0;//sum偏差和
void delay1(unsigned int i)
{
unsigned int j;
for(;i>0;i--)
for(j=0;j<333;j++)
{;}
}
/********************* 键盘扫描*************/
void KeyDown(void)
{
GPIO_KEY=0x0f;
delay1(10);
if(GPIO_KEY!=0x0f)
{
delay1(10);
if(GPIO_KEY!=0x0f)
{
//测试列
GPIO_KEY=0X0F;
delay1(10);
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//测试行
GPIO_KEY=0XF0;
delay1(10);
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
}
}
}
void timer()
{
TMOD=0x11;//定时器0工作方式1.16位,定时器1工作方式1,16位定时;
TH0=0x4b;//50ms初值
TL0=0xfe;
TH1=0xfc;//1msPWM控制
TL1=0x66;
TR1=1;//启动定时器1
ET1=1;//定时器1中断使能
IT0=1;//外部中断下降沿触发
TR0=1; //定时器启动标志
ET0=1; //定时器中断使能
EX0=1; //外部中断使能
EA=1; //全局中断
}
/***********************lcd显示*************/
void display()
{
speed[0]=CurrentSpeed/1000+0x30; //当前转速
speed[1]=CurrentSpeed/100%10+0x30;
speed[2]=CurrentSpeed/10%10+0x30;
speed[3]=CurrentSpeed%10+0x30;
speed1[0]=SpeedSet/1000+0x30;//设定转速
speed1[1]=SpeedSet/100%10+0x30;
speed1[2]=SpeedSet/10%10+0x30;
speed1[3]=SpeedSet%10+0x30;
speed2[0]=pid_val_mid/100+0x30;
speed2[1]=pid_val_mid/10%10+0x30;//占空比
speed2[2]=pid_val_mid%10+0x30;
DispHanzi(0,0,5,"当前转速:");
DispZimu(0,5,4,speed);
DispHanzi(1,0,5,"设定转速:");
DispZimu(1,5,4,speed1);
DispHanzi(3,0,4,"占空比:");//占空比
DispZimu(3,4,3,speed2);
DispHanzi(3,6,1,"%");//占空比
}
/************************电机控制*************/
void keyKZ()
{
if(KeyValue==4)//正转
{
P10=1;
P12=0;
}
if(KeyValue==5)//反转
{
P10=0;
P12=1;
}
if(KeyValue==6)//停车
{
P10=0;
P12=0;
}
if(KeyValue==12)//设定速度加50
SpeedSet+=50;
if(KeyValue==13)//设定速度减50
SpeedSet-=50;
if(KeyValue==14)//设定速度加1
SpeedSet+=1;
if(KeyValue==15)//设定速度减1
SpeedSet-=1;
[1] [2]