Tacometro auditivo para autos de 4 cilindros y 4 tiempos

Dependencies:   FastAnalogIn NVIC_set_all_priorities TextLCD mbed-dsp mbed

Fork of KL25Z_FFT_Demo_tony by hermes de la Rosa

Revision:
4:9fae1f1b731a
Parent:
3:1c6625b2d363
--- a/main.cpp	Wed Nov 16 19:48:10 2016 +0000
+++ b/main.cpp	Sat Nov 19 06:47:31 2016 +0000
@@ -10,9 +10,11 @@
 #include "arm_const_structs.h"
 #include "FastAnalogIn.h"
 #include <string>
+#include "TextLCD.h"
 
 FastAnalogIn   Audio(PTC2);
 Serial pc(USBTX, USBRX);
+TextLCD lcd(PTB0,PTB1,PTC9,PTC8,PTA5,PTA4, TextLCD::LCD16x2); // rs, e, d4-d7
 
 
 //#define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
@@ -50,7 +52,7 @@
 int SAMPLE_RATE_HZ = 1000;             // Sample rate of the audio in hertz.
 float freq=0;
                                         // Useful for turning the LED display on and off with commands from the serial port.
-const int FFT_SIZE = 256;                // Size of the FFT.
+const int FFT_SIZE = 512;                // Size of the FFT.
 
 ////////////////////////////////////////////////////////////////////////////////
 // INTERNAL STATE
@@ -66,6 +68,7 @@
 int posicion=0;
 int counter=0;
 int FFTFrequency = 0;
+float filtro[FFT_SIZE];
 
 // Convert a frequency to the appropriate FFT bin it will fall within.
 int frequencyToBin(float frequency)
@@ -93,7 +96,37 @@
         samplingTimer.detach();
     }
 }
-
+void aplicarfiltro()
+{
+    float aux;
+    for(int i=0;i<=FFT_SIZE;i++)
+    { 
+        aux=filtro[i]*magnitudes[i];
+        magnitudes[i]=aux;
+    }    
+}
+void generarfiltro()
+{
+    
+    for(int i=0;i<=FFT_SIZE;i++)
+    {
+        if(i>9)
+        {
+            if(i<82)
+            {
+                filtro[i]=1.0;
+            }
+        }
+        if(i<10)
+        {
+            filtro[i]=0.0;
+        }
+        if(i>81)
+        {
+            filtro[i]=0;    
+        }
+    }
+}
 void samplingBegin()
 {
     // Reset sample buffer position and start callback at necessary rate.
@@ -117,6 +150,14 @@
 {
     NVIC_set_all_irq_priorities(1);
     NVIC_SetPriority(UART0_IRQn, 0);
+    lcd.cls();
+    lcd.printf("RPM: ");
+    lcd.locate(0,1);
+    lcd.printf("F= ");
+    const int columnafrec=3;
+    const int columnarpm=5;
+    const int filafrec=1;
+    const int filarpm=0;
 
     // Begin sampling audio
     samplingBegin();
@@ -160,6 +201,9 @@
             arm_cfft_f32(S, samples, 0, 1);
             // Calculate magnitude of complex numbers output by the FFT.
             arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE);
+            //generar y aplicar filtro de 20hz y 200hz
+            generarfiltro();
+            aplicarfiltro();
             //Obtaining the value of the frequency 
             posicion=0;
             counter=0;
@@ -177,46 +221,103 @@
             samplingBegin();
         }
         freq=(float) posicion;
-        freq=freq*3.906;
-        if(78<freq )
+        freq=freq*1.953125;
+        if(freq<25)
+        {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("<750 ");
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+        }
+        if(freq>=25 )
         {
-            if(freq<85){
-                printf("cuerda 6 freq : %f \n\r",freq);
-                }
+            if(freq<39)
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i  ",(int)(750+(freq-25)*20.83));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+            }
         }
-        if(107<freq )
+        if(freq>=39 )
         {
-            if(freq<113){
-                printf("cuerda 5 freq  :%f  \n\r",freq);
-                }
+            if(freq<54)
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",(int)(1000+(freq-39)*33.33));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+                
+            }
         }
-        if(142<freq )
+        if(freq>=54 )
         {
-            if(freq<150){
-                printf("cuerda 4 freq  :%f  \n\r",freq);
-                }
+            if(freq<=61 )
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",(int)(1500+(freq-54)*71.42));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+            }
+        }
+        if(freq>61 )
+        {
+            if(freq<=82 )
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",((int)(2000+(freq-62)*23.8)));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+            }
         }
-        if(192<freq )
+        if(freq>82  )
         {
-            if(freq<198){
-                printf("cuerda 3 freq  :%f  \n\r",freq);
-                }
+            if(freq<=102 )
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",(int)(2500+(freq-83)*25));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i ",(int)freq);
+            }
         }
-        if(243<freq )
+        if(freq>102 )
+        {
+            if(freq<=107)
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",(int)(3000+(freq-103)*100));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i",(int)freq);
+            }
+        }
+        if(freq>107 )
         {
-            if(freq<250){
-                printf("cuerda 2 freq  :%f  \n\r",freq);
-                }
+            if(freq<=129)
+            {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("%i ",(int)(3500+(freq-108)*22.72));
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i",(int)freq);
+            }
         }
-        if(327<freq )
+        if(freq>129)
         {
-            if(freq<335){
-                printf("cuerda 1 freq  :%f  \n\r",freq);
-                }
+             
+             if(freq<=133)
+             {
+                 lcd.locate(columnarpm,filarpm);
+                 lcd.printf("4000 ");
+                 lcd.locate(columnafrec,filafrec);
+                 lcd.printf("%i",(int)freq);
+             }
         }
-        
-
-        
+        if(freq>133)
+        {
+                lcd.locate(columnarpm,filarpm);
+                lcd.printf("4000>");
+                lcd.locate(columnafrec,filafrec);
+                lcd.printf("%i",(int)freq);
+        }
     }
     
 }