本文主要是介绍基于单片机的四相步进电机控制设计(电路+程序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
博主福利:100G+电子设计学习资源包!
http://mp.weixin.qq.com/mp/homepage?__biz=MzU3OTczMzk5Mg==&hid=7&sn=ad5d5d0f15df84f4a92ebf72f88d4ee8&scene=18#wechat_redirect
--------------------------------------------------------------------------------------------------------------------------
步进电机控制应用实例
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
设计要求:
- 采用定时中断方式控制步进机的转速,定时单位时长为10ms。
-
速度设置在1/2/3/4四个档位,1档最快,依次递减,能够控制步进电机的停、转和方向,同时显示步进机的当前档位。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
设计思路:
- 通过P1控制步进电机的转动,P1.0->a,P1.1->b,P1.2->c,P1.3->d,ULN2003A作为步进电机驱动
- P2.0-P2.3对应数码管显示(使用7447芯片对应将BCD码转换成7端LED数码管),通过7447简单驱动数码管(P2只需要发送1/2/3/4即可)
- 通过定时器计数,到达设定值时步进电机转动1/4圈,计数清零
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
元件清单:
- AT89C51
- BUTTON
- MOTOR-STEPPER
- RESPACK-8
- 7SEG-COM-ANODE
- ULN2003A
- 7447
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
实现代码:
#include <reg52.h>
typedef unsigned char uchar;
uchar direction = 0; //0为正转,1为反转
uchar onoff = 0; //关为0,开为1
uchar n = 0; //每次定时器中断触发时n++,当n==max时电机转动1/4
uchar max = 10;
uchar index = 0; //通过index指示电机转动,AB,BC,CD,DA
sbit LED = P3 ^ 7; //当n==max时LED闪烁提示
int main()
{/* LED */P2 = 0xff;/* 初始化中断 */EA = 1;EX0 = 1; //要用到的中断是两个外部中断和定时器中断0IT0 = 1;EX1 = 1;IT1 = 1;ET0 = 1;/* 定时器0 */TMOD = 0x01; //使用定时器0,方式1TH0 = (65536 - 10000) / 256; //12MHz晶振下,定时器为10ms触发中断,更方便观察转动情况TL0 = (65536 - 10000) % 256;TR0 = 1;while(1){switch(P0){case 0xfe: //11111110max = 1;P2 = 1;break;case 0xfd: //11111101max = 10;P2 = 2;break;case 0xfb: //11111011max = 50;P2 = 3;break;case 0xf7: //11110111max = 100;P2 = 4;break;}}
}
void Stop()interrupt 0
{onoff++;if (onoff > 1)onoff = 0;
}
void int1()interrupt 2
{direction++;if (direction > 1)direction = 0;
}
void delay50ms(void)
{unsigned char a, b;for(b = 173; b > 0; b--)for(a = 143; a > 0; a--);
}
void time()interrupt 1
{TH0 = (65536 - 10000) / 256;TL0 = (65536 - 10000) % 256;if (onoff == 1) //在开状态下{if (n == max) //设定档位速度,经过了max次中断后,步进电机转1/4圈{LED = 0; //每次n==max时LED闪烁delay50ms();LED = 1;if (direction == 0){switch(index) //正转时以AB,BC,CD,DA顺序{case 0:P1 = 0x03;break; //00000011 AB高电平case 1:P1 = 0x06;break; //00000110 BC高电平case 2:P1 = 0x0c;break; //00001100 CD高电平case 3:P1 = 0x09;break; //00001001 DA高电平}index ++; if (index == 4) //步进电机转完一圈时index清零index = 0;n = 0; //到达max,n重置为0}if (direction == 1){switch(index) //反转时以DA,CD,BC,AB顺序{case 0:P1 = 0x09;break;case 1:P1 = 0x0c;break;case 2:P1 = 0x06;break;case 3:P1 = 0x03;break;}index ++;if (index == 4)index = 0;n = 0;}else n = 0;}n++; //n!=max时,n++}else n = 0; //在关状态下,n始终为0,无法触发电机转动
}
这篇关于基于单片机的四相步进电机控制设计(电路+程序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!