相關(guān)元件pdf下載:89c2051 uln2003 max232 程序stepper.c stepper.hex /* * stepper.c * sweeping stepper's rot cw cww 400 steps * copyright (c) 1999 by w.sirichote */ #include c:\mc51\8051io.h /* include i/o header file */ #include c:\mc51\8051reg.h register unsigned j,flag1,temp; register unsigned int cw_n,ccw_n; unsigned step[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90} #define n 400 /* flag1 mask byte 0x01 run cw() 0x02 run ccw() */ main() { flag1=0; serinit(9600); disable(); /* no need timer interrupt */ cw_n = n; /* initial step number f cw */ flag1 |=0x01; /* initial enable cw() */ while(1){ { tick_wait(); /* wait f 10ms elapsed */ energize(); /* round-robin execution the following tasks every 10ms */ cw(); ccw(); } } } cw(){ if((flag1&0x01)!=0) { cw_n--; /* decrement cw step number */ if (cw_n !=0) j++; /* if not zero increment index j */ else {flag1&=~0x01; /* disable cw() execution */ ccw_n = n; /* reload step number to ccw counter */ flag1 |=0x02; /* enable cww() execution */ } } } ccw(){ if((flag1&0x02)!=0) { ccw_n--; /* decremnent ccw step number */ if (ccw_n !=0) j--; /* if not zero decrement index j */ else {flag1&=~0x02; /* disable ccw() execution */ cw_n = n; /* reload step number to cw counter */ flag1 |=0x01; /* enable cw() execution */ } } } tick_wait(){ /* cputick was replaced by simpler asm code 10ms wait */ asm' jnb tcon.5,*'; /* wait f tf0 set */ asm' clr tcon.5'; /* clear tf0 f further set */ asm' l th0,#$dc'; /* reload th0 with $dc, tl0 = 0 */ } energize(){ p1 = step[(j&0x07)]; /* only step 0-7 needed */ }