《步进电机【驱动电路+C语言驱动程序】》.doc
/**********************************************/
/*********兰州交通大学电子电工实验中心*********/
/*******************2009-8-2********************/
/*********************************************/
#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]
#define nop _nop_()
#define Right_RUN 1
#define Left_RUN 0
unsigned long RunSpeed=76;
unsigned char RUNState=1;
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit P17=P1^7;
bit flag=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]!='\0')
{
writ2(xx[i]);
i++;
delay();
}
i=0;
while(SD[i]!='\0')
{
writ2(SD[i]);
delay();
i++;
}
P1=0xc0;
write1();
i=0;
if(RUNState==Right_RUN)
while(state[0][i]!='\0')
{
writ2(state[0][i]);
i++;