Yahoo refuse tous les emails du site. Si vous avez une adresse chez un autre prestataire, c'est le moment de l'utiliser ;)

En cas de soucis, n'hésitez pas à aller faire un tour sur la page de contact en bas de page.

Tachymètre minimaliste avec une carte Arduino (version spéciale pour signaux à rapport cyclique variable)

par skywodd | | Langue : C++ | Licence : GPLv3

Description :

Tachymètre minimaliste avec une carte Arduino (version spéciale pour signaux à rapport cyclique variable).

Code source :

Voir le code source brut | Télécharger tachymeter_pulseincycle.ino | Télécharger tachymeter_pulseincycle.ino.zip

/**
 * Tachymétre minimaliste avec une carte Arduino
 */

/* constantes pour la broche de mesure */
const byte PIN_SIGNAL = 2;

/** Fonction setup() */
void setup() {
  
  /* Initialise le port série */
  Serial.begin(115200);
  
  /* Met la broche en entrée */  
  pinMode(PIN_SIGNAL, INPUT_PULLUP);
}

/** Fonction loop() */
void loop() {
 
  /* Mesure la durée de la (demi) période */
  unsigned long periode = pulseInCycle(PIN_SIGNAL, HIGH, 1000000);
  
  /* Affiche le résultat de la mesure en RPM */
  Serial.println(1000000 / periode * 60);
  delay(1000);
}

unsigned long pulseInCycle(byte pin, byte startState, unsigned long timeout) {
 
  /* Compute low-level bitmask and port for direct I/O reading */
  byte bitmask = digitalPinToBitMask(pin);
  volatile byte* port = portInputRegister(digitalPinToPort(pin));
  byte mask = startState ? bitmask : 0;

  /* Get the starting time (for the timeout handling) */
  unsigned long startMicros = micros();

  /* Wait the end of the current starting state (avoid partial measure) */
  while ((*port & bitmask) == mask) {
    if (micros() - startMicros > timeout)
      return 0;
  }
  
  /* Wait for the next starting state */
  while ((*port & bitmask) != mask) {
    if (micros() - startMicros > timeout)
      return 0;
  }
  
  /* Start the counter */
  unsigned long start = micros();

  /* Measure the starting state duration */
  while ((*port & bitmask) == mask) {
    if (micros() - startMicros > timeout)
      return 0;
  }

  /* Measure the ending state duration */
  while ((*port & bitmask) != mask) {
    if (micros() - startMicros > timeout)
      return 0;
  }

  /* Compute the total duration */
  return micros() - start;
}