Пример #1
0
LOCAL void RF24_irqHandler(void)
{
	if (RF24_receiveCallback) {
		// Will stay for a while (several 100us) in this interrupt handler. Any interrupts from serial
		// rx coming in during our stay will not be handled and will cause characters to be lost.
		// As a workaround we re-enable interrupts to allow nested processing of other interrupts.
		// Our own handler is disconnected to prevent recursive calling of this handler.
#if defined(MY_GATEWAY_SERIAL) && !defined(__linux__)
		detachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN));
		interrupts();
#endif
		// Read FIFO until empty.
		// Procedure acc. to datasheet (pg. 63):
		// 1.Read payload, 2.Clear RX_DR IRQ, 3.Read FIFO_status, 4.Repeat when more data available.
		// Datasheet (ch. 8.5) states, that the nRF de-asserts IRQ after reading STATUS.

		// Start checking if RX-FIFO is not empty, as we might end up here from an interrupt
		// for a message we've already read.
		while (RF24_isDataAvailable()) {
			RF24_receiveCallback();		// Must call RF24_readMessage(), which will clear RX_DR IRQ !
		}
		// Restore our interrupt handler.
#if defined(MY_GATEWAY_SERIAL) && !defined(__linux__)
		noInterrupts();
		attachInterrupt(digitalPinToInterrupt(MY_RF24_IRQ_PIN), RF24_irqHandler, FALLING);
#endif
	} else {
		// clear RX interrupt
		RF24_setStatus(_BV(RF24_RX_DR));
	}
}
Пример #2
0
void setup() 
{
  // Setup button
  pinMode(BUTTON_FORWARD, INPUT);     // Set pin 2 as button input
  digitalWrite(BUTTON_FORWARD, HIGH); // Enable resistor to Vcc (active Low)
  attachInterrupt(digitalPinToInterrupt(BUTTON_FORWARD), button_forward, FALLING);
  
  // Setup button
  pinMode(BUTTON_REVERSE, INPUT);     // Set pin 2 as button input
  digitalWrite(BUTTON_REVERSE, HIGH); // Enable resistor to Vcc (active Low)
  attachInterrupt(digitalPinToInterrupt(BUTTON_REVERSE), button_reverse, FALLING);

  
  pinMode(LED, OUTPUT);
    
  pinMode(L_Motor_A, OUTPUT);
  pinMode(L_Motor_B, OUTPUT);

  pinMode(R_Motor_A, OUTPUT);
  pinMode(R_Motor_B, OUTPUT);

  pinMode(L_Motor_Ena, OUTPUT);
  pinMode(R_Motor_Ena, OUTPUT);

  digitalWrite(L_Motor_Ena, HIGH);
  digitalWrite(R_Motor_Ena, HIGH);

  digitalWrite(L_Motor_A, LOW);
  digitalWrite(L_Motor_B, HIGH);

  digitalWrite(R_Motor_A, LOW);
  digitalWrite(R_Motor_B, HIGH);
}
Пример #3
0
void Encoder::Setup()
{
    pinMode(ENCODER_LEFT, INPUT);
    pinMode(ENCODER_RIGHT, INPUT);
    m_pinInterruptL = digitalPinToInterrupt(ENCODER_LEFT);
    m_pinInterruptR = digitalPinToInterrupt(ENCODER_RIGHT);
    m_pulsesDetectL = 0;
    m_pulsesDetectR = 0;
}
Пример #4
0
void Location::Init(void) {
  pinMode(ENCODERPINX, INPUT_PULLUP);
  pinMode(ENCODERPINY, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ENCODERPINX), encoderISRx, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENCODERPINY), encoderISRy, CHANGE);
  // IR Sensors
  for (int i = 0; i < IRCOUNT; i++)
    pinMode(irRead[i], INPUT);
}
Пример #5
0
// Interrupt service routines
void on_ping_reply_change() {
  if(digitalRead(PIN_PING_ECHO) == HIGH) {
      attachInterrupt(digitalPinToInterrupt(PIN_PING_ECHO), on_ping_reply_change, FALLING);
     g_ping_reply_start_us = micros();
  } else {
    g_ping_reply_end_us = micros();
    g_ping_reply_ready = true;
    detachInterrupt(digitalPinToInterrupt(PIN_PING_ECHO));
  }
}
Пример #6
0
void WiegandReader::Initialize()
{
	// Arduino Uno only has 2 pins for interrupts
	Reset();
	pinMode(Data0Pin, INPUT);
	pinMode(Data1Pin, INPUT);
	attachInterrupt(digitalPinToInterrupt(Data0Pin), Data0IntHandler, FALLING);	// high to low
	attachInterrupt(digitalPinToInterrupt(Data1Pin), Data1IntHandler, FALLING);	// high to low
	IsInitialized = true;
}
//The setup function is called once at startup of the sketch
void setup() {
	Serial.begin(9600);
	lastMotionDetected = millis();
	lastTrigger = millis();
	pinMode(SDA_PIN, INPUT_PULLUP);
	pinMode(SCL_PIN, INPUT_PULLUP);
	lcd.init();
	attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), sensorChange, FALLING);
	attachInterrupt(digitalPinToInterrupt(MOTION_DETECTOR_PIN), motionDetected,
			CHANGE);
	dht.begin();
	delay(3000);
}
void ABPhaseEncoders::initEncoder()
{
  // Set mode of pins to be INPUT
  pinMode(leftApin, INPUT);
  pinMode(leftBpin, INPUT);
  pinMode(rightApin, INPUT);
  pinMode(rightBpin, INPUT);
  leftDirect = true;  rightDirect = true;
  leftReversed = false;  rightReversed = false;
  leftDur = 0L;  rightDur = 0L;
  // Set which pins to use as interrupt port
  attachInterrupt(digitalPinToInterrupt(leftApin), callLeftISR, CHANGE);
  attachInterrupt(digitalPinToInterrupt(rightApin), callRightISR, CHANGE);
}
void WIEGAND::begin(int pinD0, int pinD1)
{
	_lastWiegand = 0;
	_cardTempHigh = 0;
	_cardTemp = 0;
	_code = 0;
	_wiegandType = 0;
	_bitCount = 0;  
	pinMode(pinD0, INPUT);					// Set D0 pin as input
	pinMode(pinD1, INPUT);					// Set D1 pin as input
	
	attachInterrupt(digitalPinToInterrupt(pinD0), ReadD0, FALLING);  // Hardware interrupt - high to low pulse
	attachInterrupt(digitalPinToInterrupt(pinD1), ReadD1, FALLING);  // Hardware interrupt - high to low pulse
}
Пример #10
0
void Ping::execute(){
    _new_data_ready = false;
    unsigned long ms = millis();
    unsigned long  us = micros();
    if(_waiting_for_reply) {

       // transfer volatile variables from interrupts
       if(g_ping_reply_ready) {
         cli();
         unsigned long reply_start_us = g_ping_reply_start_us;
         unsigned long reply_end_us = g_ping_reply_end_us;
         sei();

         if(0) {
           Serial.print("reply_start_us");
           Serial.println(reply_start_us);
           Serial.print("reply_end_us");
           Serial.println(reply_end_us);
          }
        _waiting_for_reply = false;
        _new_data_ready = true;
        detachInterrupt(digitalPinToInterrupt(echo_pin));
        unsigned long duration = reply_end_us - reply_start_us;
        set_distance_from_us(duration);
       } else if(us - ping_start_us > ping_timeout_us) {
        detachInterrupt(digitalPinToInterrupt(echo_pin));
        _waiting_for_reply = false;
       }

    } else {
        if( ms - ping_start_ms >= ping_rate_ms) {
          ping_start_ms = ms;
          ping_start_us = us;
          digitalWrite(echo_pin, LOW);
          pinMode(echo_pin, INPUT);

          // prepare interrupt ahead of time since we don't know when it will start
          _waiting_for_reply = true;
          g_ping_reply_ready = false;
          g_ping_reply_start_us = g_ping_reply_end_us = 0;
          attachInterrupt(digitalPinToInterrupt(echo_pin), on_ping_reply_change, RISING);

          // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
          digitalWrite(ping_pin, HIGH);
          delayMicroseconds(10);  // todo: can we get rid of this delay or maybe it's ok?
          digitalWrite(ping_pin, LOW);
        }
    }
}
// 設定を行う
void Sleep::doSetup() {
  // 各デバイスをオフにする
  devices_->reader.turnOff();
  devices_->xbee.sleep();
  devices_->led_success.turnOff();
  devices_->led_error.turnOff();

  devices_->lcd.setCursor(0, 0);
  // "スリープモード"
  devices_->lcd.print(F("\xBD\xD8\xB0\xCC\xDF\xD3\xB0\xC4\xDE       "));

  devices_->lcd.setCursor(0, 1);
  // "「ツギヘ」デカイジョシマス"
  devices_->lcd.print(F("\xA2\xC2\xB7\xDE\xCD\xA3\xC3\xDE\xB6\xB2\xBC\xDE\xAE\xBC\xCF\xBD"));

  // スリープモードと割り込みを設定する
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  // 割り込み設定の際は割り込みを無効にしておく
  noInterrupts();
  attachInterrupt(digitalPinToInterrupt(DeviceSet::PIN_INT), sleepInt, LOW);
  sleep_enable();

  // 割り込みを有効にしてからスリープモードに入る
  interrupts();
}
void audiohandler_begin(uint8_t pinAudioInt) {
	// ah_pin = pinAudioInt;

	// Prepare pin for ISR
	pinMode(pinAudioInt, INPUT);
	attachInterrupt(digitalPinToInterrupt(pinAudioInt), audiohandler_ISR, RISING);
}
bool RealTimeClock::begin()
{
  m_SPI.Initialize();

  // The seconds register contains a flag which indicates whether the 
  // clock is valid. If it is not valid, reset the device & restore time to zero. 
  uint8_t uSeconds;

  uSeconds = m_SPI.Read(RTC_02_Sec);
  if (uSeconds & 0x80)
  {
    // Clock integrity is not guaranteed. Oscillator has stopped or has been 
    // interrupted. Start with a software reset to restore to a known state. 
    m_SPI.Write(RTC_00_Control1, 0x58); // 0x58=>software reset.
    delay(40); // Give time for reset. 
  }

  // Hook up interrupts. 
  if (c_uInterruptPin != NO_INTERRUPT)
  {
    pinMode(c_uInterruptPin, INPUT_PULLUP); // RTC interrupt is open drain, active low. 
    uint8_t CurrentInterruptState = SREG; // Save interrupt state. 
    cli();  // disable interrupts. 
    s_pInterruptHandler = this;
#ifdef digitalPinToInterrupt
    attachInterrupt(digitalPinToInterrupt(m_SPI.m_uChipSelectPin), InterruptHandler, LOW);
#else
    attachInterrupt(2, InterruptHandler, LOW);
#endif
    m_SPI.Write(RTC_01_Control2, 0x01); // Interrupt from countdown timer. 
    SREG = CurrentInterruptState;
  }

  return true;
}
Пример #14
0
// Init method for setting up the pinmodes and attaching interrupt.
// Using pin 2 and 3 for Uno
// Using pin 19 and 18 for Mega 2560
// This is for simplicity when using PORTD bit 2 and 3.
void JAF_UltrasonicLib::init()
{
	pinMode(TRIGGPIN, OUTPUT);
	pinMode(ECHOPIN, INPUT);
	digitalWrite(TRIGGPIN, LOW);
	attachInterrupt(digitalPinToInterrupt(ECHOPIN), JAF_UltrasonicLib::_signal, CHANGE);
}
Пример #15
0
void setup_rc(role_e rcRole)
{
  // global variable initialize
  //memset(rcData, 0, sizeof(rcData));
  memset(&rcPacket, 0, sizeof(rcPacket));
  rcPacket.header.guidePattern = RC_PKT_GUIDE_PATTERN;
  rcPacket.header.cmd = E_RC_CMD_MAX;
  
  //
  // Role
  //
  role = rcRole;


  //
  // Setup and configure rf radio
  //

  radio.begin();

  // We will be using the Ack Payload feature, so please enable it
  radio.enableAckPayload();

  //
  // Open pipes to other nodes for communication
  //

  // This simple sketch opens a single pipe for these two nodes to communicate
  // back and forth.  One listens on it, the other talks to it.

  if ( role == role_sender )
  {
    radio.openWritingPipe(pipe);
  }
  else
  {
    radio.openReadingPipe(1,pipe);
  }

  //
  // Start listening
  //

  if ( role == role_receiver )
    radio.startListening();

  //
  // Dump the configuration of the rf unit for debugging
  //

  radio.printDetails();

  //
  // Attach interrupt handler to interrupt #0 (using pin 2)
  // on BOTH the sender and receiver
  //

  attachInterrupt(digitalPinToInterrupt(RC_RF24_INT_NUM), check_radio, FALLING);
}
Пример #16
0
void decodeurDCF1_c::stop()
{
  if (_pin > 127)
  {
    detachInterrupt(digitalPinToInterrupt(_pin & 127));
    _pin &= 127;
  }
}
Пример #17
0
static void hal_interrupt_init() {
  for (uint8_t i = 0; i < NUM_DIO; ++i) {
      if (lmic_pins.dio[i] == LMIC_UNUSED_PIN)
          continue;

      attachInterrupt(digitalPinToInterrupt(lmic_pins.dio[i]), interrupt_fns[i], RISING);
  }
}
Пример #18
0
/*
 * \brief Turns off the given interrupt.
 */
void detachInterrupt(uint32_t pin)
{
  EExt_Interrupts in = digitalPinToInterrupt(pin);
  if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
    return;

  EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT(1 << in);
}
Пример #19
0
/*
 * \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
 *        Replaces any previous function that was attached to the interrupt.
 */
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
  static int enabled = 0;
  uint32_t config;
  uint32_t pos;
  
  // Assign pin to EIC
  if (pinPeripheral(pin, PIO_EXTINT) != RET_STATUS_OK)
    return;
  
  EExt_Interrupts in = digitalPinToInterrupt(pin);
  if (in == NOT_AN_INTERRUPT || in == EXTERNAL_INT_NMI)
    return;

  if (!enabled) {
    __initialize();
    enabled = 1;
  }

  // Assign callback to interrupt
  callbacksInt[in] = callback;

  // Look for right CONFIG register to be addressed
  if (in > EXTERNAL_INT_7) {
    config = 1;
  } else {
    config = 0;
  }

  // Configure the interrupt mode
  pos = (in - (8 * config)) << 2;
  switch (mode)
  {
    case LOW:
      EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_LOW_Val << pos;
      break;

    case HIGH:
      EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
      break;

    case CHANGE:
      EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
      break;

    case FALLING:
      EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
      break;

    case RISING:
      EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
      break;
  }

  // Enable the interrupt
  EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << in);
}
Пример #20
0
void setup() {
  Serial.begin(115200);
  setup_wifi();
  pinMode(LED_PIN, OUTPUT);
  pinMode(SW_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SW_PIN), handle_toggle_switch_interrupt, FALLING);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
Пример #21
0
void SpeedSense::init(RunMode mode) {
  m_last_input_time = 0;
  if(mode == Task::RunMode::production) {
    attachInterrupt (digitalPinToInterrupt(m_pin), rpm_interrupt, FALLING);
  }
  populate_log_buffer();
  setLogTime(millis());
  Task::init(mode);
}
ImperiumPulseCounter::ImperiumPulseCounter(int objectId, int* pins, int pinCount) : ImperiumObject(objectId, pins, pinCount){
	pinMode(getPin(0), INPUT);
	digitalWrite(getPin(0), HIGH);//pullup

	interruptNum = digitalPinToInterrupt(getPin(0));
	counterObjects[interruptNum] = this;
	count = 0;
	attachInterrupt(interruptNum, interruptHandlers[interruptNum], RISING);
}
Пример #23
0
/**
* SDWriter
* Overrided constructor
* @param String _groupID
*/
SDWriter::SDWriter(String _groupID)
{
	groupID = _groupID;
	pin = 1;

	pinMode(ledPin, OUTPUT);
	pinMode(interruptPin, INPUT_PULLUP);
	attachInterrupt(digitalPinToInterrupt(interruptPin), eraseSD, CHANGE);
}
Пример #24
0
int mcp23s08::getInterruptNumber(byte pin) {
	int intNum = digitalPinToInterrupt(pin);
	if (intNum != NOT_AN_INTERRUPT) {
		#if defined (SPI_HAS_TRANSACTION)
			SPI.usingInterrupt(intNum);
		#endif
		return intNum;
	}
	return 255;
}
Пример #25
0
void setup(){
	// configure interrupt pin or RX as input
	pinMode(pin, INPUT);

	// initialize serial port
	XBee.begin(9600);

	// add interrupt with pin, ISR, and mode
	attachInterrupt(digitalPinToInterrupt(pin), XBee_receive, RISING);
}
UltrasonicSensorArray::UltrasonicSensorArray(int inputPin) {
    _echoPin=inputPin;
    currentSensor=0;
    sensorsEntered=0;
    sensorPointer = this;
    pinMode(_echoPin, INPUT);
    attachInterrupt(digitalPinToInterrupt(_echoPin),
                    UltrasonicSensorArray::sensorStaticHandler, FALLING);//attaches echoPin interupt to ultrasonicIRS's static handler

}
Пример #27
0
void decodeurDCF1_c::start()
{
  int interruption = digitalPinToInterrupt(_pin & 127);

  if (interruption != -1)
  {
    attachInterrupt(interruption, ISR_DCF1, CHANGE);
    _pin |= 128;
  }
}
// メインループ
void Sleep::doLoop() {
  // スリープする
  sleep_cpu();

  // スリープを無効にし、割り込み関数を除く
  sleep_disable();
  detachInterrupt(digitalPinToInterrupt(DeviceSet::PIN_INT));

  // 接続確認から再開する
  app_->shiftToConfirmConnection();
}
Пример #29
0
Anemometer::Anemometer (int pin, int _constr, long _average) : Sensor (pin, constr)
{
	constr = _constr;
	average = _average;
	// enable the internal pullup resistor
	pinMode (pin, INPUT_PULLUP);
	// i.e. anemomenter has to be connected between GND and pin
	attachInterrupt (digitalPinToInterrupt(pin), anemometerClick, FALLING);
	value = 0.0;
	gust = 0.0;
}
Пример #30
0
/**
 *	Constructor arg are pins for channels A and B on right and left encoders
 **/
Odometry::Odometry(byte rightEncoderAPin, byte rightEncoderBPin, byte leftEncoderAPin, byte leftEncoderBPin, float wheelBase, float wheelDiameter, int cpr) {
    pinMode(rightEncoderAPin, INPUT);
    pinMode(rightEncoderBPin, INPUT);
    pinMode(leftEncoderAPin, INPUT);
    pinMode(leftEncoderBPin, INPUT);
    digitalWrite(leftEncoderBPin, HIGH);
    rightEncoderCounter = 0.;
    leftEncoderCounter = 0.;
    attachInterrupt(digitalPinToInterrupt(rightEncoderAPin), rightEncoderAChange, CHANGE);
    setupPinChangeInterrupt(rightEncoderBPin);
    attachInterrupt(digitalPinToInterrupt(leftEncoderAPin), leftEncoderAChange, CHANGE);
    attachInterrupt(digitalPinToInterrupt(leftEncoderBPin), leftEncoderBChange, CHANGE);
    _rightEncoderAPin = rightEncoderAPin;
    _rightEncoderBPin = rightEncoderBPin;
    _leftEncoderAPin = leftEncoderAPin;
    _leftEncoderBPin = leftEncoderBPin;
    _wheelBase = wheelBase;
    _wheelDiameter = wheelDiameter;
    _cpr = cpr;
    
    theta = 0;
    clock = millis();
}