/********************************************************** - use 57600 bits/second - every byte received will be sent back (except spaces in the query comand) even if it does not belong to any command - commands are case sensitive - 'r' will be sent to the host if a stepping is ready (for commands sx,sy,Sx,Sy,ix,iy) Commands: sx : set position sy : set position Sx : set position (fast) Sy : set position (fast) ix : increment position iy : increment position qx : query position, won't be sent back, instead qy : query position, won't be sent back, instead w : set m*x*0.1ms wait time after a step n : set m for wait time ***********************************************************/ #include #include #include <8052.h> #define DLO P0 #define DHI P2 #define A1 P3_4 #define A2 P3_3 #define A3 P1_7 #define A4 P3_2 #define RST P3_6 #define RDWR P3_5 #define STR P3_7 #define LED P1_5 static unsigned char dly_cycles=10; unsigned current_x,current_y,desired_x,desired_y; unsigned tmr2_i,tmr2_n; void Delay(void) { { _asm push b mov b,_dly_cycles 00001$: djnz b,00001$ pop b _endasm; } } void WaitStep() { LED=0; tmr2_i=tmr2_n; while (tmr2_i); LED=1; } unsigned char SIn(void) { while (!RI); RI=0; return SBUF; } void SOut(unsigned char a) { while (!TI); TI=0; SBUF=a; } unsigned char SInOut(void) { unsigned char c; while (!RI); RI=0; c=SBUF; while (!TI); TI=0; SBUF=c; return c; } void SInit() { TH1=255; // 57600 baud PCON|=SMOD; // set double baud rate TMOD=(TMOD & T0_MASK) | 0x20; // timer 1 mode : 8-bit auto reload SCON=0x50; // Set Serial for mode 1 & enable reception TR1=1; // Start timer 1 TI=1; // ti is normally set in this program RI=0; // ri is normally cleared } unsigned char Bitreverse(unsigned char x) { unsigned char b,i; b=x & 1; // first bit of x for(i=0;i<7;i++) // seven shifts { x=x >> 1; b=(b << 1) + (x & 1); } return b; } void WriteX() { DLO=current_x; DHI=Bitreverse(current_x>>8); A1=0; A2=1; RDWR=0; STR=0; Delay(); STR=1; WaitStep(); } void WriteY() { DLO=current_y; DHI=Bitreverse(current_y>>8); A1=1; A2=0; RDWR=0; STR=0; Delay(); STR=1; WaitStep(); } void DoSteppingX() { if (desired_x!=current_x) { if (desired_x>current_x) { while (current_xdesired_x) { current_x--; WriteX(); } } } SOut('R'); } void DoSteppingY() { if (desired_y!=current_y) { if (desired_y>current_y) { while (current_ydesired_y) { current_y--; WriteY(); } } } SOut('R'); } void Reset() { RST=0; Delay(); RST=1; } void Timer2Init(unsigned tmr_n) { EA=0; ET2=0; T2CON=0x0; RCAP2H=tmr_n>>8; RCAP2L=tmr_n; TH2=tmr_n>>8; TL2=tmr_n; EA=1; ET2=1; TR2=1; } void Timer2IRQ(void) interrupt 5 { TF2=0; if (tmr2_i) tmr2_i--; } void main(void) { unsigned char c; unsigned d; unsigned char tmr2_step; LED=1; tmr2_n=100; tmr2_step=10; Timer2Init(65444); // 10kHz rate SInit(); // 57600 RST=1; // reset inactive current_x=32768; WriteX(); // default position current_y=32768; WriteY(); while (1) { c=SInOut(); if (c=='S') // set absolute position { c=SInOut(); if (c=='x') { current_x=SInOut(); current_x=(current_x << 8) + SInOut(); WriteX(); SOut('R'); } else if (c=='y') { current_y=SInOut(); current_y=(current_y << 8) + SInOut(); WriteY(); SOut('R'); } } else if (c=='s') { c=SInOut(); if (c=='x') { desired_x=SInOut(); desired_x=(desired_x << 8) + SInOut(); DoSteppingX(); } else if (c=='y') { desired_y=SInOut(); desired_y=(desired_y << 8) + SInOut(); DoSteppingY(); } } else if (c=='i') // increment position { c=SInOut(); if (c=='x') { d=SInOut(); d=(d << 8) + SInOut(); if (d<32768) // positive step { if (65535-current_x>=d) desired_x=current_x+d; else desired_x=65535; } else // negative step { d=-d; // absolute value if (current_x>=d) desired_x=current_x-d; else desired_x=0; } DoSteppingX(); } else if (c=='y') { d=SInOut(); d=(d << 8) + SInOut(); if (d<32768) // positive step { if (65535-current_y>=d) desired_y=current_y+d; else desired_y=65535; } else // negative step { d=-d; // absolute value if (current_y>=d) desired_y=current_y-d; else desired_y=0; } DoSteppingY(); } } else if (c=='q') { c=SInOut(); if (c=='x') { SIn(); SOut(current_x>>8); SIn(); SOut(current_x); } else if (c=='y') { SIn(); SOut(current_y>>8); SIn(); SOut(current_y); } } else if (c=='w') { tmr2_n=SInOut(); if (tmr2_n==0) tmr2_n=1; tmr2_n*=tmr2_step; // resolution is tmr2_step*0.1millisec } else if (c=='n') { tmr2_step=SInOut(); if (tmr2_step==0) tmr2_step=1; } else if (c=='r') { if (SInOut()=='s') { if (SInOut()=='t') { RST=0; Delay(); RST=1; tmr2_step=10; tmr2_n=100; // resolution is 0.1millisec } } } } }