/* Operation TB6600 Stepper Controller with PC Parallel Port https://www.bristolwatch.com/pport2/tb6600.htm by Lewis Loflin lewis@bvu.net */ #include #include #include #include // #include // #include // #include "myfile.h" #define DATA 0x378 /* parallel port base address */ // D0-D7 pins 2-9 #define STATUS DATA + 1 // inputs only // base address 0x379 - five bit 3-7 // bit 3 pin 15 // bit 4 pin 13 // bit 5 pin 12 // bit 6 pin 10 // bit 7 pin 11 inverted - HIGH reads LOW #define CONTROL DATA + 2 // outputs // base address 0x37A - four bits 0-3 // bit 0 pin 1 inverted // bit 1 pin 14 inverted // bit 2 pin 16 not inverted // bit 3 pin 17 inverted #define D0 1 #define D1 2 #define D2 4 #define D3 8 #define D4 16 #define D5 32 #define D6 64 #define D7 128 #define HIGH 1 #define LOW 0 #define CW 0 #define CCW 1 #define PUL 1 // 0x378 D0 #define DIR 2 // 0x378 D1 #define ENA 4 // 0x378 D2 #define SW1 8 #define SW2 16 #define SW3 32 #define SW4 64 #define SW5 128 // declare subroutines void digitalWrite(int, int); // pin, state void stepperOn(int, int); // steps, step_delay int readSwitch(int bit); // uses bits 3-7 with 7 inverted // all have internal PU to HIGH // Bitwise AND port data with bit value int readSwitch(int SW_Number) { int x; x = inb(Status) & SW_Number; if (x) return 1; // non-zero result else return 0; } // set pin state on DB25 pins 2-9 (D0-D7) at 0x378 void digitalWrite(int pinNumber, int state) { unsigned char x, y, z; x = inb(DATA); // read data latches y = x & pinNumber; // determine pin state HIGH or LOW // pinNumber is a numeric value based on powers of 2 if (state == 0 && y != 0) { z = x & (0xFF - pinNumber); // bitwise AND clear bit outb(z, DATA); } if (state == 1) { z = x | pinNumber; // bitwise OR set bit outb(z, DATA); } } // D0 is clk, D1 is direction, D2 is enable // enable LOW to turn on // 1 is CW, 0 is CCW void stepperOn(int count, int delayMs) { int x; digitalWrite(ENA, LOW); // motor enable for (x = 0; x <= count; x++) { printf("Count = %d\n", x); // if (readSwitch(D3) == 0) break; digitalWrite(PUL, HIGH); usleep(10); digitalWrite(PUL, LOW); usleep(delayMs * 1000); } digitalWrite(ENA, HIGH); // motor off } int main(void) { // must include to access port if (ioperm(DATA, 3, 1)) fprintf(stderr, "Access denied to %x\n", DATA), exit(1); printf("Hello world."); digitalWrite(PUL, LOW); digitalWrite(ENA, HIGH); // off while (1) { // HIGH is CW, LOW is CCW digitalWrite(DIR, CW); stepperOn(100, 10); sleep(1); // wait 2 sec. digitalWrite(DIR, CCW); stepperOn(200, 10); } return 0; }