步进电机驱动电路+C语言驱动程序.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流步进电机驱动电路+C语言驱动程序.精品文档./*兰州交通大学电子电工实验中心*/*2009-8-2*/#include"reg51.h"#include"intrins.h"#include"math.h"#include"absacc.h"#define PORTA XBYTE0x8500#define PORTB XBYTE0x8502#define PORTC XBYTE0x8504#define PORTC0 XBYTE0x8506#define nop _nop_()#define Right_RUN 1#define Left_RUN 0unsigned long RunSpeed=76;unsigned char RUNState=1;sbit RS=P20;sbit RW=P21;sbit E=P22;sbit P17=P17;bit flag=0;unsigned char xx="speed(n/min):"unsigned char SD="76"char state214="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(xxi!='0') writ2(xxi); i+; delay(); i=0; while(SDi!='0') writ2(SDi); delay(); i+; P1=0xc0; write1(); i=0; if(RUNState=Right_RUN) while(state0i!='0') writ2(state0i); i+;else if(RUNState=Left_RUN) while(state1i!='0') writ2(state1i); 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; SD0=RunSpeed/100+48; SD1=RunSpeed%100/10+48; SD2=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; elses=s>>1;if(s=0x0f)s=0xf7; TH0=t; TL0=a; TR0=1;