|
#include <avr/io.h> #include <avr/delay.h>
#define uchar unsigned char #define uint unsigned int
uchar np;
//步進(jìn)電機(jī)運(yùn)行數(shù)據(jù)表
const uchar motortb[]={0x11,0x99,0x88,0xcc,0x44,0x66,0x22,0x33};
void delay_nms(uint ms)// 每步延時(shí)de子程序 { uint i; for(i=0;i<ms;i++) _delay_loop_2(8*250); }
void a_step(uchar d,uchar t) //步進(jìn)電機(jī)走一步d=0 正轉(zhuǎn)d=1 反轉(zhuǎn) t 越大走得越慢 { if (d&0x01) { if (np==0) np=7; else np--; } else { if (np==7) np=0; else np++; } PORTD=motortb[np]; delay_nms(t); }
void a_turn(uchar d,uchar t)// 步進(jìn)電機(jī)走一圈 { uchar i; for (i=0;i<96;i++) a_step(d,t); }
int main(void) { DDRD=0xff; PORTD=0x44; DDRB=0xff; PORTB=0xff; np=4; while (1) a_turn(1,200); } |