作者:到几百元_309 | 来源:互联网 | 2024-11-02 17:09
本文基于Proteus平台,对步进电机的仿真与控制技术进行了深入研究。通过修改代码,实现了步进电机精确转动1圈的控制。实验结果验证了该方法的有效性和可靠性,为步进电机在精密控制领域的应用提供了有力支持。
步进电机圈数测试
下面代码已经修改过,圈数改为1圈
#include
#define uint unsigned int
#define uchar unsigned char
uchar code FFW[]=
{0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
};uchar code REV[]=
{0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
};sbit K1 = P3^0;
sbit K2 = P3^1;
sbit K3 = P3^2;void DelayMS(uint ms)
{uchar i;while(ms--){for(i&#61;0;i<120;i&#43;&#43;);}
}void SETP_MOTOR_FFW(uchar n)
{uchar i,j;for(i&#61;0;i<1*n;i&#43;&#43;) {for(j&#61;0;j<8;j&#43;&#43;){if(K3 &#61;&#61; 0) break;P1 &#61; FFW[j];DelayMS(100);}}
}void SETP_MOTOR_REV(uchar n)
{uchar i,j;for(i&#61;0;i<5*n;i&#43;&#43;){for(j&#61;0;j<8;j&#43;&#43;){if(K3 &#61;&#61; 0) break;P1 &#61; REV[j];DelayMS(25);}}
}void main()
{uchar N &#61; 1;while(1){if(K1 &#61;&#61; 0){P0 &#61; 0xfe;SETP_MOTOR_FFW(N);if(K3 &#61;&#61; 0) break;}else if(K2 &#61;&#61; 0){P0 &#61; 0xfd;SETP_MOTOR_REV(N);if(K3 &#61;&#61; 0) break;}else{P0 &#61; 0xfb;P1 &#61; 0x03;}}
}
步进电机速度测试&#xff08;用延迟进行调速&#xff09;&#43;按键控制&#43;数码管显示延迟时间
上图有点杂乱&#xff0c;下面代码只用到&#xff1a;数码管&#xff0c;AT89c51芯片&#xff0c;步进电机&#xff0c;ULN2003A驱动&#xff0c;按键
#include "main.h"
#include
#include "Stepper motor.h"unsigned int Sec_5&#61;0,Sec_4&#61;0,Sec_3&#61;0 ,Sec_2 &#61; 0,Sec_1 &#61; 0,Sec_0&#61;0;
code unsigned char table[] &#61; { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
code unsigned char chage[] &#61; { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf };
unsigned int c &#61; 0;
unsigned int cnt &#61; 0;
unsigned long int time &#61; 1;
unsigned int num ;
unsigned int Sec &#61; 0;
unsigned int mode &#61; 1;
void main()
{Init_Timer0();while(1){scan_key();if(mode&#61;&#61;0)Phase_B();if(mode&#61;&#61;1)Phase_A();if(mode&#61;&#61;2)move&#61;0x03;delay_10um(time);}
}
void Init_Timer0()
{TMOD &#61; 0X01; TH0 &#61; (65536 - 10000)/256;TL0 &#61; (65536 - 10000)%256;TR0 &#61; 1; ET0 &#61; 1; EA &#61; 1;
}
void Timer0() interrupt 1
{TH0 &#61; (65536 - 1000)/256;TL0 &#61; (65536 - 1000)%256;Sec&#43;&#43;;cnt&#43;&#43;;num&#61;time;
Sec_0 &#61; num%10;Sec_1 &#61; num/10%10;Sec_2 &#61; num/100%10;Sec_3 &#61; num/1000%10;Sec_4 &#61; num/10000%10;Sec_5 &#61; num/100000;
if(cnt&#61;&#61;10){cnt&#61;0;SEG_DATA&#61;0x00;delay_10um(10);COM0&#61;chage[c]; switch(c){case 5:SEG_DATA&#61;table[Sec_0];break;case 4:SEG_DATA&#61;table[Sec_1];break;case 3:SEG_DATA&#61;table[Sec_2];break;case 2:SEG_DATA&#61;table[Sec_3];break;case 1:SEG_DATA&#61;table[Sec_4];break;case 0:SEG_DATA&#61;table[Sec_5];break;default:break;}c&#43;&#43;;if(c&#61;&#61;6)c&#61;0;}
if(Sec&#61;&#61;1000){Sec&#61;0; }
}
void scan_key()
{if(key1&#61;&#61;0){mode&#61;1;}
if(key2&#61;&#61;0){mode&#61;0;}
if(key3&#61;&#61;0){mode&#61;2;}while(key3&#61;&#61;0);if(key4&#61;&#61;0){time-&#61;1;delay_10um(10);if(time&#61;&#61;-1)time&#61;100;
}
}
void Delay_ms(unsigned int n)
{unsigned int i&#61;0,j&#61;0;for(i&#61;0;i<n;i&#43;&#43;)for(j&#61;0;j<123;j&#43;&#43;);
}
void delay_10um(unsigned int n)
{while(n--);
}
main.c头文件
#ifndef __MAIN__H__
#define __MAIN__H__#include
#define COM0 P0
#define SEG_DATA P2sbit key1&#61;P3^0;
sbit key2&#61;P3^1;
sbit key3&#61;P3^2;
sbit key4&#61;P3^3;
extern void Delay_ms( unsigned int m);
extern void delay_10um( unsigned int n);
extern void Init_Timer0();
extern void scan_key();
extern void forward();
extern unsigned long int time;#endif
下面是步进电机控制程序&#xff0c;每执行一次&#xff0c;转一个指定的角度
#include "Stepper motor.h"unsigned int a&#61; 0;
unsigned char code right[8]&#61;{0x03,0x02,0x06,0x04,0x0c,0x08,0x09,0x01,}; void Phase_A()
{
Stepper motor.h头文件
#ifndef __STEPPER MOTOR__H__
#define __STEPPER MOTOR__H__#include #define move P1
extern void Phase_A();
extern void Phase_B();#endif