2

I have a code for finding the rpm of one system calling here as engine , the engine have a hall sensor output and I have tried the following Code


#define SERVO_MIN 2000
#define SERVO_MAX 4000


struct Pulse {
  uint32_t last_toggle;
  uint32_t width;
  uint32_t width_prev;
  uint32_t time_prev;
  bool stateHigh = false;
  uint32_t get_width() {
    noInterrupts();
    uint32_t width_copy = width;
    interrupts();
    return width_copy;
  }
};


struct Engine {
  uint16_t servoMin = 2000, servoMax = 4000;
  uint16_t rpm_desired;
  volatile Pulse pulse;
  uint32_t get_rpm() {
    cli();
    uint32_t rpm_width = pulse.get_width();
    sei();
    return 12e7 / rpm_width;
  }
};

Engine engine1,engine2;

void pin_initialization() {
  // INT2
  pinMode(19, INPUT_PULLUP);

  // Input capture points
  pinMode(49, INPUT_PULLUP);
  pinMode(48, INPUT_PULLUP);

}

void timer4_initialization() {
  cli();
  TCCR4A = 0; TCCR4B = 0; TCCR4C = 0;
  TCCR4A = B10101011;
  TIMSK4 = B00100000;
  TIFR4 = _BV(ICF4);
  TCCR4B = B11011010;
  OCR4A = 39999;
  OCR4B = SERVO_MIN;//7
  OCR4C = SERVO_MIN;//8
  sei();
}

void timer5_initialization() {
  cli();
  TCCR5A = 0; TCCR5B = 0; TCCR5C = 0;
  TCCR5A = B10101011;
  TIMSK5 = B00100000;
  TIFR5 = _BV(ICF5);
  TCCR5B = B11011010;
  OCR5A = 39999;
  OCR5B = SERVO_MIN;//45
  OCR5C = SERVO_MIN;//44
  sei();
}


void timer3_initialization() {
  TCCR3A = 0x00; TCCR3C = 0x00;

  TCNT3 = 0x1FF; // from datasheet
  TIFR3 = _BV(TOV3);
  TIMSK3 = _BV(TOIE3); // overflow enable
  TCCR3B = B00000010; // 8 prescaling
}

volatile uint32_t ovfCount = 0;
ISR(TIMER3_OVF_vect) {
  ovfCount++;
}


void pulseWidthCalculator(Pulse *channel) {
  unsigned char sreg;
  sreg = SREG;
  cli();
  uint16_t tcnt3 = TCNT3;
  uint32_t ovf_count = ovfCount;
  SREG = sreg;

  if (bit_is_set(TIFR3, TOV3) && tcnt3 < 16384) {
    ovf_count++;
  }

  uint32_t time = ovf_count << 16 | tcnt3;

  if (!channel->stateHigh) {
    channel->stateHigh = true;
  } else if ( channel->stateHigh) {
    channel->width = time - channel->last_toggle;
    channel->stateHigh = false;
  }
  channel->last_toggle = time;
  sei();
}

void externalInterrputInitialization() {
  EIFR = _BV(INTF2) ;
  EICRA = 0; EICRB = 0;
  EICRA = B00100000;
  EIMSK = B00000100;
}


ISR(INT2_vect) {
      pulseWidthCalculator(&engine1.pulse);
}

ISR(TIMER4_CAPT_vect) {
  pulseWidthCalculator(&engine2.pulse);
}

ISR(TIMER5_CAPT_vect) {
//  pulseWidthCalculator(&engine2.pulse);
}

void setup() {
  Serial.begin(115200);

  pin_initialization();
  delayMicroseconds(10);

  externalInterrputInitialization();
  delayMicroseconds(10);

  timer3_initialization();
  delayMicroseconds(10);

  timer4_initialization();
  delayMicroseconds(10);

  timer5_initialization();
  delayMicroseconds(10);

}

void loop() {
  Serial.print(engine1.get_rpm());
  Serial.print("\t" );
  Serial.println(engine2.get_rpm());
}

The same hall sensor output is given to the INT2 (pin #19) pin and ICP4(pin #49) and the results obtained is given below

24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
24590   1626
1658    1626
1658    1626
1658    1626
1658    1626
1658    1626
1658    1626
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
1658    1646
4577    1646
4577    1646
4577    1646
4577    1646
4577    1646
4577    1646
4577    1646
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
1667    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
2029    1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1581
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3428571 1546
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
3054    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
2961    1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
4615384 1565
3792    1565
3792    1565
3792    1565
3792    1565
3792    1565
3792    1565
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
3792    1587
1542    1587
1542    1587
1542    1587
1542    1587
1542    1587
1542    1587
1542    1587
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1542    1521
1710    1521
1710    1521
1710    1521
1710    1521
1710    1521
1710    1521
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
1710    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3062    1619
3361    1619
3361    1619
3361    1619
3361    1619
3361    1619
3361    1619
3361    1619
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
3361    1581
1449    1581
1449    1581
1449    1581
1449    1581
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
1449    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1541
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
2166    1628
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
9880    1544
1588    1544
1588    1544
1588    1544
1588    1544
1588    1544
1588    1544
1588    1544
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1588    1586
1693    1586
1693    1586
1693    1586
1693    1586
1693    1586
1693    1586
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1693    1599
1584    1599
1584    1599
1584    1599
1584    1599
1584    1599
1584    1599
1584    1599
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1584    1567
1526    1567
1526    1567
1526    1567
1526    1567
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1526    1646
1693    1646
1693    1646
1693    1646
1693    1646
1693    1646
1693    1646
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
1693    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1682
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
2464    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
1699    1623
3947    1623
3947    1623
3947    1623
3947    1623
3947    1623
3947    1623
3947    1623
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
3947    1688
1537    1688
1537    1688
1537    1688
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1537    1655
1709    1655
1709    1655
1709    1655
1709    1655
1709    1655
1709    1655
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
1709    1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
61696   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699
83449   1699

I have a feeling that the Noise canceller in the input capture pin is helping here, I would like to get help on

  1. How to make the interrupt values also correct
  2. What logical mistake am I making.

how to add the logic of noise cancelling to Interrupt subroutine code.

If I have to add 4/6 such engines what is the best option.

** with a function generator both INT and ICP are giving the correct result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.