步进电机的转子为永磁体,当电流流过定子绕组时,定子绕组产生一矢量磁场。磁场会带动转子旋转一定的角度,使得转子的一对磁场方向与定子的磁场方向一致。改变绕组通电的顺序,电机就会反转。所以控制电动机各相绕组的通电顺序来控制步进电机的转动。
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit LA=P1^0;//电机转子
sbit LB=P1^1;
sbit LC=P1^2;
sbit LD=P1^3;
sbit key1=P3^1;//按键
sbit key2=P3^0;
sbit key3=P3^2;
sbit key4=P3^3;
sbit LSA=P2^2;//数码管
sbit LSB=P2^3;
sbit LSC=P2^4;
#define key1press 1
#define key2press 2
#define key3press 3
#define key4press 4
#define unkeypress 0
uint number;
uint speed=10;
uint speednumber;
uint code motospeed[]={0x38,0x4C,0x60,0x74,0x88,0x9C,0xB0,0xC4,0xD8,0xEC};//档位
uint code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//速度级别
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void chose(uint a)
{
uint temp;
if(!a)
temp=8-number%8;//a=0反转
else
temp=number%8;
switch(temp)//转子位置,正转
{
case 0:LA=1,LB=0,LC=0,LD=0;break;
case 1:LA=1,LB=1,LC=0,LD=0;break;
case 2:LA=0,LB=1,LC=0,LD=0;break;
case 3:LA=0,LB=1,LC=1,LD=0;break;
case 4:LA=0,LB=0,LC=1,LD=0;break;
case 5:LA=0,LB=0,LC=1,LD=1;break;
case 6:LA=0,LB=0,LC=0,LD=1;break;
case 7:LA=1,LB=0,LC=0,LD=1;break;
default:LA=0,LB=0,LC=0,LD=0;break;
}
}
int scan( uint mode)//按键扫描函数
{
static uint key=1;
if(mode) key=1;
if(key==1&&(key1==0||key2==0||key3==0||key4==0))
{
key=0;
delayms(2);
{
if(key1==0)
return key1press;
if(key2==0)
return key2press;
if(key3==0)
return key3press;
if(key4==0)
return key4press;
}
}
if(key1==1&&key2==1&&key3==1&&key4==1)
key=1;
return unkeypress;
}
void init()
{
TMOD=0x01;
TH0=0xFF;//100US
TL0=motospeed[0];
ET0=1;
EA=1;
TR0=1;
}
void main()//主函数
{
uint keyscan;
uint dir=1;
init();//定时
while(1)
{
LSC=0,LSB=0,LSA=0;
keyscan=scan(1);//一次按下多次扫描或一次扫描
if(keyscan==key1press)//正转
{
chose(dir);
}
if(keyscan==key2press)//反转
{
chose(!dir);
}
if(keyscan==key3press)//加速
{
TR0=0;
P0=0x00;
speednumber=(speednumber+1)%10;
TL0=motospeed[speednumber];
P0=table[speednumber];
TR0=1;
}
if(keyscan==key4press)//减速
{
TR0=0;
P0=0x00;
speednumber=(speednumber-1)%10;
TL0=motospeed[speednumber];
P0=table[speednumber];
TR0=1;
}
}
}
void imp_time() interrupt 1
{
TH0=0xFF;
TL0=motospeed[speednumber];
number++;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)