Level1_SensorNode/control_all/rotary_encoders.cpp
//================================================================================================//
// Attach the given pins to the rotary encoder input pins                                         //
//================================================================================================//
Attach the given pins to the rotary encoder input pins
Attach the given pins to the rotary encoder input pins
uint8_t RotaryEncoders::attach(int n, uint8_t pin1, uint8_t pin2, uint8_t mode) {
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#ifdef _usePCI0
#endif
#ifdef _usePCI1
#endif
#ifdef _usePCI2
  if (0 <= n && n <= 3) {
    if (62 <= pin1 && pin1 <= 69 && 62 <= pin2 && pin2 <= 69) {
      pinMode(pin1, INPUT);
      pinMode(pin2, INPUT);
      enc_a_pin[n] = pin1;
      enc_b_pin[n] = pin2;
      enc_isActive[n] = true;
      this->isActive = true;
      noInterrupts();
      PCICR = _BV(PCIE2);                               
      PCMSK2 |= (1 << (pin1 - 62)) | (1 << (pin2 - 62));  
      if (mode == QUAD_MULTI) {
      }
      interrupts();
      return (0);
    } else {
      return (1);
    }
  } else {
    return (1);
  }
#endif
#endif
#if defined(__AVR_ATmega328__) || (__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__)
  uint8_t pin1_pcint = 0, pin2_pcint = 0;
  if (0 <= n && n <= 3) {
    if (pin1 <= 23 && pin2 <= 23) {
      pinMode(pin1, INPUT);
      pinMode(pin2, INPUT);
      enc_a_pin[n] = pin1;
      enc_b_pin[n] = pin2;
      noInterrupts();
      PCICR |= _BV(PCIE2) | _BV(PCIE1) | _BV(PCIE0);       
      pin1_pcint = d2pcint[pin1];
      if (pin1_pcint <= 7) {
        PCMSK0 |= (1 << pin1_pcint);
      }
      else if (8 <= pin1_pcint && pin1_pcint <= 15) {
        PCMSK1 |= (1 << (pin1_pcint - 8));
      }
      else if (16 <= pin1_pcint && pin1_pcint <= 23) {
        PCMSK2 |= (1 << (pin1_pcint - 16));
      }
      if (mode == QUAD_MULTI) {
        pin2_pcint = d2pcint[pin2];
        if (pin2_pcint <= 7) {
          PCMSK0 |= (1 << pin2_pcint);
        }
        else if (8 <= pin2_pcint && pin2_pcint <= 15) {
          PCMSK1 |= (1 << (pin2_pcint - 8));
        }
        else if (16 <= pin2_pcint && pin2_pcint <= 23) {
          PCMSK2 |= (1 << (pin2_pcint - 16));
        }
      }
      interrupts();
#ifdef DEBUG
      Serial.print(PCICR, HEX);
      Serial.print(",");
      Serial.print(PCMSK0, HEX);
      Serial.print(",");
      Serial.print(PCMSK1, HEX);
      Serial.print(",");
      Serial.print(PCMSK2, HEX);
      Serial.print("\n");
#endif
      return (0);
    } else {
      return (1);
    }
  } else {
    return (1);
  }
#endif
}
uint8_t RotaryEncoders :: attach ( int n , uint8_t pin1 , uint8_t pin2 , uint8_t mode ) { #if defined ( __AVR_ATmega1280__ ) || defined ( __AVR_ATmega2560__ ) #ifdef _usePCI0 #endif #ifdef _usePCI1 #endif #ifdef _usePCI2 if ( 0 <= n && n <= 3 ) { if ( 62 <= pin1 && pin1 <= 69 && 62 <= pin2 && pin2 <= 69 ) { pinMode ( pin1 , INPUT ) ; pinMode ( pin2 , INPUT ) ; enc_a_pin [ n ] = pin1 ; enc_b_pin [ n ] = pin2 ; enc_isActive [ n ] = true ; this -> isActive = true ; noInterrupts ( ) ; PCICR = _BV ( PCIE2 ) ; PCMSK2 |= ( 1 << ( pin1 - 62 ) ) | ( 1 << ( pin2 - 62 ) ) ; if ( mode == QUAD_MULTI ) { } interrupts ( ) ; return ( 0 ) ; } else { return ( 1 ) ; } } else { return ( 1 ) ; } #endif #endif #if defined ( __AVR_ATmega328__ ) || ( __AVR_ATmega328P__ ) || defined ( __AVR_ATmega168__ ) || defined ( __AVR_ATmega168P__ ) uint8_t pin1_pcint = 0 , pin2_pcint = 0 ; if ( 0 <= n && n <= 3 ) { if ( pin1 <= 23 && pin2 <= 23 ) { pinMode ( pin1 , INPUT ) ; pinMode ( pin2 , INPUT ) ; enc_a_pin [ n ] = pin1 ; enc_b_pin [ n ] = pin2 ; noInterrupts ( ) ; PCICR |= _BV ( PCIE2 ) | _BV ( PCIE1 ) | _BV ( PCIE0 ) ; pin1_pcint = d2pcint [ pin1 ] ; if ( pin1_pcint <= 7 ) { PCMSK0 |= ( 1 << pin1_pcint ) ; } else if ( 8 <= pin1_pcint && pin1_pcint <= 15 ) { PCMSK1 |= ( 1 << ( pin1_pcint - 8 ) ) ; } else if ( 16 <= pin1_pcint && pin1_pcint <= 23 ) { PCMSK2 |= ( 1 << ( pin1_pcint - 16 ) ) ; } if ( mode == QUAD_MULTI ) { pin2_pcint = d2pcint [ pin2 ] ; if ( pin2_pcint <= 7 ) { PCMSK0 |= ( 1 << pin2_pcint ) ; } else if ( 8 <= pin2_pcint && pin2_pcint <= 15 ) { PCMSK1 |= ( 1 << ( pin2_pcint - 8 ) ) ; } else if ( 16 <= pin2_pcint && pin2_pcint <= 23 ) { PCMSK2 |= ( 1 << ( pin2_pcint - 16 ) ) ; } } interrupts ( ) ; #ifdef DEBUG Serial . print ( PCICR , HEX ) ; Serial . print ( " " ) ; Serial . print ( PCMSK0 , HEX ) ; Serial . print ( " " ) ; Serial . print ( PCMSK1 , HEX ) ; Serial . print ( " " ) ; Serial . print ( PCMSK2 , HEX ) ; Serial . print ( " \n " ) ; #endif return ( 0 ) ; } else { return ( 1 ) ; } } else { return ( 1 ) ; } #endif }
Attach the given pins to the rotary encoder input pins
Attach the given pins to the rotary encoder input pins
// pin direction settings // Initialize Pin change interrupt // Enables PORT K Pin Change Interrupt // Enables all PORT K Pin Change Interrupt // pin direction settings // enc_isActive[n] = true; // Initialize Pin change interrupt // Enables PORT B, C, D Pin Change Interrupt