本帖最后由 bsm5632 于 2016-12-25 16:32 编辑
void PWM_Isr() interrupt 22 using 1
{
if(PWMCFG&0x80)
{
PWMCFG&=~0x80; //mtime0++;
P_SW2|=0x80;
if(b==1) // 正转
{
uchar aa; bit a; uint j; // if(ADC_RES>=0x08){PWMFDCR|=0x10;pwm_0=0;} //紧急关闭PWM输出
if(cc1<bu)//CC1是运行计步,BU是规定的步数,CC1没有达到规定步数将在此程序里运行,否则最厚几步慢停止到位后进入刹车程序
{
if(cc1<=t_num){j++;if(j>=t_num&&sdjc>sdxz&&jsnum<=254-minnum){j=0;if(sdjc>sdxz){jsnum++;}}} //头机步慢启动
if(cc1>=bu-t_num){j++;if(j>=t_num&&jsnum>=1){j=0;jsnum--;}} //最后几步慢停止
pwm_0=minnum+jsnum ;
if(pwm_0>=255){pwm_0=255;} if(pwm_0<=0){pwm_0=0;} //占空比范围保护
//if(tmp>=65000){b=2;} //电机堵转保护 pwm_0=minnum+jsnum ;
/***************电机时序*********************/
if(ha==0&&hb==1&&hc==0) // a+c- b=0
{
if(a==0)
{
a=1;
cc1++;//计步
aa=0;//记霍尔位置
sdjc=tmp; //替换出每步所用了多少个PWM
tmp=0;
tmp1=num4;//显示速度用
num4=0;
if(cc1>=t_num&&cc1<=bu-t_num)//运行中段调速
{
if(sdjc<sdxz)//速度超过规定数值减占空比
{
jsnum--;
}
if(sdjc>sdxz)//速度没有达到规定数值增加占空比
{
jsnum++;
}
}
}
PWM5HLD=0x00; PWM2HLD=0x00;PWM1HLD=0x01; PWM0HLD=0x02; PWM3HLD=0x01;PWM4HLD=0x01;tmp++;num4++; PWM5T1=pwm_0; PWM5T2=pwm1; PWM2T1=pwm_0;PWM2T2=pwm1;
}
if(ha==0&&hb==1&&hc==1)// b+c- a=0
{
if(a==1)
{
a=0;cc1++;aa=1;sdjc=tmp;tmp=0;
if(cc1>=t_num&&cc1<=bu-t_num)
{
if(sdjc<sdxz)
{
jsnum--;
}
if(sdjc>sdxz)
{
jsnum++;
}
}
}
PWM5HLD=0x01; PWM2HLD=0x02;PWM1HLD=0x00; PWM0HLD=0x00; PWM3HLD=0x01;PWM4HLD=0x01;tmp++;num4++;PWM1T1=pwm_0; PWM1T2=pwm1; PWM0T1=pwm_0;PWM0T2=pwm1;
}
if(ha==0&&hb==0&&hc==1) // b+a- c=0
{
if(a==0)
{
a=1;cc1++;aa=2;sdjc=tmp;tmp=0;
if(cc1>=t_num&&cc1<=bu-t_num)
{
if(sdjc<sdxz){jsnum--;}
if(sdjc>sdxz){jsnum++;}
}
}
PWM5HLD=0x01; PWM2HLD=0x01;PWM1HLD=0x00; PWM0HLD=0x00;PWM3HLD=0x01;PWM4HLD=0x02;tmp++;num4++; PWM1T1=pwm_0; PWM1T2=pwm1; PWM0T1=pwm_0;PWM0T2=pwm1;
}
if(ha==1&&hb==0&&hc==1) // c+a- b=0
{
if(a==1)
{
a=0;cc1++;aa=3;sdjc=tmp;tmp=0;
if(cc1>=t_num&&cc1<=bu-t_num)
{
if(sdjc<sdxz){jsnum--;}
if(sdjc>sdxz){jsnum++;}
}
}
PWM5HLD=0x01; PWM2HLD=0x01;PWM1HLD=0x01; PWM0HLD=0x02; PWM3HLD=0x00;PWM4HLD=0x00;tmp++;num4++; PWM3T1=pwm_0; PWM3T2=pwm1; PWM4T1=pwm_0;PWM4T2=pwm1;
}
if(ha==1&&hb==0&&hc==0) // c+b- a=0
{
if(a==0)
{
a=1;cc1++;aa=4;sdjc=tmp;tmp=0;
if(cc1>=t_num&&cc1<=bu-t_num)
{
if(sdjc<sdxz){jsnum--;}
if(sdjc>sdxz){jsnum++;}
}
}
PWM5HLD=0x01; PWM2HLD=0x02;PWM1HLD=0x01; PWM0HLD=0x01; PWM3HLD=0x00;PWM4HLD=0x00;tmp++;num4++; PWM3T1=pwm_0; PWM3T2=pwm1; PWM4T1=pwm_0;PWM4T2=pwm1;
}
if(ha==1&&hb==1&&hc==0)// a+b- c=0
{
if(a==1)
{
a=0;cc1++;aa=5;sdjc=tmp;tmp=0;
if(cc1>=t_num&&cc1<=bu-t_num)
{
if(sdjc<sdxz){jsnum--;}
if(sdjc>sdxz){jsnum++;}
}
}
PWM5HLD=0x00; PWM2HLD=0x00;PWM1HLD=0x01; PWM0HLD=0x01; PWM3HLD=0x01;PWM4HLD=0x02;tmp++;num4++; PWM5T1=pwm_0; PWM5T2=pwm1; PWM2T1=pwm_0;PWM2T2=pwm1;
}
}
else
{
sdjc=0; tmp=0; tmp1=0; pwm_0=10; //刹车电流
switch(aa)//根据最后一步位置刹车不动
{
case 0: PWM5HLD=0x00; PWM2HLD=0x00; PWM1HLD=0x01; PWM0HLD=0x02; PWM3HLD=0x01;PWM4HLD=0x01; PWM5T1=pwm_0; PWM5T2=pwm1; PWM2T1=pwm_0; PWM2T2=pwm1; break;
case 1: PWM5HLD=0x01; PWM2HLD=0x02; PWM1HLD=0x00; PWM0HLD=0x00; PWM3HLD=0x01;PWM4HLD=0x01; PWM1T1=pwm_0; PWM1T2=pwm1; PWM0T1=pwm_0; PWM0T2=pwm1; break;
case 2: PWM5HLD=0x01; PWM2HLD=0x01; PWM1HLD=0x00; PWM0HLD=0x00; PWM3HLD=0x01;PWM4HLD=0x02; PWM1T1=pwm_0; PWM1T2=pwm1; PWM0T1=pwm_0; PWM0T2=pwm1; break;
case 3: PWM5HLD=0x01; PWM2HLD=0x01; PWM1HLD=0x01; PWM0HLD=0x02; PWM3HLD=0x00;PWM4HLD=0x00; PWM3T1=pwm_0; PWM3T2=pwm1; PWM4T1=pwm_0; PWM4T2=pwm1; break;
case 4: PWM5HLD=0x01; PWM2HLD=0x02; PWM1HLD=0x01; PWM0HLD=0x01; PWM3HLD=0x00;PWM4HLD=0x00; PWM3T1=pwm_0; PWM3T2=pwm1; PWM4T1=pwm_0; PWM4T2=pwm1; break;
case 5: PWM5HLD=0x00; PWM2HLD=0x00; PWM1HLD=0x01; PWM0HLD=0x01; PWM3HLD=0x01;PWM4HLD=0x02; PWM5T1=pwm_0; PWM5T2=pwm1; PWM2T1=pwm_0; PWM2T2=pwm1; break;
}
}
}
/*****************************下面问题出来了****************************************/
//tmp的数值是无刷电机每步过程中利用PWM中断++的,然后根据sdjc=tmp,sdxz为限速定量,sdjc和sdxz比较,如果sdjc大于sdxz说明速度不够,增加pwm的占空反之减少,
//运行结果很理想,但要把sdjc数值显示出来数值差很多,sdjc实际在250左右。PWM时钟频率 24MHZ/2 /257,大概在47KHZ左右
/***********直接显示sdjc的数值很大,达14000左右********************/
bu_00[0]=sdjc/10000+0x30;
bu_00[1]=sdjc%10000/1000+0x30;
bu_00[2]=sdjc%1000/100+0x30;
bu_00[3]=sdjc%100/10+0x30;
bu_00[4]=sdjc%10+0x30;
display_GB2312_string(3,45,bu_00);
/*********************用kk替换显示数值是0-96之间变动***************/
kk=sdjc;
bu_00[0]=kk/10000+0x30;
bu_00[1]=kk%10000/1000+0x30;
bu_00[2]=kk%1000/100+0x30;
bu_00[3]=kk%100/10+0x30;
bu_00[4]=kk%10+0x30;
display_GB2312_string(3,45,bu_00);
|