Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-dsp mbed capstone_display_2
Diff: main.cpp
- Revision:
- 3:30dcfcf9412c
- Parent:
- 2:8ae58834937f
- Child:
- 4:9ee3ae61db7f
diff -r 8ae58834937f -r 30dcfcf9412c main.cpp --- a/main.cpp Mon Mar 10 23:33:38 2014 +0000 +++ b/main.cpp Thu Apr 03 20:30:18 2014 +0000 @@ -1,18 +1,26 @@ #include "mbed.h" #include "FIR_f32.h" #include "arm_math.h" +#include "display.h" #define f_sampling 2000 //the sampling frequency #define NumTaps 27 //the number of FIR coefficients -#define BlockSize 1024 //the size of the buffer +#define BlockSize 2048 //the size of the buffer + +SPI spi(p5, p6, p7); +DigitalOut cs(p8); +ST7735_LCD disp( p14, p13, p12, p10, p11); //for digital display +display lcd(&disp); Serial pc(USBTX, USBRX); //USB serial for PC, to be removed later AnalogIn input(p15); //pin 15 for analog reading +AnalogOut waveOut(p18); float32_t waveform[BlockSize]; //array of input data float32_t postFilterData[BlockSize]; //array of filtered data bool fullRead; //whether the MBED has finish bool waitForNext; //the filter coefficients for a band pass filter, consider changing to doubles if not precise enough + float32_t pCoeffs[NumTaps] = { 0.012000000000000, 0.012462263166161, -0.019562318415964, -0.026175892863747, 0.031654803781611, 0.050648026372209, -0.032547136829180, -0.070997780956819, @@ -21,6 +29,29 @@ -0.020568171368382, 0.094643188024728, 0.032992306874351, -0.070997780956815, -0.032547136829177, 0.050648026372211, 0.031654803781612, -0.026175892863746, -0.019562318415964, 0.012462263166161, 0.012000000000000 }; +// */ +/* +float32_t pCoeffs[NumTaps] = +{ + -0.00130297171184699, -0.00456436168827987, -0.00757978930408609, -0.00696944302000657, + -0.00100059082174453, 0.00812867271498616, 0.0148953048520266, 0.0137935053264369, + 0.00350484996910501, -0.0112195199182290, -0.0216305356563913, -0.0202538386423356, + -0.00609419278464673, 0.0137348990478646, 0.0275645559768492, 0.0261107576153156, + 0.00866220574766616, -0.0156131009924596, -0.0324957126350438, -0.0311514181527343, + -0.0110879396617141, 0.0168179120126559, 0.0362758644669149, 0.0352058948414930, + 0.0132978095684398, -0.0172706692984796, -0.0386711719606551, -0.0379507530937637, + -0.0149419841919419, 0.0172996706397712, 0.0400000000000000, 0.0397279151377323, + 0.0164353142069562, -0.0164055618588934, -0.0396949785867063, -0.0399629114640568, + -0.0172605211576678, 0.0149790280104299, 0.0379815311949588, 0.0386933807609119, + 0.0172844840085185, -0.0132904115318555, -0.0352024033389307, -0.0362742608690452, + -0.0168170401765007, 0.0110885383139611, 0.0311518509994083, 0.0324959946809230, + 0.0156132578212073, -0.00866213238945794, -0.0261107291487171, -0.0275645472357883, + -0.0137348973043660, 0.00609419268963993, 0.0202538383407381, 0.0216305354798053, + 0.0112195198475825, -0.00350484999121515, -0.0137935053321021, -0.0148953048532365, + -0.00812867271519995, 0.00100059082171422, 0.00696944302000319, 0.00757978930408577, + 0.00456436168827984, 0.00130297171184699 + }; + //*/ float32_t pState[NumTaps + BlockSize - 1]; @@ -49,6 +80,46 @@ } sampler.detach(); } + +void outputWaveform() +{ + Ticker outputter; + waitForNext = true; + outputter.attach_us(&readPoint, (int) (1000000/f_sampling) ); //output data according the sampling freq + for (int i = 0; i < BlockSize; i++) + { + while (waitForNext); //wait until the ticker calls for the next data point + waveOut.write(postFilterData[i]); + waitForNext = true; + } + outputter.detach(); +} + +int setPot(int wiperNo, float kOhms) +{ + //257 steps (8 bits + 1), see section 7.0 for SPI instructions + float Rmax = 100000; + spi.frequency(2000000); + spi.format(16, 0); //16 bits, mode b00 + float ratio = kOhms * 1000.0 / Rmax; + if (ratio > 1) ratio = 1; + if (ratio < 0) ratio = 0; + int dataBits = (int) (ratio * 0x100); + int command = wiperNo << 12; //setting the Address and Command bits + command += dataBits; //add in the digital setting + spi.write(command); + return command; +} + +float32_t rms() +{ + float32_t rms = 0; + for(int i = 0; i < BlockSize; i++) + { + rms += postFilterData[i]*postFilterData[i]; + } + return sqrt(rms/BlockSize); +} int main() { @@ -69,6 +140,9 @@ int state = 0; uint16_t numTaps = NumTaps; uint32_t blockSize = BlockSize; + char buffer[32]; //for debugging scanf things + char* outputString; + float32_t estimate = 0; while(1) { //pc.printf("While loop\n\r"); @@ -79,7 +153,7 @@ //pc.printf("pre-filter init\n\r"); arm_fir_init_f32(filter, numTaps, pCoeffs, pState, blockSize); //pc.printf("Pre-attachment"); - + spi.frequency(1000000); state = 1; pc.printf("Done with init.\n\r"); break; @@ -87,10 +161,10 @@ case 1: //read data, take samples pc.printf("Reading data.\n\r"); readSamples(); - state = 2; + state = 3; break; - case 2: //printout to pc connection + case 2: //printout to pc connection or other output debugging //pc.printf("into print\n\r"); /* for (int i = 0; i < BlockSize; i++) @@ -98,25 +172,66 @@ pc.printf("Waveform contents:%f\n\r", waveform[i]); } */ - state = 3; + for (int i = 0; i < 10; i++) outputWaveform(); + wait_ms(500); + state = 1; break; case 3: //filter? pc.printf("Filtering?\n\r"); arm_fir_f32(filter, waveform, postFilterData, blockSize); - state = 6; + state = 2; break; case 4: //FFT? break; case 5: //output, write to display and PWM tone + sprintf(outputString, "RMS = %f", estimate); + lcd.print(outputString); + state = 1; break; - case 6: //calculate the average voltage + case 6: //calculate the average voltage maybe depreciated + /* + * + * + double sum = 0; for (int i = 0; i < BlockSize; i++) sum += postFilterData[i]; double average = sum/BlockSize*3.3; //*3.3 V_ref (array stored as fractions of V_ref) pc.printf("Average = %f\n\r", average); wait_ms(500); + state = 2; + */ + estimate = rms(); + pc.printf("RMS = %f", estimate); + state = 5; + break; + case 7: //estimate amplitude + pc.printf("Into estimation\n\r"); + int peaks = 0; + float sum = 0.0; + float prev, current, next; + for (int i = 0+1; i < BlockSize-1; i++) + { + prev = postFilterData[i-1]; + current = postFilterData[i]; + next = postFilterData[i+1]; + if (prev < current && next < current) //local max + { + sum += current; + peaks++; + } + } + float average = sum/peaks; + pc.printf("Average of peaks (scalar) = %f\n\r", average); state = 1; break; + + case 8: //digital pot interfacing and calibration + pc.printf("kOhms?\n\r"); + pc.scanf("%s", buffer); + float value = atof(buffer); + pc.printf("Command: %x Scanned:%f\n\r", setPot(0, value), value); + wait_ms(250); + break; default: break; }