微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机驱动步进电机protues仿真

51单片机驱动步进电机protues仿真

时间:11-30 来源:互联网 点击:
电路图:

protues仿真文件和完整代码下载地址:http://www.51hei.com/bbs/dpj-20399-1.html
下面是部分程序代码:
#include"reg51.h"
#include"intrins.h"
#include"math.h"
#include"absacc.h"
#define PORTA XBYTE[0x8500]
#define PORTB XBYTE[0x8502]
#define PORTC XBYTE[0x8504]
#define PORTC0 XBYTE[0x8506]
#definenop_nop_()
#define Right_RUN 1
#define Left_RUN 0
unsignedlongRunSpeed=76;
unsigned char RUNState=1;
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit P17=P1^7;
bitflag=0;
unsigned char xx[]="speed(n/min):";
unsigned char SD[]="76";
char state[2][14]={"direction:cw","direction:ccw"};
unsigned char FLG,INMA=0;
unsigned char t,s=0x01;
unsigned int a;
main()
{
void OPJIAN();
unsigned char JIAN();
unsigned char JIANZHI();
void speed();
void SHOW_LCD();
void show_state();
void write1();
void writ2(unsigned char i);
void delay();
void time();
void busy();
unsigned char i;
i=0x90;
PORTC0=i;
i=0x01;
TMOD=i;
t=0x10;
a=0x0f;
TH0=0x10;
TL0=0x0f;
FLG=INMA;
SHOW_LCD();
show_state();
EA=1;
ET0=1;
TR0=1;
while(1)
{
i=JIAN();
if(i!=0)
{
time();
time();
}
i=JIAN();
if(i!=0)
{
INMA=JIANZHI();
OPJIAN();
time();
}
if(FLG!=INMA||flag==1)
{ FLG=INMA;
SHOW_LCD();
show_state();
flag=0;
}
}
}
void SHOW_LCD()/*lcd设置程序*/
{
P1=0x38;
write1();
delay();
P1=0x38;
write1();
delay();
P1=0x38;
write1();
delay();
P1=0x01;
write1();
delay();
P1=0x0f;
write1();
delay();
P1=0x06;
write1();
delay();
P1=0x0c;
write1();

}
void show_state()/*状态显示程序*/
{
unsigned char i=0;
P1=0x80;
write1();
while(xx[i]!=)
{
writ2(xx[i]);
i++;
delay();
}
i=0;
while(SD[i]!=)
{
writ2(SD[i]);
delay();
i++;

}
P1=0xc0;
write1();
i=0;
if(RUNState==Right_RUN)
while(state[0][i]!=)
{
writ2(state[0][i]);
i++;
}
else
if(RUNState==Left_RUN)
while(state[1][i]!=)
{
writ2(state[1][i]);
i++;
}
}
void write1()/*lcd写控制字*/
{
RS=0;
RW=0;
E=0;
busy();
E=1;
}
void writ2( unsigned char i)/*lcd写数据*/
{ P1=i;
RS=1;
RW=0;
E=0;
busy();
E=1;
}

void delay()/*延时程序*/
{
unsigned char i;
for(i=0;i<100;i++)
{
i=i;
}

}
void time()
{ unsigned char i,j;
for(i=0;i<100;i++)
{
for(j=0;j<30;j++)
{
j=j;
}
}
}

void busy() //判断LCD是否忙
{
do
{
P1=0xff;
RS=0;
RW=1;
E=0;
nop;
E=1;
}while(P17==1);

}
unsigned char JIAN()/*判断是否有键按下*/
{
unsigned char i,j;
i=0xff;
PORTA=i;
time();
i=0x00;
PORTB=i;
j=PORTA;
j=~j;
j=j&(0x0f);
return j;
}
unsigned char JIANZHI()/*读键值程序*/
{
unsigned i,j,d,k,m=1;
i=0xfe;
k=0x01;
do
{
PORTB=i;
j=PORTA;
m=j&(0x01);
if(m==0)
d=k/2;
else
{
m=j&(0x02);
if(m==0)
d=k/2+4;
else
{
m=j&(0x04);
if(m==0)
d=k/2+8;
else
{
m=j&(0x08);
if(m==0)
d=k/2+12;
}
}
}
k=k+2;
i=i<1;
i=i|(0x01);
}while(m!=0);
return d;
}
void OPJIAN() /* 键值处理程序*/
{
switch(INMA)
{
case 0x08: {RUNState=Right_RUN;s=0xf7;}break;
case 0x09: {RUNState=Left_RUN;s=0x01;}break;
case 0x0b: {
flag=1;
t=t-2;
RunSpeed=5000000/(65536-t*256-a);
speed();}break;
case 0x0f: {flag=1;
t=t+2;
RunSpeed=5000000/(65536-t*256-a);
speed();}
}
}
void speed()/*速度处理程序*/
{
unsigned char i=0;
SD[0]=RunSpeed/100+48;
SD[1]=RunSpeed%100/10+48;
SD[2]=RunSpeed%10+48;
}
void time1(void) interrupt 1 using 2
{

TR0=0;
PORTC=s;
if(RUNState==0)
{
s=(s<1);
if(s==0x10)
s=0x01;

}
else
{
s=s>>1;
if(s==0x0f)
s=0xf7;
}
TH0=t;
TL0=a;
TR0=1;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top