Level1_SensorNode/control_all/rotary_encoders.cpp
//================================================================================================//
// Pin Change Interrupt Function                                                                  //
// MEMO : This function counts the encoder pulse                                                  //
//================================================================================================//
Pin Change Interrupt Function
MEMO : This function counts the encoder pulse
Pin Change Interrupt Function MEMO : This function counts the encoder pulse
static inline void interrupt_handler() {
  uint8_t i;
  noInterrupts();
  for (i = 0; i < 4; i++) {
    if (enc_isActive[i] == true) {
      enc_a_now[i] = digitalRead(enc_a_pin[i]);
      enc_b_now[i] = digitalRead(enc_b_pin[i]);
    }
  }
  interrupts();
  for (i = 0; i < 4; i++) {
    if (enc_a_now[i] != enc_a_pre[i]) {
      if (enc_a_now[i] == 0) {
        if (enc_b_now[i] == 0) enc_cnt[i]--;
        else                   enc_cnt[i]++;
      } else {
        if (enc_b_now[i] == 0) enc_cnt[i]++;
        else                   enc_cnt[i]--;
      }
    }
    else if (enc_b_now[i] != enc_b_pre[i]) {
      if (enc_b_now[i] == 0) {
        if (enc_a_now[i] == 0) enc_cnt[i]++;
        else                   enc_cnt[i]--;
      } else {
        if (enc_a_now[i] == 0) enc_cnt[i]--;
        else                   enc_cnt[i]++;
      }
    }
    enc_a_pre[i] = enc_a_now[i];
    enc_b_pre[i] = enc_b_now[i];
  }
  test_count++;
}
static inline void interrupt_handler ( ) { uint8_t i ; noInterrupts ( ) ; for ( i = 0 ; i < 4 ; i ++ ) { if ( enc_isActive [ i ] == true ) { enc_a_now [ i ] = digitalRead ( enc_a_pin [ i ] ) ; enc_b_now [ i ] = digitalRead ( enc_b_pin [ i ] ) ; } } interrupts ( ) ; for ( i = 0 ; i < 4 ; i ++ ) { if ( enc_a_now [ i ] != enc_a_pre [ i ] ) { if ( enc_a_now [ i ] == 0 ) { if ( enc_b_now [ i ] == 0 ) enc_cnt [ i ] -- ; else enc_cnt [ i ] ++ ; } else { if ( enc_b_now [ i ] == 0 ) enc_cnt [ i ] ++ ; else enc_cnt [ i ] -- ; } } else if ( enc_b_now [ i ] != enc_b_pre [ i ] ) { if ( enc_b_now [ i ] == 0 ) { if ( enc_a_now [ i ] == 0 ) enc_cnt [ i ] ++ ; else enc_cnt [ i ] -- ; } else { if ( enc_a_now [ i ] == 0 ) enc_cnt [ i ] -- ; else enc_cnt [ i ] ++ ; } } enc_a_pre [ i ] = enc_a_now [ i ] ; enc_b_pre [ i ] = enc_b_now [ i ] ; } test_count ++ ; }
Pin Change Interrupt Function
MEMO : This function counts the encoder pulse
Pin Change Interrupt Function MEMO : This function counts the encoder pulse
// Get current input value of the each encoder output // If the previous and the current state of the phase A are different, that means a Pulse has occured // If the phase B state is different to the phase A state, that means the encoder is rotating clockwise