void encoder_init(){ encode_setup(); printf("SETUP DONE\n"); while(1){ int end = 0; gettimeofday(&start, NULL); oldpulse = pulse; while(!end){ usleep(50000); gettimeofday(¤tTime, NULL); readRotary(); if (currentTime.tv_usec == 1000+start.tv_usec){ end = 1; RPM = ((pulse - oldpulse) * 60)/resolution; printf("RPM: %f\n", RPM); } } } }
void InterruptHandler( void) { sec1000++; dir = readRotary(); // the rotary encoder is read state = state + dir; //the state is changed depending on the returned value r1 = r1 + dir; //total number of adjustments updated if (state == 15) //state 14 rolls over to state 0 state = 0; if (state == -1) //state 0 rolls over to state 14 state = 14; if (dir != 0) //if the direction is not zero (rotart moved) { //then the code in main runs. go = 1; } if ((r1 <= -30) && (lock == 0) && (state == 11)) //the rotary needs to be moved { //30 times clockwise and be in state 11 lock++; //in order to increment lock the first time r1 = 0; //the count is reset } if ((r1 >= 15) && (lock == 1) && (state == 2) && (r1 <= 30 )) //the rotary needs to be moved { //15 times counterclockwise but less //than 30 times and be in lock++; //state 2 to increment lock a second time r1 = 0; } if ((r1 < 0) && (lock == 2) && (state == 13) && (r1 > -15)) //the rotary needs to be moved less than { //15 times clockwise to increment lock lock = 3; // to 3 } if ((lock == 3) && getRotary(3)) //once lock is incremented to 3 and { //the rotary pushbutton is pressed, unlocked = 1; //then the status becomes unlocked lock = 0; } mT2ClearIntFlag(); }