file:///C:Documents and SettingsAdministratorApplication DataTencentUsers448031894QQWinTempRichOleBV`9EYMV8Z4I7G~1}(GNRUT.jpgfile:///C:Documents and SettingsAdministratorApplication DataTencentUsers448031894QQWinTempRichOleBV`9EYMV8Z4I7G~1}(GNRUT.jpg
#include <stdio.h>
#include <math.h>
#define uc unsigned char
#define ui unsigned int
void chkbusy(void);
void wcode(uc cd) ;
void wdata(uc dat);
void disrow(uc page,uc col,uc *temp);
void display( uc page,uc col,uc *temp);
void ground(step); //转步
void run1(); //正转
void run2();
void stop();
void delay(ui time);
uc scan_key1,scan_key2; //按键功能选择,00 停止,01 正转 10 反转
uc step1;step2;
static step_index;
ui count1,count2; //定时
uc butter; //按键
static speed; //速度参数
void main(void)
{
P2=0xff;
step2=0;
step1=0;
P1=0xff;
P0=0;
EX1=1;
EA=1; //开中断
speed=1500;
while(1)
{
if((scan_key1==1)&(scan_key2==0)) // 正转
{
ground(step_index);
delay(speed);
step_index++; //大于7,从头再来
if(step_index>7)
step_index=0;
}
if((scan_key1==0)&(scan_key2==1)) //反转
{
ground(step_index);
delay(speed);
step_index--;
if(step_index<0) //小于0,从头再来。
step_index=7;
}
if(scan_key1==0&scan_key2==0)
{
P0=0xff;
}
if(step1==1&step2==0)
{
speed=speed-100;
if(speed<1000|speed==1000)
{ speed=1000;
}
}
if(step1==0&step2==1)
{
speed=speed+100;
if(speed>2500|speed==2500)
{ speed=2500;
}
}
}
}
//***************************************************
//************************* 延时子程序**************
//**************************************************
void delay(ui time) ///延时程序
{ for (count1=0;count1<time;count1++ ) //此处可以用中断进行准确定时。我只是瞎定了几个参数
for(count2=0;count2<3;count2++);
}
//*********************************************************
//******************按键处理程序****************************
//************************************************************
void key(void) interrupt 2
{
uc i;
for(i=0;i<200;i++); //延时防抖
if(P3_3==0)
{
butter=~P1;
switch(butter)
{case 0x01: scan_key1=1;scan_key2=0; break; //正常运行 ,用两个数字进行选择,是不错的办法
case 0x02: scan_key1=0;scan_key2=0;break; //停止
case 0x04: scan_key1=0 ;scan_key2=1; break; // 正转
case 0x08: step1=1;step2=0;break; //减速
case 0x10: step1=0;step2=1;break; // 加速
default : ; //其它值返回
}
}
P1=0XFF;
}
//**************************************************
//**********************转步************************
//**************************************************
void ground(step_index) //转步
{
switch(step_index)
{
case 0://0 //
P0_0 = 1; //
P0_1 = 0; //
P0_2 = 0;
P0_3 = 0;
break;
case 1://0,1
P0_0 = 1;
P0_1 = 1;
P0_2 = 0;
P0_3 = 0;
break;
case 2://1
P0_0 = 0;
P0_1 = 1;
P0_2 = 0;
P0_3 = 0;
break;
case 3://1,2
P0_0 = 0;
P0_1 = 1;
P0_2 = 1;
P0_3 = 0;
break;
case 4://2
P0_0 = 0;
P0_1 = 0;
P0_2 = 1;
P0_3 = 0;
break;
case 5://2,3
P0_0 = 0;
P0_1 = 0;
P0_2 = 1;
P0_3 = 1;
break;
case 6://3
P0_0 = 0;
P0_1 = 0;
P0_2 = 0;
P0_3 = 1;
break;
case 7://3,0
P0_0 = 0;
P0_1 = 0;
P0_2 = 0;
P0_3 = 1;
}
}
源程序
是对的~。~两相是黑绿红蓝,不过只是轴轻微震动,而电机里面转子好像也在转,但轴爱上保持初始力矩不动,请问程序上要怎么改?最后转步的程序貌似是根据相位来的,是直接删掉然后就可以了吗??小白求教导
一周热门 更多>