/* stepper.c Stepper motor controller with serial interface. Copyright: Jeroen Vreeken (pe1rxq@amsat.org), 2000 Published under the terms of the GNU Public License 2.0 or any later version. This works well with sdcc under linux, any other compiler might not work at all! */ #include #include "wls.h" #define LEFT 1 #define RIGHT 2 #define SWEEP 3 #define LEFT_SAFETY P1_4 #define RIGHT_SAFETY P1_5 #define OUTPUT_ENABLE P1_6 #define PULSE_MASK 0xf0 /******************************************************************************* Global vars *******************************************************************************/ /* Used to receive commands with rs232 */ unsigned char motor, command, cmddata; unsigned char bytenr=0; /* current rotating direction */ unsigned char direction=0; /* number of steps to be taken (left_n and right_n) */ unsigned char n=0; /* Are we executing the sweep command? */ unsigned char sweep=0; /* Default speed */ unsigned char speed=250; /* counter used for speed calculation */ unsigned char ticks=0; /* used for shifting bits through */ unsigned char curpulse=1; /* used when windows crashes ;-) */ unsigned char serialtimeout=0; /* counter to prevent continous current */ unsigned char last_pulse; /******************************************************************************* Interrupt routines *******************************************************************************/ void timer0_int (void) interrupt 1 using 1 { /* Ignore all received bytes after about 2 seconds*/ serialtimeout++; if (!serialtimeout) bytenr=0; /* Switch of the driver after about 1/4 of a second */ last_pulse--; if (!last_pulse) { OUTPUT_ENABLE=1; } /* invert P1.7, nice for debugging */ P1 ^= 128; ticks++; if (!ticks) { ticks=speed; if (direction) { if (direction==LEFT && LEFT_SAFETY) { curpulse=curpulse << 1; if (curpulse > 8) { curpulse=1; } } else if (RIGHT_SAFETY) { if (curpulse==1) { curpulse=8; } else { curpulse=curpulse >> 1; } } if (direction==LEFT && !LEFT_SAFETY) { if (sweep==SWEEP) { direction=RIGHT; } else { direction=0; n=0; } } if (direction==RIGHT && !RIGHT_SAFETY) { if (sweep==SWEEP) { direction=LEFT; } else { direction=0; n=0; } } if (n) { n--; if (!n) { direction=0; } } P1 &= PULSE_MASK; P1 |= curpulse; last_pulse=16; OUTPUT_ENABLE=0; } } return; } void serial_int (void) interrupt 4 { /* Did we receive data? */ if (RI) { serialtimeout=0; RI=0; switch (bytenr) { case 0: { motor=SBUF; break; } case 1: { command=SBUF; break; } case 2: { cmddata=SBUF; break; } } bytenr++; if (bytenr > 2) { bytenr=0; /* motornr should always be zero */ if (!motor) { /* command handler */ switch (command) { case COMMAND_LEFT_N: { n=cmddata; if (n) direction=LEFT; else direction=0; sweep=0; break; } case COMMAND_RIGHT_N: { n=cmddata; if (n) direction=RIGHT; else direction=0; sweep=0; break; } case COMMAND_LEFT: { direction=LEFT; n=0; sweep=LEFT; break; } case COMMAND_RIGHT: { direction=RIGHT; n=0; sweep=RIGHT; break; } case COMMAND_SWEEP: { if (!direction) direction=LEFT; sweep=SWEEP; n=0; break; } case COMMAND_STOP: { direction=0; sweep=0; n=0; break; } case COMMAND_SPEED: { speed=cmddata; if (ticks 65311= 256*255 + 31 */ TL0 = 64; TR0 = 1; /* Enable timer 0 */ ET0 = 1; /* Enable timer 0 interrupt */ EA=1; /* Enable all enabled interrupts */ /* Set some stuff right */ P1 &= PULSE_MASK; OUTPUT_ENABLE=1; /* Say hello to the world */ SBUF= direction + STATUS_SAFETYL * (!LEFT_SAFETY) + STATUS_SAFETYR * (!RIGHT_SAFETY); /* Go into an endless loop.... */ while (1) { } }