Autor Tema: duda, pierdo resolucion al medir rpm  (Leído 353 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado johenrod

  • PIC18
  • ****
  • Mensajes: 265
duda, pierdo resolucion al medir rpm
« en: 21 de Agosto de 2023, 14:31:26 »
cordial saludo, ya he consultado sobre temas de rpm pero ahora estoy tratando de realizar la lectura "RPM" cambiando el numero de ranuras del disco que lee el control optico (creeria que si mas ranuras mejor resolucion).
cuando lo hago con 1 ranura(1pulso) trabaja perfecto pero al hacerlo con 2 ranuras (2 pulsos ) la lectura varia demasiado, el disco ranurado tiene originalmente 8 ranuras (8pulsos) pero asi mas se enloquece la lectura. que podria estar pasando, influye la velocidad del puerto serie para leer rpm? la coloco  a 9600 y a 115200 y trabaja igual tambien cambio el delay y tampoco cambia. gracias de antemano por las sugerencias.



Código: C
  1. // Frequency timer
  2. // Author: Nick Gammon
  3. // Date: 10th February 2012
  4.  
  5. volatile boolean first;
  6. volatile boolean triggered;
  7. volatile unsigned long overflowCount;
  8. volatile unsigned long startTime;
  9. volatile unsigned long finishTime;
  10.  
  11. // here on rising edge
  12. void isr ()
  13. {
  14.   unsigned int counter = TCNT1;  // quickly save it
  15.  
  16.   // wait until we noticed last one
  17.   if (triggered)
  18.     return;
  19.  
  20.   if (first)
  21.     {
  22.     startTime = (overflowCount << 16) + counter;
  23.     first = false;
  24.     return;  
  25.     }
  26.    
  27.   finishTime = (overflowCount << 16) + counter;
  28.   triggered = true;
  29.   detachInterrupt(0);  
  30. }  // end of isr
  31.  
  32. // timer overflows (every 65536 counts)
  33. ISR (TIMER1_OVF_vect)
  34. {
  35.   overflowCount++;
  36. }  // end of TIMER1_OVF_vect
  37.  
  38.  
  39. void prepareForInterrupts ()
  40.   {
  41.   // get ready for next time
  42.   EIFR = _BV (INTF0);  // clear flag for interrupt 0
  43.   first = true;
  44.   triggered = false;  // re-arm for next time
  45.   attachInterrupt(0, isr,FALLING );    
  46.   }  // end of prepareForInterrupts
  47.  
  48.  
  49. void setup () {
  50.   Serial.begin(115200);      
  51.   Serial.println("Frequency Counter");
  52.  
  53.   // reset Timer 1
  54.   TCCR1A = 0;
  55.   TCCR1B = 0;
  56.   // Timer 1 - interrupt on overflow
  57.   TIMSK1 = _BV (TOIE1);   // enable Timer1 Interrupt
  58.   // zero it
  59.   TCNT1 = 0;    
  60.   // start Timer 1
  61.   TCCR1B =  _BV (CS10);  //  no prescaling
  62.  
  63.   // set up for interrupts
  64.   prepareForInterrupts ();  
  65.  
  66. } // end of setup
  67.  
  68. void loop ()
  69.   {
  70.  
  71.   if (!triggered)
  72.     return;
  73.  
  74.   unsigned long elapsedTime = finishTime - startTime;
  75.   float freq = 1.0 / ((float (elapsedTime) * 62.5e-9));  // each tick is 62.5 nS
  76.  
  77.   //Serial.print ("Took: ");
  78.   //Serial.print (elapsedTime);
  79.   // Serial.print (" counts. ");
  80.  
  81.   //Serial.print ("Frequency: ");
  82.   Serial.println (freq);
  83.   Serial.println (freq/1*60);
  84.   Serial.println (freq/2*60);
  85.   //Serial.println (" Hz. ");
  86.  
  87.   // so we can read it  
  88.   delay (500);
  89.  
  90.   prepareForInterrupts ();  
  91. }   // end of loop
« Última modificación: 21 de Agosto de 2023, 14:47:32 por johenrod »
Si algo parece complicado... es por que esta mal explicado.

Desconectado johenrod

  • PIC18
  • ****
  • Mensajes: 265
Re:duda, pierdo resolucion al medir rpm
« Respuesta #1 en: 21 de Agosto de 2023, 14:57:47 »
... las lecturas que requiero esta comprendidas entre 500 y 5000 rpm.
Si algo parece complicado... es por que esta mal explicado.