Tacometro auditivo para autos de 4 cilindros y 4 tiempos

Dependencies:   FastAnalogIn NVIC_set_all_priorities TextLCD mbed-dsp mbed

Fork of tacometrojala by Efrain Duarte

Committer:
luisda130595
Date:
Tue Nov 15 23:04:41 2016 +0000
Revision:
2:177596541c8d
Parent:
1:7421267b0777
Child:
3:1c6625b2d363
Pa' Chuy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony1tf 0:b8c9dffbbe7e 1 // Audio Spectrum Display
tony1tf 0:b8c9dffbbe7e 2 // Copyright 2013 Tony DiCola (tony@tonydicola.com)
tony1tf 0:b8c9dffbbe7e 3 // Code ported from the guide at http://learn.adafruit.com/fft-fun-with-fourier-transforms?view=all
tony1tf 0:b8c9dffbbe7e 4 // mods by Tony Abbey to simplify code to drive tri-colour LED as a "colour organ"
tony1tf 0:b8c9dffbbe7e 5
tony1tf 0:b8c9dffbbe7e 6 #include "mbed.h"
tony1tf 0:b8c9dffbbe7e 7 #include "NVIC_set_all_priorities.h"
tony1tf 0:b8c9dffbbe7e 8 #include <ctype.h>
tony1tf 0:b8c9dffbbe7e 9 #include "arm_math.h"
tony1tf 0:b8c9dffbbe7e 10 #include "arm_const_structs.h"
tony1tf 0:b8c9dffbbe7e 11 #include "FastAnalogIn.h"
luisda130595 1:7421267b0777 12 #include <string>
tony1tf 0:b8c9dffbbe7e 13
luisda130595 1:7421267b0777 14 FastAnalogIn Audio(PTC2);
tony1tf 0:b8c9dffbbe7e 15 Serial pc(USBTX, USBRX);
tony1tf 0:b8c9dffbbe7e 16
tony1tf 0:b8c9dffbbe7e 17
tony1tf 0:b8c9dffbbe7e 18 //#define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
tony1tf 0:b8c9dffbbe7e 19
tony1tf 0:b8c9dffbbe7e 20
tony1tf 0:b8c9dffbbe7e 21 #ifndef RGBW_ext
tony1tf 0:b8c9dffbbe7e 22 // RGB direct output to PWM channels - on-board RGB LED
tony1tf 0:b8c9dffbbe7e 23 PwmOut gled(LED_GREEN);
tony1tf 0:b8c9dffbbe7e 24 PwmOut rled(LED_RED);
tony1tf 0:b8c9dffbbe7e 25 PwmOut bled(LED_BLUE);
tony1tf 0:b8c9dffbbe7e 26 #else
tony1tf 0:b8c9dffbbe7e 27 // HSI to RGBW conversion with direct output to external PWM channels - RGBW LED
tony1tf 0:b8c9dffbbe7e 28 // hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); //Red, Green, Blue, White
tony1tf 0:b8c9dffbbe7e 29 #endif
tony1tf 0:b8c9dffbbe7e 30
tony1tf 0:b8c9dffbbe7e 31 // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !!
tony1tf 0:b8c9dffbbe7e 32 // More info at https://mbed.org/questions/1387/How-can-I-access-the-FTFA_FOPT-register-/
tony1tf 0:b8c9dffbbe7e 33 extern "C" void NMI_Handler() {
tony1tf 0:b8c9dffbbe7e 34 DigitalIn test(PTA4);
tony1tf 0:b8c9dffbbe7e 35 }
tony1tf 0:b8c9dffbbe7e 36
tony1tf 0:b8c9dffbbe7e 37
tony1tf 0:b8c9dffbbe7e 38 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 39 // CONFIGURATION
tony1tf 0:b8c9dffbbe7e 40 // These values can be changed to alter the behavior of the spectrum display.
tony1tf 0:b8c9dffbbe7e 41 // KL25Z limitations
tony1tf 0:b8c9dffbbe7e 42 // -----------------
tony1tf 0:b8c9dffbbe7e 43 // - When used with the Spectrogram python script :
tony1tf 0:b8c9dffbbe7e 44 // There is a substantial time lag between the music and the screen output.
tony1tf 0:b8c9dffbbe7e 45 // Max allowed SAMPLE_RATE_HZ is 40000
tony1tf 0:b8c9dffbbe7e 46 // Max allowed FFT_SIZE is 64
tony1tf 0:b8c9dffbbe7e 47 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 48 // A value >= 1000 and <= 1000 + PIXEL_COUNT fixes the output to a single frequency
tony1tf 0:b8c9dffbbe7e 49 // window = a single color.
luisda130595 1:7421267b0777 50 int SAMPLE_RATE_HZ = 1000; // Sample rate of the audio in hertz.
luisda130595 1:7421267b0777 51
tony1tf 0:b8c9dffbbe7e 52 // Useful for turning the LED display on and off with commands from the serial port.
luisda130595 1:7421267b0777 53 const int FFT_SIZE = 256; // Size of the FFT.
tony1tf 0:b8c9dffbbe7e 54
tony1tf 0:b8c9dffbbe7e 55 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 56 // INTERNAL STATE
tony1tf 0:b8c9dffbbe7e 57 // These shouldn't be modified unless you know what you're doing.
tony1tf 0:b8c9dffbbe7e 58 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 59 const static arm_cfft_instance_f32 *S;
tony1tf 0:b8c9dffbbe7e 60 Ticker samplingTimer;
tony1tf 0:b8c9dffbbe7e 61 float samples[FFT_SIZE*2];
tony1tf 0:b8c9dffbbe7e 62 float magnitudes[FFT_SIZE];
tony1tf 0:b8c9dffbbe7e 63 int sampleCounter = 0;
tony1tf 0:b8c9dffbbe7e 64
luisda130595 2:177596541c8d 65 int maxFrequencyValue = 0;
tony1tf 0:b8c9dffbbe7e 66
luisda130595 2:177596541c8d 67 int FFTFrequency = 0;
tony1tf 0:b8c9dffbbe7e 68
tony1tf 0:b8c9dffbbe7e 69 // Convert a frequency to the appropriate FFT bin it will fall within.
tony1tf 0:b8c9dffbbe7e 70 int frequencyToBin(float frequency)
tony1tf 0:b8c9dffbbe7e 71 {
tony1tf 0:b8c9dffbbe7e 72 float binFrequency = float(SAMPLE_RATE_HZ) / float(FFT_SIZE);
tony1tf 0:b8c9dffbbe7e 73 return int(frequency / binFrequency);
tony1tf 0:b8c9dffbbe7e 74 }
tony1tf 0:b8c9dffbbe7e 75
tony1tf 0:b8c9dffbbe7e 76
tony1tf 0:b8c9dffbbe7e 77
tony1tf 0:b8c9dffbbe7e 78 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 79 // SAMPLING FUNCTIONS
tony1tf 0:b8c9dffbbe7e 80 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 81
tony1tf 0:b8c9dffbbe7e 82 void samplingCallback()
tony1tf 0:b8c9dffbbe7e 83 {
tony1tf 0:b8c9dffbbe7e 84 // Read from the ADC and store the sample data
tony1tf 0:b8c9dffbbe7e 85 samples[sampleCounter] = (1023 * Audio) - 511.0f;
tony1tf 0:b8c9dffbbe7e 86 // Complex FFT functions require a coefficient for the imaginary part of the input.
tony1tf 0:b8c9dffbbe7e 87 // Since we only have real data, set this coefficient to zero.
tony1tf 0:b8c9dffbbe7e 88 samples[sampleCounter+1] = 0.0;
tony1tf 0:b8c9dffbbe7e 89 // Update sample buffer position and stop after the buffer is filled
tony1tf 0:b8c9dffbbe7e 90 sampleCounter += 2;
tony1tf 0:b8c9dffbbe7e 91 if (sampleCounter >= FFT_SIZE*2) {
tony1tf 0:b8c9dffbbe7e 92 samplingTimer.detach();
tony1tf 0:b8c9dffbbe7e 93 }
tony1tf 0:b8c9dffbbe7e 94 }
tony1tf 0:b8c9dffbbe7e 95
tony1tf 0:b8c9dffbbe7e 96 void samplingBegin()
tony1tf 0:b8c9dffbbe7e 97 {
tony1tf 0:b8c9dffbbe7e 98 // Reset sample buffer position and start callback at necessary rate.
tony1tf 0:b8c9dffbbe7e 99 sampleCounter = 0;
tony1tf 0:b8c9dffbbe7e 100 samplingTimer.attach_us(&samplingCallback, 1000000/SAMPLE_RATE_HZ);
tony1tf 0:b8c9dffbbe7e 101 }
tony1tf 0:b8c9dffbbe7e 102
tony1tf 0:b8c9dffbbe7e 103 bool samplingIsDone()
tony1tf 0:b8c9dffbbe7e 104 {
tony1tf 0:b8c9dffbbe7e 105 return sampleCounter >= FFT_SIZE*2;
tony1tf 0:b8c9dffbbe7e 106 }
tony1tf 0:b8c9dffbbe7e 107
tony1tf 0:b8c9dffbbe7e 108 ////////////////////////////////////////////////////////////////////////////////
luisda130595 1:7421267b0777 109 // FREQUENCY
tony1tf 0:b8c9dffbbe7e 110 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 111
luisda130595 1:7421267b0777 112
luisda130595 2:177596541c8d 113 int set_ArrayPosition()
tony1tf 0:b8c9dffbbe7e 114 {
luisda130595 1:7421267b0777 115 float maxValue = 0.0;
luisda130595 2:177596541c8d 116 int arrayPosition = 0;
luisda130595 2:177596541c8d 117
luisda130595 1:7421267b0777 118 for(int counter=0; counter < FFT_SIZE; counter++)
luisda130595 1:7421267b0777 119 {
luisda130595 1:7421267b0777 120 if(magnitudes[counter] > maxValue){
luisda130595 1:7421267b0777 121 maxValue = magnitudes[counter];
luisda130595 2:177596541c8d 122 arrayPosition = counter;
tony1tf 0:b8c9dffbbe7e 123 }
tony1tf 0:b8c9dffbbe7e 124 }
luisda130595 2:177596541c8d 125
luisda130595 2:177596541c8d 126 return arrayPosition;
tony1tf 0:b8c9dffbbe7e 127 }
tony1tf 0:b8c9dffbbe7e 128
luisda130595 1:7421267b0777 129 int get_FFTFrequency()
luisda130595 1:7421267b0777 130 {
luisda130595 1:7421267b0777 131 maxFrequencyValue = SAMPLE_RATE_HZ/2;
luisda130595 1:7421267b0777 132
luisda130595 1:7421267b0777 133 set_ArrayPosition();
luisda130595 1:7421267b0777 134
luisda130595 2:177596541c8d 135 return (maxFrequencyValue*sarrayPosition)/(FFT_SIZE-1);
luisda130595 1:7421267b0777 136
luisda130595 1:7421267b0777 137 }
tony1tf 0:b8c9dffbbe7e 138 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 139 // MAIN FUNCTION
tony1tf 0:b8c9dffbbe7e 140 ////////////////////////////////////////////////////////////////////////////////
tony1tf 0:b8c9dffbbe7e 141
tony1tf 0:b8c9dffbbe7e 142 int main()
tony1tf 0:b8c9dffbbe7e 143 {
tony1tf 0:b8c9dffbbe7e 144 NVIC_set_all_irq_priorities(1);
tony1tf 0:b8c9dffbbe7e 145 NVIC_SetPriority(UART0_IRQn, 0);
tony1tf 0:b8c9dffbbe7e 146
tony1tf 0:b8c9dffbbe7e 147 // Begin sampling audio
tony1tf 0:b8c9dffbbe7e 148 samplingBegin();
tony1tf 0:b8c9dffbbe7e 149
tony1tf 0:b8c9dffbbe7e 150 // Init arm_ccft_32
tony1tf 0:b8c9dffbbe7e 151 switch (FFT_SIZE)
tony1tf 0:b8c9dffbbe7e 152 {
tony1tf 0:b8c9dffbbe7e 153 case 16:
tony1tf 0:b8c9dffbbe7e 154 S = & arm_cfft_sR_f32_len16;
tony1tf 0:b8c9dffbbe7e 155 break;
tony1tf 0:b8c9dffbbe7e 156 case 32:
tony1tf 0:b8c9dffbbe7e 157 S = & arm_cfft_sR_f32_len32;
tony1tf 0:b8c9dffbbe7e 158 break;
tony1tf 0:b8c9dffbbe7e 159 case 64:
tony1tf 0:b8c9dffbbe7e 160 S = & arm_cfft_sR_f32_len64;
tony1tf 0:b8c9dffbbe7e 161 break;
tony1tf 0:b8c9dffbbe7e 162 case 128:
tony1tf 0:b8c9dffbbe7e 163 S = & arm_cfft_sR_f32_len128;
tony1tf 0:b8c9dffbbe7e 164 break;
tony1tf 0:b8c9dffbbe7e 165 case 256:
tony1tf 0:b8c9dffbbe7e 166 S = & arm_cfft_sR_f32_len256;
tony1tf 0:b8c9dffbbe7e 167 break;
tony1tf 0:b8c9dffbbe7e 168 case 512:
tony1tf 0:b8c9dffbbe7e 169 S = & arm_cfft_sR_f32_len512;
tony1tf 0:b8c9dffbbe7e 170 break;
tony1tf 0:b8c9dffbbe7e 171 case 1024:
tony1tf 0:b8c9dffbbe7e 172 S = & arm_cfft_sR_f32_len1024;
tony1tf 0:b8c9dffbbe7e 173 break;
tony1tf 0:b8c9dffbbe7e 174 case 2048:
tony1tf 0:b8c9dffbbe7e 175 S = & arm_cfft_sR_f32_len2048;
tony1tf 0:b8c9dffbbe7e 176 break;
tony1tf 0:b8c9dffbbe7e 177 case 4096:
tony1tf 0:b8c9dffbbe7e 178 S = & arm_cfft_sR_f32_len4096;
tony1tf 0:b8c9dffbbe7e 179 break;
tony1tf 0:b8c9dffbbe7e 180 }
tony1tf 0:b8c9dffbbe7e 181
luisda130595 1:7421267b0777 182 while(true) {
tony1tf 0:b8c9dffbbe7e 183 // Calculate FFT if a full sample is available.
tony1tf 0:b8c9dffbbe7e 184 if (samplingIsDone()) {
tony1tf 0:b8c9dffbbe7e 185 // Run FFT on sample data.
tony1tf 0:b8c9dffbbe7e 186 arm_cfft_f32(S, samples, 0, 1);
tony1tf 0:b8c9dffbbe7e 187 // Calculate magnitude of complex numbers output by the FFT.
tony1tf 0:b8c9dffbbe7e 188 arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE);
luisda130595 1:7421267b0777 189 //Obtaining the value of the frequency
luisda130595 1:7421267b0777 190 FFTFrequency = get_FFTFrequency();
luisda130595 1:7421267b0777 191
luisda130595 1:7421267b0777 192
tony1tf 0:b8c9dffbbe7e 193 // Restart audio sampling.
tony1tf 0:b8c9dffbbe7e 194 samplingBegin();
luisda130595 1:7421267b0777 195 }
tony1tf 0:b8c9dffbbe7e 196 }
luisda130595 1:7421267b0777 197
tony1tf 0:b8c9dffbbe7e 198 }