《步进电机驱动电路+C语言驱动程序.doc》由会员分享,可在线阅读,更多相关《步进电机驱动电路+C语言驱动程序.doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流步进电机驱动电路+C语言驱动程序.精品文档./*兰州交通大学电子电工实验中心*/*2009-8-2*/#includereg51.h#includeintrins.h#includemath.h#includeabsacc.h#define PORTA XBYTE0x8500#define PORTB XBYTE0x8502#define PORTC XBYTE0x8504#define PORTC0 XBYTE0x8506#define nop _nop_()#define Right_RUN 1#define Left_RUN 0unsi
2、gned 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 JIA
3、N(); 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;
4、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()
5、; 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(state
6、0i); 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;i100;i+) i=i;void time() unsigned char i,j;for(i=0;i100;i+)f
7、or(j=0;j30;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 POR
8、TB=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=i1;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=
9、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=(s1;if(s=0x0f)s=0xf7; TH0=t; TL0=a; TR0=1;