/* Stepping motor tachometer 1996.6.26 - 6.27 - 7.2 */ /* 1996.9.8 3mode->2mode /* LSIC-80 */ /* CPU:AKI-80(12.25MHz) ROM:HN27C256HG-70 */ #include #define APD 0x1c #define APC 0x1d #define BPD 0x1e #define BPC 0x1f #define abs(a) ((a)>=0?(a):(-(a))) unsigned char m[4]={0x10,0x20,0x40,0x80}; /* n=0~5041 (msec) for 12.25MHz */ void delay(unsigned int n) { char a=25; unsigned int c; if(n==0)return; c=n*13; while(a--){ n=c; while(n--); } } void PortInit(void) { outp(APC,0xcf); /* mode=3 */ outp(APC,0xff); /* (0:OUT 1:IN) all bit in cnt7-0 */ outp(BPC,0xcf); /* mode=3 */ outp(BPC,0x0f); /* bit7-4:OUT to step.m. bit3-0:IN sw,cnt10,9,8 */ } /* delay=5 ... 1rpm */ void main(void) { int mode; unsigned int tacho; unsigned int cur_pos; unsigned int tmp_pos; unsigned int des_pos; PortInit(); mode=0; cur_pos=30; for(;;){ /* UCW */ outp(BPD,m[(--cur_pos)&3]); delay(10); if(cur_pos==0)break; } des_pos=cur_pos; delay(250); tacho=inp(APD)+(inp(BPD)&7)*0x100; for(;;){ if(tacho!=(inp(APD)+(inp(BPD)&7)*0x100))break; } cont: /* mode 0:normal 1:zero (2:maximum) */ if((inp(BPD)&8)==0){; mode=(mode+1)%2; delay(250); } tacho=inp(APD)+(inp(BPD)&7)*0x100; if(tacho==0){ ; }else{ tmp_pos=3000U/tacho; if(tmp_pos<=100)des_pos=tmp_pos; } if(mode==1)des_pos=0; if(abs(cur_pos-des_pos)>1){ if(cur_posdes_pos){ outp(BPD,m[(--cur_pos)&3]); delay(5); } }else{ delay(20); } goto cont; } /* End of File */