步进电机角度控制.docx

上传人:b****6 文档编号:5606418 上传时间:2022-12-28 格式:DOCX 页数:15 大小:16.71KB
下载 相关 举报
步进电机角度控制.docx_第1页
第1页 / 共15页
步进电机角度控制.docx_第2页
第2页 / 共15页
步进电机角度控制.docx_第3页
第3页 / 共15页
步进电机角度控制.docx_第4页
第4页 / 共15页
步进电机角度控制.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

步进电机角度控制.docx

《步进电机角度控制.docx》由会员分享,可在线阅读,更多相关《步进电机角度控制.docx(15页珍藏版)》请在冰豆网上搜索。

步进电机角度控制.docx

步进电机角度控制

 

步进电机角度控制

 

1.编程控制步进电机顺时针行走,180度(角度显示在七段LED上),3秒后行走45度(显示角度),3秒后行走90度(显示角度),3秒后行走180度,依次循环。

#include

#defineucharunsignedchar

#defineuintunsignedint

#definePA0x60

#definePB0x61

#definePC0x62

#defineCTRL0x63

ucharpai[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};

ucharteble[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uintm=0;

uchara3,a2,a1,a0=0;

voiddelay(uintxms)

{

uinti,j;

for(i=xms;i>0;i--){

for(j=110;j>0;j--);}

}

voiddisplay()

{

outp(PA,teble[a0]);

outp(PB,0x07);

delay(10);

outp(PA,teble[a1]);

outp(PB,0x0b);

delay(10);

outp(PA,teble[a2]);

outp(PB,0x0d);

delay(10);

outp(PA,teble[a3]);

outp(PB,0x0e);

delay(10);

}

voidzhuan(uintzz)

{

uinttemp,i,j;

a3=zz/1000

a2=(zz%1000)/100;

a1=(zz%100)/10;

a0=(zz%10);

temp=(uint)(zz/15*4);

for(i=temp;i>0;i--)

{

for(j=3;j>0;j--)

display();

outp(PC,pai[m]);

m++;

if(m>7)

m=0;

}

}

voidmain()

{

inti;

outp(CTRL,0x88);

while

(1)

{

zhuan(180);

for(i=75;i>0;i--)

display();

zhuan(45);

for(i=75;i>0;i--)

display();

zhuan(90);

for(i=75;i>0;i--)

display();

}

}

2.完成A/D转换实验并把转换结果在七段LED上显示出来。

#include

#defineucharunsignedchar

#defineuintunsignedint

#definePA0x60

#definePB0x61

#definePC0x62

#defineCTRL0x63

#defineAD0x00

ucharpai[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};

ucharteble[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uintm=0;

uchara3,a2,a1,a0;

voiddelay(uintxms)

{

uinti,j;

for(i=xms;i>0;i--){

for(j=110;j>0;j--);}

}

voiddisplay()

{

outp(PA,teble[a0]);

outp(PB,0x07);

delay(10);

outp(PA,teble[a1]);

outp(PB,0x0b);

delay(10);

outp(PA,teble[a2]);

outp(PB,0x0d);

delay(10);

outp(PA,teble[a3]);

outp(PB,0x0e);

delay(10);

}

voidmain()

{

inttemp=0;

outp(CTRL,0x88);

while

(1)

{

outp(AD,0x0d);

temp=inp(AD);

a3=0;

a2=0;

a1=temp/16;

a0=(temp%16);

display();

}

}

3.如实验指导书(3)。

#include

#defineucharunsignedchar

#defineuintunsignedint

#definePA0x60

#definePB0x61

#definePC0x62

#defineCTRL0x63

#defineAD0x00

ucharpai[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//正转电机时序

ucharpai_fan[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};//反转电机时序

ucharteble[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,//数码管段选

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uintjiao[]={0,0,0};//贮存三次设置的电机角度

uintm=0;//记录电机相序

uintdisp;//数码管显示的数值

uintset_flag,jiao_max,jiao_now,change_flag=1,zhuan_flag;

uintqian,bai,shi,ge;

voiddelay(uintxms)//延时函数,约一毫秒

{

uinti,j;

for(i=xms;i>0;i--){

for(j=110;j>0;j--);}

}

voiddisplay()//显示子程序

{

uchara3,a2,a1,a0;

uinttemp1,temp;

if(change_flag==0)//如果change_flag=0;显示实时AD转换后对应的角度

{//否则显示disp

outp(AD,0x0d);

temp=inp(AD);

if(temp==0)

temp=1;

temp1=(jiao_max*50)/256;

temp1=temp*temp1;

temp1=temp1/50;

disp=temp1;

}

a3=disp/1000;//分别取所显示数字的千,百,十,个位

a2=(disp%1000)/100;

a1=(disp%100)/10;

a0=disp%10;

outp(PA,teble[a0]);//送段选

outp(PB,0x07);//送位选

delay(10);//延时

outp(PA,teble[a1]);

outp(PB,0x0b);

delay(10);

outp(PA,teble[a2]);

outp(PB,0x0d);

delay(10);

outp(PA,teble[a3]);

outp(PB,0x0e);

delay(10);

}

voidfan_zhuan(uintxx)//电机反转程序

{

staticuinttemp,i,j;

temp=((xx*10)/15*4);

temp=temp/10;//计算要转的拍

for(i=temp;i>0;i--)

{

for(j=1;j>0;j--)

display();

outp(PC,pai_fan[m]);//反转

m++;

if(m>7)//8拍,如果等于8拍,从新

m=0;

}

}

voidzhuan(uintzz)//正转

{

staticuinttemp,i,j;

temp=((zz*10)/15*4);

temp=temp/10;

for(i=temp;i>0;i--)

{

for(j=1;j>0;j--)

display();

outp(PC,pai[m]);

m++;

if(m>7)

m=0;

}

}

voidkey_bai()//键百,每按一次加一百,要进位时,自减一千

{

staticuinttemp,temp1;

change_flag=1;

temp=jiao_max/1000;

jiao_max=jiao_max+100;

temp1=jiao_max/1000;

if((temp1-temp)==1)

jiao_max=jiao_max-1000;

disp=jiao_max;

}

voidkey_set()//键SET,

{

staticuinttemp,temp1;

change_flag=0;

outp(AD,0x0d);//读取当前AD值

temp=inp(AD);

if(temp==0)

temp=1;

temp1=(jiao_max*50)/256;//转换成要旋转的角度。

temp1=temp*temp1;

temp1=temp1/50;

if(set_flag==0)

disp=temp1;

else

{

jiao[set_flag-1]=temp1;//存储到JIAO数组中。

disp=temp1;

}

set_flag++;

if(set_flag==4)//存储3个角度

{

set_flag=0;

change_flag=1;

}

}

voidkey_clr()//键CLR,主要是让电机反转回到原来的位置。

{

staticuinttemp;

change_flag=1;

if(zhuan_flag==1)//加个标志,如果没有按下START,则按CLR不反转。

{

zhuan_flag=0;

temp=jiao[0]+jiao[1];

temp=temp+jiao[2];

//temp=temp%360;

disp=temp;

fan_zhuan(temp);

disp=0;

}

set_flag=0;

jiao[0]=0;//清楚角度值

jiao[1]=0;

jiao[2]=0;

}

voidkey_qian()////键百,每按一次加一千,要进位时,自减一万。

{

staticuinttemp,temp1;

change_flag=1;

temp=jiao_max/10000;

jiao_max=jiao_max+1000;

temp1=jiao_max/10000;

if((temp1-temp)==1)

jiao_max=jiao_max-10000;

disp=jiao_max;

}

voidkey_ge()////键百,每按一次加一,要进位时,自减一十

{

staticuinttemp,temp1;

change_flag=1;

temp=jiao_max/10;

jiao_max=jiao_max+1;

temp1=jiao_max/10;

if((temp1-temp)==1)

jiao_max=jiao_max-10;

disp=jiao_max;

}

voidkey_kong()//有一个按键没用到。

{

change_flag=1;

}

voidkey_shi()////键百,每按一次加十,要进位时,自减一百。

{

staticuinttemp,temp1;

change_flag=1;

temp=jiao_max/100;

jiao_max=jiao_max+10;

temp1=jiao_max/100;

if((temp1-temp)==1)

jiao_max=jiao_max-100;

disp=jiao_max;

}

voidkey_start()//开始旋转,转了三个角度后停止。

{

staticuinti;

change_flag=1;

zhuan_flag=1;

set_flag=0;

disp=jiao[0];

zhuan(jiao[0]);

for(i=75;i>0;i--)

display();

disp=jiao[1];

zhuan(jiao[1]);

for(i=75;i>0;i--)

display();

disp=jiao[2];

zhuan(jiao[2]);

for(i=75;i>0;i--)

display();

disp=0;

}

voidkeyscan()//键盘扫描。

{

staticuinttemp,temp1;

outp(PB,0xfe);//setbai

temp=inp(PC);

temp=temp&0xc0;

if(temp!

=0xc0)//有按键按下。

{

switch(temp)

{

case0x80:

key_set();

break;

case0x40:

key_bai();

break;

}

while(temp!

=0xc0)//等待按键放松

{

display();

outp(PB,0xfe);

temp=inp(PC);

temp=temp&0xc0;

}

}

outp(PB,0x0d);

temp=inp(PC);

temp=temp&0xc0;

if(temp!

=0xc0)

{

switch(temp)

{

case0x80:

key_qian();

break;

case0x40:

key_clr();

break;

}

while(temp!

=0xc0)

{

display();

outp(PB,0x0d);

temp=inp(PC);

temp=temp&0xc0;

}

}

outp(PB,0x0b);

temp=inp(PC);

temp=temp&0xc0;

if(temp!

=0xc0)

{

switch(temp)

{

case0x80:

key_kong();

break;

case0x40:

key_ge();

break;

}

while(temp!

=0xc0)

{

display();

outp(PB,0x0b);

temp=inp(PC);

temp=temp&0xc0;

}

}

outp(PB,0x07);

temp=inp(PC);

temp=temp&0xc0;

if(temp!

=0xc0)

{

switch(temp)

{

case0x80:

key_start();

break;

case0x40:

key_shi();

break;

}

while(temp!

=0xc0)

{

display();

outp(PB,0x07);

temp=inp(PC);

temp=temp&0xc0;

}

}

}

voidmain()//主函数。

{

outp(CTRL,0x88);//控制位,PA输出,PB输出,PC高位输入,低位输出

while

(1)

{

keyscan();//调用按键扫描子程序

display();//调用显示子程序。

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1