//----------------------------------------------------------- // demo1.cpp Warren Kuo // // This program used the index reset method (C+/C-) // to get the encoder counter value for one revolution. // // compiled under large mode, Turbo C++ // // v1.0 5/6/2002 //----------------------------------------------------------- #include #include #include #include #include "enc600.h" #define CARD1 0 //----------------------------------------------------------------------- void main() { long x_value, pre_x_value; unsigned char x_index, index_count, x_axis; clrscr(); if (ENC6_REGISTRATION(CARD1, 0)!=YES) cprintf("PISO_ENC600 doesn't exist!\r\n"); // set quadrant-counting mode and index reset mode for X1-axis ENC6_INIT_CARD(CARD1, ENC_QUADRANT | ENC_INDEX_RESET , ENC_QUADRANT | ENC_INDEX_RESET , ENC_QUADRANT | ENC_INDEX_RESET , ENC_QUADRANT | ENC_INDEX_RESET , ENC_QUADRANT | ENC_INDEX_RESET , ENC_QUADRANT | ENC_INDEX_RESET); gotoxy(10,5); cprintf(" Select axis to test (1, 2, 3, 4, 5, 6):"); scanf("%d", &x_axis); if((x_axis == 0) || (x_axis > 6)) { gotoxy(10,7); cprintf(" Input error, please input again (1, 2, 3, 4, 5, 6):"); scanf("%d", &x_axis); if((x_axis == 0) || (x_axis > 6)) { gotoxy(10,8); cprintf(" Input error"); return; } } ENC6_RESET_ENCODER(CARD1,x_axis); pre_x_value = x_value = ENC6_GET_ENCODER(CARD1,x_axis); index_count = 0; gotoxy(10,17); cprintf(" Press any key to exit."); do { x_value = ENC6_GET_ENCODER(CARD1,x_axis); x_index = ENC6_GET_INDEX(CARD1,x_axis); gotoxy(10,9); cprintf(" X%d : %6ld Revolution :%x", x_axis, x_value, index_count); // if index channel is 1 and encoder counter value changes, then index_count++ if((x_index & 0x02) && (pre_x_value != x_value)) index_count ++; // do the motor rotate one revolution? if((index_count == 2) && (pre_x_value != x_value)) { gotoxy(10,13); cprintf(" The encoder counter value for one revolution is about %5ld", pre_x_value+1); break; } // if index_count > 2, set index_count=0 to retry if(index_count > 2) index_count = 0; if(pre_x_value != x_value) { pre_x_value = x_value; } } while (!kbhit()); while (!kbhit()); }