/* FILE: 7266_03.C DATE: 07/26/99 - 1.0 05/16/00 - 2.0 started - WORKS! 05/17/00 - 13:10 3.0 started - WORKS! AUTH: P.OH DESC: Testing prototyped 7266 card. Code based on US Digital's sample.c file and Tom Kulaga's txt_dro.cpp Added some things like, DEGREEs printout 3.0: rewrite things as I understand it */ #include #include #include #include /* LS7266 commands */ #define CLOCK_DATA 14 /* FCK frequency divider */ #define CLOCK_SETUP 0X98 /* transfer PR0 to PSC (x and y) */ #define INPUT_SETUP 0XC1 /* enable inputs A and B (x and y) */ #define QUAD_X1 0XA8 /* quadrature multiplier to 1 (x and y) */ #define QUAD_X2 0XB0 /* quadrature multiplier to 2 (x and y) */ #define QUAD_X4 0XB8 /* quadrature multiplier to 4 (x and y) */ #define BP_RESET 0X01 /* reset byte pointer */ #define BP_RESETB 0X81 /* reset byte pointer (x and y) */ #define CNTR_RESET 0X02 /* reset counter */ #define CNTR_RESETB 0X82 /* reset counter (x and y) */ #define TRSFRPR_CNTR 0X08 /* transfer preset register to counter */ #define TRSFRCNTR_OL 0X90 /* transfer CNTR to OL (x and y) */ #define EFLAG_RESET 0X86 /* reset E bit of flag register */ #define SLOTS_ON_DISK 500 /* US Digital's encoder S1-500-INT encoder */ /* prototypes */ void initialize7266(int, int, int); long xReadEncoder(int, int); void xPrintValue(long, int); void main(void) { long xEncoderValue; int quadrature, desiredQuadrature; int BASEADDR; int xControl, xData; clrscr(); window(5,5,75,30); gotoxy(1,1); cprintf("Enter base address (decimal) e.g. 608\n"); gotoxy(1,2); scanf("%d", &BASEADDR); xData = BASEADDR; xControl = BASEADDR + 1; gotoxy(1,3); cprintf("Desired quadrature: 1 (single), 2 (double), 4 (quad) e.g. 1\n"); gotoxy(1,4); scanf("%d", &desiredQuadrature); switch (desiredQuadrature) { case 1 : quadrature = QUAD_X1; break; case 2 : quadrature = QUAD_X2; break; case 4 : quadrature = QUAD_X4; break; } gotoxy(1,5); cprintf("Initializing..."); initialize7266(quadrature, xControl, xData); cprintf("...done\n"); gotoxy(1,7); cprintf("Spin encoder or hit a key to quit\n"); while (!kbhit()) { outportb(xControl, TRSFRCNTR_OL); /* transfer CNTR to OL */ xEncoderValue = xReadEncoder(xControl, xData); xPrintValue(xEncoderValue, desiredQuadrature); } } /* end of main */ void initialize7266(int quadrature, int xControl, int xData) { /* initialize the 7266 */ int fail = 0; outportb(xControl, EFLAG_RESET); /* reset E bit of flag register */ outportb(xControl, BP_RESETB); /* reset byte pointer */ outportb(xData, CLOCK_DATA); /* FCK freq divider */ outportb(xControl, CLOCK_SETUP); /* transfer PR0 to PSC */ outportb(xControl, INPUT_SETUP); /* enable inputs A and B */ outportb(xControl, quadrature); /* desired quadrature e.g. 1, 2 or 4 */ outportb(xControl, CNTR_RESETB); /* reset counter */ /* preset values to the X-axis preset register */ outportb(xControl, BP_RESET); /* reset byte pointer */ outportb(xData, 0x12); /* output preset value */ outportb(xData, 0x34); outportb(xData, 0x56); /* read counter back to test it */ outportb(xControl, TRSFRPR_CNTR); /* transfer preset to counter */ outportb(xControl, TRSFRCNTR_OL); /* transfer CNTR to OL */ outportb(xControl, CNTR_RESET); /* reset counter */ outportb(xControl, BP_RESET); /* reset byte pointer */ /* if all goes well, then the 3 output preset values should read back the same 3 values. If not then fail variable is incremented and initialize7266 failure message is printed. */ if (inportb(xData) != 0x12) fail++; if (inportb(xData) != 0x34) fail++; if (inportb(xData) != 0x56) fail++; if (fail) { printf("initialize7266 failed\n"); exit(0); }; return; } /* end of init */ long xReadEncoder(int xControl, int xData) { /* read position of X encoder */ long position; outportb(xControl, BP_RESETB); /* reset byte pointer */ /* read 3 consecutive bytes and add them */ position = (long)inportb(xData); /* least significant byte */ position += (long)inportb(xData) << 8; position += (long)inportb(xData) <<16; /* most significant byte */ return position; } /* end of xReadEncoder */ void xPrintValue(long xEncoderValue, int desiredQuadrature) { /* print X encoder value */ /* NB: 2^24 = 16777216 */ float degreesPerPulse = 360.0/SLOTS_ON_DISK/desiredQuadrature; gotoxy(5,15); cprintf("Value = %7lX (HEX) = %10ld (DECIMAL)", xEncoderValue, xEncoderValue); gotoxy(5,17); cprintf("Pulses = %10ld", (16777216-xEncoderValue) ); gotoxy(5,18); cprintf("Degrees = %10.3f", (16777216-xEncoderValue)*degreesPerPulse); } /* end of xPrintValue */