步进电机正反转设计
设计要求: 设置开始、停止以及正反转键。转速以及转向由数码管显示。
#include <reg52.h>
#define uc unsigned
unsigned char code F_Rotation[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};
unsigned char code B_Rotation[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};
uc code x[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x80,0x00,0x40};
int shumaguan[8]={0};
bit is_action = 0;
bit correct = 1;
int hz = 150;
sbit k1 = P3^1;
sbit k2 = P3^0;
sbit k3 = P3^2;
sbit k4 = P3^3;
sbit lsa = P2^2;
sbit lsb = P2^3;
sbit lsc = P2^4;
int num = 0;
int time = 0;
int mod = 0;
float zhuansu = 0;
void delayms(int xms)
{
unsigned int x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void delay10um(int h)
{
while(h--);
}
void display()
{
uc i;
if(correct == 0)
{
shumaguan[2] = 12;
}
else
{
shumaguan[2] = 11;
}
if(mod == 0)
{
shumaguan[3] = 0;
shumaguan[5] = 0;
shumaguan[6] = 0;
shumaguan[7] = 0;
}
for(i=0;i<8;i++)
{
switch(7-i)
{
case (0): lsa=0,lsb=0;lsc=0;break;
case (4): lsa=0,lsb=0;lsc=1;break;
case (2): lsa=0,lsb=1;lsc=0;break;
case (6): lsa=0,lsb=1;lsc=1;break;
case (1): lsa=1,lsb=0;lsc=0;break;
case (5): lsa=1,lsb=0;lsc=1;break;
case (3): lsa=1,lsb=1;lsc=0;break;
case (7): lsa=1,lsb=1;lsc=1;break;
}
P0=x[shumaguan[i]];
delay10um(80);
P0=0x00;
}
}
void scan()
{
if(!(k1 & k2 & k3 & k4 ) )
{
delayms(10);
if(!k1)
{
mod = mod + 1 ;
is_action = 1;
if(mod == 3)
{
mod = 0;
}
}
if(!k2)
{
is_action = 1;
if(mod == 1)
{
correct = 1;
}
if(mod == 2)
{
hz = hz - 10 ;
}
}
if(!k3)
{
if(mod == 1)
{
correct = 0;
}
if(mod == 2)
{
hz = hz + 10 ;
}
}
if(!k4)
{
is_action = 0 ;
mod = 0;
}
}
while(!(k1 & k2 & k3 & k4 )) ;
shumaguan[0] = mod ;
}
void motor_zheng(void)
{
char i =0;
for(i=0;i<8;i++)
{
P1=F_Rotation[i];
num++;
delay10um(hz);
}
}
void motor_fan(void)
{
char i =0;
for(i=0;i<8;i++)
{
P1=B_Rotation[i];
num++;
delay10um(hz);
}
}
void xuanzhuan()
{
if(correct == 1)
{
motor_zheng();
}
if(correct == 0)
{
motor_fan();
}
}
void tim0_init()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ET0=1;
EA = 1;
TR0=1;
}
void jiexi(float t)
{
char ge=0,gef=0,shif=0,baif=0,qianf=0;
ge = t ;
gef = (t-ge)*10;
shif = (t*1000 - ge*1000- gef*100)/10;
baif = t*1000 - ge*1000- gef*100-shif*10;
shumaguan[3] = ge;
shumaguan[5] = gef;
shumaguan[6] = shif;
shumaguan[7] = baif;
}
void main()
{
tim0_init();
shumaguan[4] = 10;
shumaguan[1] = 11;
while(1)
{
scan();
display();
if(is_action != 0)
{
xuanzhuan();
}
}
}
void timer_0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
time++ ;
if(num == 1024)
{
num = 0;
zhuansu = 4*time/1000.0;
time = 0;
jiexi(zhuansu);
}
}
|