用mege8L的PB0-PB3連接步進電機的四個接線端!該步進電機的轉矩很小,強烈建議不要將其從底座上拆下。!否則沒有軸承支撐,摩擦力太大,轉不起來!
#include <iom8v.h> #define uchar unsigned char #define uint unsigned int
uchar np; const uchar motortb[]={0x11,0x99,0x88,0xcc,0x44,0x66,0x22,0x33};//步進電機運行數據表
void delay(uchar t)// 每步延時的子程序 { uchar i; uint j; for (i=0;i<t;i++) for (j=0;j<900;j++); }
void a_step(uchar d,uchar t) //步進電機走一步d=0 正轉d=1 反轉, t 越大走得越慢 慢 { if (d&0x01) { if (np==0) np=7; else np--; } else { if (np==7) np=0; else np++; } PORTB=motortb[np]; delay(t); }
void a_turn(uchar d,uchar t)// 步進電機走一圈 { uchar i; for (i=0;i<96;i++) a_step(d,t); }
void main(void) { DDRB=0xff; PORTB=0x44; np=4; while (1) a_turn(1,5);//轉速可以在5-25左右的范圍內調節(jié),t太小則嗡嗡作響,轉不起來 } |
|