#define UBRR 0x09 // UART Baud Rate Register #define USR 0x0B // UART Status Register #define UCR 0x0A // UART Control Register /* USR Register Bits */ #define UDRE 5 // set when ok to send a byte #define RXC 7 // set when byte in receive register /* UCR Register Bits */ #define RXEN 4 // enable receive on UART #define TXEN 3 // enable transmit on UART #include /***************************************************************************** * Proto's */ void moveto( int dest, unsigned int *curLoc, int ramp, unsigned int delaycnt ); void ccw( unsigned int * curLoc ); void cw( unsigned int * curLoc ); void stepper_delay( int delay_amt ); void moveitF( unsigned int curLoc ); void moveitH( unsigned int curLoc ); int getc( void ); void putc( int ch ); void init_serial( void ); void stepper_init( void ); void release( void ); void printNum( unsigned int num ); void readBuf( char buf[] ); int buf2int( char buf[] ); void dipInit( void ); int dipRead( void ); /***************************************************************************** * It all happens here */ main() { int dest; int ramp = 20; unsigned int delaycnt = 600; unsigned int curLoc = 0; char buf[10]; int brake = 0; dipInit(); init_serial(); stepper_init(); while (1) { putc( '#' ); readBuf( buf ); switch (buf[0]) { // Zero case 'Z': curLoc = 0; break; // Ramp set case 'R': ramp = buf2int(&buf[1]); break; // Delay set case 'D': delaycnt = buf2int(&buf[1]); break; // Display settings case '?': putc( 'L' ); printNum( curLoc ); putc( ' ' ); putc( 'D' ); printNum( delaycnt ); putc( ' ' ); putc( 'R' ); printNum( ramp ); putc( ' ' ); if (brake) putc( 'B' ); else putc( ' ' ); putc( '\r' ); putc( '\n' ); break; // Relative backward case '-': moveto( curLoc-buf2int(&buf[1]), &curLoc, ramp, delaycnt ); if (!brake) release(); break; // Relative forward case '+': moveto( curLoc+buf2int(&buf[1]), &curLoc, ramp, delaycnt ); if (!brake) release(); break; // No brake case 'N': brake = 0; release(); break; // Brake case 'B': brake = 1; moveitH( curLoc ); break; // Id case 'I': printNum( dipRead() ); putc( '\r' ); putc( '\n' ); break; // Goto... case 'G': moveto( buf2int(&buf[1]), &curLoc, ramp, delaycnt ); if (!brake) release(); break; default: putc( '?' ); putc( '\r' ); putc( '\n' ); break; } } } /***************************************************************************** * Delay */ void stepper_delay( int delay_amt ) { int j; sbi(DDRD,PD5); // enable PB5 as output LED for (j=0; j 0) cw( curLoc ); else ccw( curLoc ); stepper_delay( (ramp - j) * delaycnt ); } // Mid for (j=0; j 0) cw( curLoc ); else ccw( curLoc ); stepper_delay( delaycnt ); } // RampDown for (j=rampdown-1; j>=0; j--) { if (dir > 0) cw( curLoc ); else ccw( curLoc ); stepper_delay( (ramp - j) * delaycnt ); } } /***************************************************************************** * Full step */ void moveitF( unsigned int curLoc ) { moveitH( curLoc / 2 ); } /***************************************************************************** * Half step */ void moveitH( unsigned int curLoc ) { switch (curLoc % 8) { case 0:cbi(PORTC,PC0); cbi(PORTC,PC1); cbi(PORTC,PC2); sbi(PORTC,PC3); break; case 1:cbi(PORTC,PC0); cbi(PORTC,PC1); sbi(PORTC,PC2); sbi(PORTC,PC3); break; case 2:cbi(PORTC,PC0); cbi(PORTC,PC2); sbi(PORTC,PC2); cbi(PORTC,PC3); break; case 3:cbi(PORTC,PC0); sbi(PORTC,PC2); sbi(PORTC,PC2); cbi(PORTC,PC3); break; case 4:cbi(PORTC,PC0); sbi(PORTC,PC1); cbi(PORTC,PC2); cbi(PORTC,PC3); break; case 5:sbi(PORTC,PC0); sbi(PORTC,PC1); cbi(PORTC,PC2); cbi(PORTC,PC3); break; case 6:sbi(PORTC,PC0); cbi(PORTC,PC1); cbi(PORTC,PC2); cbi(PORTC,PC3); break; case 7:sbi(PORTC,PC0); cbi(PORTC,PC1); cbi(PORTC,PC2); sbi(PORTC,PC3); break; } } /***************************************************************************** * Release the stepper */ void release( void ) { cbi(PORTC,PC0); cbi(PORTC,PC1); cbi(PORTC,PC2); cbi(PORTC,PC3); } /***************************************************************************** * Print a number to the serial port */ void printNum( unsigned int num ) { int divisor = 10000; int j; int digit; for (j=0; j<=4; j++) { digit = num / divisor; num -= digit * divisor; putc( digit += '0' ); divisor /= 10; } } /***************************************************************************** * Read a string into buf (6 char max) */ void readBuf( char buf[] ) { int n=0; char ch; do { ch = getc(); if ((ch >=' ') && (ch <='~') && (n<6)) { buf[n++] = ch; putc( ch ); } else if ((ch == 8) && (n)) { n--; putc( 8 ); putc( ' ' ); putc( 8 ); } } while ((ch != '\r') && (ch != '\n')); buf[n] = '\0'; putc( '\r' ); putc( '\n' ); } /***************************************************************************** * String to int */ int buf2int( char buf[] ) { int total = 0; int n = 0; while ((buf[n] >= '0') && (buf[n] <= '9')) { total *= 10; total += buf[n] - '0'; n++; } return( total ); } /***************************************************************************** * DIP init */ void dipInit( void ) { cbi(DDRC,PC4); // enable PC4 as input cbi(DDRC,PC5); // enable PC5 as input cbi(DDRC,PC6); // enable PC6 as input cbi(DDRC,PC7); // enable PC7 as input } /***************************************************************************** * DIP read */ int dipRead( void ) { int id = 0; if bit_is_set( PINC, PC4 ) id |= 1; if bit_is_set( PINC, PC5 ) id |= 2; if bit_is_set( PINC, PC6 ) id |= 4; if bit_is_set( PINC, PC7 ) id |= 8; return( id ); } // EOF