《步进电机的控制程序.docx》由会员分享,可在线阅读,更多相关《步进电机的控制程序.docx(11页珍藏版)》请在课桌文档上搜索。
1、mega16的,16和32管脚兼容,只不过flash大小不一样,不过中断向量号也不一样,你看下自己改改.时钟频率:内部RCIM芯片:ULN2003键值:O小角度快正转.1小角度快倒.2大角度快转.3大角度快倒.4小角度正慢转.5小角度倒慢转.6大角度正17角到慢*m*m*m*mn*m*m*/#include#include#defineucharunsignedchar#defineuintunsignedintuchara=0,b=0;ucharKEY_num=0xe1;unsignedintm=9000;constucharf1=0x02,0x06,0x04,OxOc,0x08,OxO9Q
2、xO1Qx03;正转时序3.75度constUCharf2=0x04,0x06,0x02,0x03,0x01,0x09,0x08,0x0c;倒转时序3.75度constucharf30=OxO1,0x02,0x04,0x08;正转时序7.5度constucharf4Q=0x01,0x08,0x04,0x02;倒转时序7.5度voiddelay(intk)延时inti;for(i=0;ik;i+);voiddelay_10ms(uintdata)uintm=2;while(data)data-;m=2;while(m)m-;voidZhengZhUanI(VOid)正转3.75度unsigned
3、charj;for(j=0;j8;j+)P0RTC=f1;delay(m);voiddaozhuan1(void)倒转3.75度unsignedcharj;for(j=0;j8;j+)P0RTC=f2j;delay(m);voidZhengZhUan2(Void)正转7.5度unsignedcharj;for(j=0;j4;j+)PORTC=f3j;delay(m);voiddaozhuan2(void)/倒转7.5度unsignedcharj;for(j=0;j4;j+)P0RTC=f4j;delay(m);voidport_int()初始化端口PORTB=OxfO;DDRB=OxOF;DD
4、RC=Oxff;PRTC=0x01;voidinit_devices(void)CLI();/禁止所有中断MCUCR=0x00;MCUCSR=0x80;/禁止JTAGGICR=0x00;portjnt();SEi();开全局中断按键键值读取程序返回按键键值,如果没有按键那么返回0.voidKYY.read()定义按键值存放内存PoRTB=OXfO;/行全部送高电平PORTB=OxfO;if(PINB&OxfO)!=OXf0)有按键delayjms;延时消抖if(PINB&OxfO)!=OxfO)确定有按键按下PORTB=Oxfe;扫描第一行PORTB=Oxfe;if(PINB&OxfO)!=O
5、xfO)KEY_num=(PINB&OxfO)+1;a=9;PoRTB=Oxfd;扫描第二行PORTB=Oxfd;if(PINB&OxfO)!=OxfO)KEY_num=(PINB&0xf0)+2;PoRTB=Oxfb;扫描第三行PORTB=Oxfb;if(PINB&OxfO)!=OxfO)KEY_num=(PINB&0xf0)+4;PORTB=Oxf7;扫描第四行PORTB=Oxf7;if(PINB&OxfO)!=OxfO)KEY_num=(PINB&0xf0)+8;没有按键返回0按键执行程序送如参数:按键键值KEY_do(uchardata)ucharKEY_number=data;swi
6、tch(KEY_number)case0xe1:a=0;b=0;daozhuan1();m=5000;break;case0xd1:a=0;b=1;daozhuan1();m=6000;break;case0xb1:a=0;b=2;daozhuan1();m=7000;break;case0x71:a=0;b=3;daozhuan1();m=8000;break;case0xe2:a=0;b=4;daozhuan2();m=5000;break;case0xd2:a=0;b=5;daozhuan2();m=6000;break;case0xb2:a=0;b=6;daozhuan2();m=70
7、00;break;case0x72:a=0;b=7;daozhuan2();m=8000;break;case0xe4:a=0;b=8;zhengzhuan1();m=5000;break;case0xd4:a=0;b=9;zhengzhuan1();m=6000;break;case0xb4:a=1;b=0;zhengzhuan1();m=7000;break;case0x74:a=1;b=1;zhengzhuan1();m=8000;break;case0xe8:a=1;b=2;zhengzhuan2();m=5000;break;case0xd8:a=1;b=3;zhengzhuan2(
8、);m=6000;break;case0xb8:a=1;b=4;zhengzhuan2();m=7000;break;case0x78:a=1;b=5;zhengzhuan2();m=8000;break;default:b=O;break;)voidmain(void)主程序init_devices();while(1)KYY_read();KEY_do(KEY_num);)/include/51芯片管脚定义头文件#include内部包含延时函数JIOP_();#definecharunsignedchar#defineuintunsignedintucharcodeFFW8=0xf1,0x
9、f3,0xf2,0xf6,0xf410xfc,0xf8,0xf9;ucharcodeREV8=0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0x.;sbitK1=P3A4;正转SbitK2=P3A5;反转sbitK3=P3A6;停止sbitK4=P3A7;sbitBEEP=P0A6;蜂鸣器*厂延时t毫秒尸11.0592MHZ时钟,延时约ImS*7void delay(uint t)mmintk;while(t-)(for(k=0;k125;k+)()VoiddlsyB(uchsr)/x*014MSchari;while(x-)for(i=0;i13;i+)()voidb
10、eep()uchari;for(i=0;i180;i+)delayB(5);BEEP=!BEEP;Z/BEEP取反关闭蜂鸣器BEEP=1;*步进电机正转voidmotor_ffw()uchari;uintj;forG=0;j12;j+)if(K4=0)转1*n圈退出此循环程序break;一个周期转30度for(i=0;i8;i+)取数据PO=FFWi;delay(15);/*步进电机反转voidmotor_rev()uchari;转IXn圈退出此循环程uintj;for(j=0;j12;j+)if(K4=0)break;for(i=0;i8;i+)一个周期转30度取数据调节转Po=REVi;速
11、delay(15);)*主程序main()(/N步进电机运转圈数ucharr,N=5;while(1)P2=0xDF;(if(K1=0)beep();for(r=0;rN;r+)motor_ffw();电机正转if(K4=0)beep();break;退出此循环程序elseif(K2=0)beep();for(r=0;rN;r+)motojrev();电机反转if(K4=O)beep();break;退出此循环程序elsePO=OxTO;r*WWW*W*W*W*WWW*W*WW*WULN2803是8路NPN达林顿连接晶体管系列,特别适用于低逻辑电平数字电路,如:TTL,COMS或PMOS/NM
12、OS,和较高的电压/电流要求之间的接口,广泛应用与计算机、打印机、继电器、灯等类似负载中.所有器件具有集电极开路输出和续流箱位二极管,用于抑制跃变.ULN2803的设计与标准TTL系列兼容.28BYJ48是一款五线四相八拍步进电机,可以采用4-1-2相驱动.电压为DC5V-DC12V.当对步进电机施加一系列连续不断的限制脉冲时,它可以连续不断地转动.每一个脉冲信号对应步进电机的某一相或两相绕组的通电状态改变一次,也就对应转子转过一定的角度(一个步距角).当通电状态的改变完成一个循环时,转子转过一个齿距.四相步进电机可以在不同的通电方式下运行,常见的通电方式有单(单相绕组通电)四拍(A-B-C-
13、D-A.),双(双相绕组通电)四拍(AB-BC-CD-DA-AB-.),八拍(A-AB-B-BC-C-CD-D-DA-A.)具体驱动方式如下表,红色为电源引脚,始终接电源橙黄粉蓝十八制10000x081100OxOc01000x0401100x0600100x0200110x0300010x0110010x09电机电机至群龙小电机和单片机使用网电源,可以断兄边跣然弁在”业机电源G、D-播入苴他也小邛.狸岩电机供电广电源,电机电可i单片机目L电源电机和驱动板连接示意图:6V-PAOPAIPA2PA3-PD3PD2-PDlOCNDPDAVR范例程序如下:芯片使用ATMegaI6,晶振使用12M#i
14、ncludeunsignedcharkey=。;IL匕机的方向unsignedcharspeed=5;步进电机的转速八拍方式驱动,顺序为AABBBCCCDDDAunsignedcharclockWise=0x01,0x03,0x02,006,0x04,00c,0x08,0x09);voiddelay(unsignedcharz)unsignedcharx;unsignedinty;for(x=0;xz;x+)(for(y=0;y750;y+)/z;x+)void.unsignedchari;DDRA=Oxff;PORTA=Oxff;DDRD=Oxff;PORTD=Oxff;DDRB=Oxfe;
15、PINB=Oxff;key=1;PORTA=0x00;PORTD=0x00;whiled)for(i=0;i8;i+)key=PINB&0x01;if(key)、,正转PORTA=clockWisei;PORTD=clkWisei;delay(speed);elsePORTA=clockWise8-i;PORTD=clockWise(8-i;跪求单片机限制2相四线步进电机的启停、正反转,步进电机限制器是L298,单片机是89C52#include/51芯片管脚定义头文件include内部包含延时函数_nop_();#defineucharunsignedchar#defineuintunsig
16、nedintucharcodeFFW8=0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9;ucharcodeREV8=0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1;sbitK1=P3八4;/正转sbitK2=P3八5;反转sbitK3=P3八6;停止SbitK4=P3八7;sbitBEEP=P0a6;/蜂鸣器/mm*mmm*m*m*m*口寸t,5木少*0592MHZ电中JFms/y*/voiddelay(uintt)uintk;while(t-)for(k=0;k1*/voiddeIaB(ucharx)/x*0.14MSuchar
17、i;while(x-)for(i=0;i13;i+)J*uchari;for(i=0;i180;i+)delayB(5);7voidbeep()BEEP=!BEEP;/BEEP取反)BEEP=1;*关闭蜂鸣*/*步进电机正转/万前g箔芳名:传笛力0Q.jv2,j+)/uchari;if(K4=0)i8;i+)/取数据速电机反转uintj;break;一个周期转30度delay(15);1*n圈退出此循睫星FWJrg;调节转/*vojdmotor_rev()uchari;uintj;for(j=0;j12;j+)if(K4=0)break;for(i=0;i8;i+)/取数据速*主程序一个周期转30度delay(15);调节转转IXrl圈退出此循环程序PO=REVi;/*main()/N步进电机运转圈数beep();motor_ffw();beep();break;P2=0xDF;while(1)for(r=0;rN;r+)ucharr,N=5;if(K1=0)电机正转退出此循环程序if(K4=0)elseif(K2=0)beep();motorjev();电机反转elsefor(r=0;rN;r+)if(K4=0)beep();break;退出此循环程序