基于单片机控制的步进电机设是以单片机为主控制模块,从而实现电机的启停、正反转和调速的目的的一个设计。
(1)、5个按键控制整个电路,对应功能分别是:正转、反转、暂停/开始、速度加、速度减;
(2)、数码管显示电机运行速度的档数和正反转的指示;
(3)、5个小红灯一个为电源指示,四个指示电机的转速。
制作出来的实物图如下:
单片机源程序如下:
- //四个按键控制步进电机:正转,反转,加1,减1
- //上电时电机启动,加减档位,电机采用单双八拍方式
- //电机转速一共10档,通过按键调节转速
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit s1 = P1^0;
- sbit s2 = P1^1;
- sbit s3 = P3^0;
- sbit s4 = P3^1;
- sbit s5 = P3^2;//按键定义,s1正转,s2反转,s3加1,s4减1
- sbit a = P2^7;sbit b = P2^6;sbit c = P2^5;sbit d = P2^4;//脉冲信号输入端定义
- uchar code time_counter[10][2]={{0xda,0x1c},{0xde,0xe4},{0xe1,0xec},{0xe5,0xd4},{0xe9,0xbc}, //9.7 ----1ms
- {0xed,0xa4},{0xf1,0x8c},{0xf5,0x74},{0xf9,0x5c},{0xfc,0x18}};
- uchar num1 = 0;//控制取励磁信号变量
- uchar num2 = 8;
- char k=0;//加减档位控制,1为最小档
- char pause=1;//暂停时保存之前的转速,开机默认1
- bit flag1 = 0;//初始正转,正反转标志
- //================================定时器0/1初始化函数================================
- void T0_T1_init()
- {
- TMOD = 0x11;//定时器0/1均工作于方式1,16位计时方式
- TH1 = time_counter[k-1][0];
- TL1 = time_counter[k-1][1];//定时器1,定时10ms用于步进电机转速控制
- TR1 = 0;
- ET1= 1;//开定时器中断
- EA = 1;//开总中断
- }
- //================================ms级延时函数=======================================
- void delay1m(uint x)
- {
- uint i,j;
- for(i=0;i<x;i++) //连数x次,约 x ms
- for(j=0;j<120;j++); //数120 次,约1 ms
- }
- //================================主函数=============================================
- void main()
- {
- T0_T1_init();
- a=b=c=d=0;
- while(1)
- {
- if(s1 == 0)
- {
- delay1m(3);
- if(s1 == 0)
- {
- flag1 = 0;//正转
- }
- while(!s1);
- }
- if(s2 == 0 )
- {
-
- delay1m(3);
- if(s2 == 0)
- {
- flag1 = 1;//反转
- }
- while(!s2);
- }
- if(s3 == 0) //速度加1档
- {
- delay1m(3);
- if(s3 == 0)
- {
- k++;
- TR1=1;
- if(k > 10 )
- {
- k = 1;
- }
- }
- while(!s3);
- }
- if(s4 == 0) //速度减1档
- {
- delay1m(3);
- if(s4 == 0)
- {
- k--;
- TR1=1;
- if(k <= 0)
- {
- k = 10;
- }
- }
- while(!s4);
- }
- if(s5==0)
- {
- delay1m(3);
- if(s5==0)
- {
- if(TR1==1)
- {
- pause=k;
- k=0;
- TR1=0;
- a=b=c=d=0;
- }
- else if(TR1==0)
- {
- k=pause;
- TR1=1;
- }
- while(!s5);
- }
- }
- }
- }
- //==================================定时器1中断函数,用于脉冲频率控制=====================================
- void time1_interrupt()interrupt 3
- {
- static num1 = 0;
- static num2 = 0;
- TH1 = time_counter[k-1][0];
- TL1 = time_counter[k-1][1];//定时器1,定时1 用于步进电机转速控制
- if(flag1 == 0)//正转
- {
- switch(num1)
- {
- case 0:a = 1;b = 0;c = 0;d = 0;break;
- case 1:a = 1;b = 1;c = 0;d = 0;break;
- case 2:a = 0;b = 1;c = 0;d = 0;break;
- case 3:a = 0;b = 1;c = 1;d = 0;break;
- case 4:a = 0;b = 0;c = 1;d = 0;break;
- case 5:a = 0;b = 0;c = 1;d = 1;break;
- case 6:a = 0;b = 0;c = 0;d = 1;break;
- case 7:a = 1;b = 0;c = 0;d = 1;break;
- }
- num1++;
- if(num1 == 8)num1 = 0;
- }
- else //反转
- {
- switch(num2)
- {
- case 0:a = 1;b = 0;c = 0;d = 1;break;
- case 1:a = 0;b = 0;c = 0;d = 1;break;
- case 2:a = 0;b = 0;c = 1;d = 1;break;
- case 3:a = 0;b = 0;c = 1;d = 0;break;
- case 4:a = 0;b = 1;c = 1;d = 0;break;
- case 5:a = 0;b = 1;c = 0;d = 0;break;
- case 6:a = 1;b = 1;c = 0;d = 0;break;
- case 7:a = 1;b = 0;c = 0;d = 0;break;
- }
- num2++;
- if(num2 == 8)num2 = 0;
- }
- }
复制代码
数字化电动机控制技术的发展使电机这个古老的机电能量转换装置得到新的发展和广泛运用,极大的改善了工农业的生产效率,改善了人民的生活水平。步进电动机是纯粹的数字控制电动机,它将电脉冲信号转变成角位移,即给一个脉冲信号,步进电动机就转一个角度,因此非常适合于单片机控制。
|