步进式电机的速度恒定即可
单片机没有数据传输过来的时候步进式电机停止,一旦有高度数据传送过来,电机即工作,上升或下降相应的距离 单片机4个 I/O控制相的的顺序
本步进电机步进角为 7.5度 . 一圈 360 度 , 需要 48 个脉冲完成!!!
A组线圈对应 P1.4 B组线圈对应 P1.5 C组线圈对应 P1.6 D组线圈对应 P1.7
正转次序: AB组--BC组--CD组--DA组 (即一个脉冲,正转 7.5 度) */
#include #define uchar unsigned char #define uint unsigned int //正转脉冲表 //unsigned char Table_Run[4]={0x3, 0x60, 0xC0, 0x90}; //反转脉冲表 //unsigned char Table_Rev[4]={0x3, 0x90, 0xC0, 0x60}; ///没有ULN2003的情况 //unsigned char forword[]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}; //unsigned char back[]={0x06,0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07}; ////ULN2003 unsigned char forword[8]={0xc0,0x40,0x60,0x20,0x30,0x10,0x90,0x80}; unsigned char back[8]={0xc0,0x80,0x90,0x10,0x30,0x20,0x60,0x40}; sbit key1=P2^0; //正转 sbit key2=P2^1; //反 sbit key3=P2^2; //停止 uchar count=0; uint time=0; //函数说明 void delay(uint t); void front_move(uint step); void back_move(); //定时器0中断 void time0() interrupt 1 { TH0=(65536-1000)/256; TL0=(65536-1000)%256; //time++; } //主程序 void main() { uchar i; TMOD=0x01; TH0=(65536-1000)/256; TL0=(65536-1000)%256; ET0=1; EA=1; TR0=1; while(1) { if(key1==0) { delay(10); if(key1==0) { front_move(12); } } if(key2==0) { back_move(); } if(key3==0) { P1=0X00; } } } //延时 //电机驱动子程序 void front_move(uint step) { uchar i=0,j; for(j=0;j P1=forword[i]; delay(50); } /*if(i<8) { i++; } else { i=0; } */ } } //反转 void back_move() { uchar i; for(i=0;i<8;i++) { P1=back[i]; delay(145); } } void delay(uint t) /* 对于12M时钟,约延时1ms */ { uint i; while(t--) { for (i=0;i<80;i++) {} } } 因篇幅问题不能全部显示,请点此查看更多更全内容