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.
Fork of MAX11410-test by
Revision 17:f051190b4bff, committed 2019-03-21
- Comitter:
- laserdad
- Date:
- Thu Mar 21 22:33:50 2019 +0000
- Parent:
- 16:d25147dfdd5c
- Commit message:
- updated to use fixed running average of 10 samples--best compromise of noise and lag.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Mar 18 03:10:55 2019 +0000
+++ b/main.cpp Thu Mar 21 22:33:50 2019 +0000
@@ -7,8 +7,10 @@
#define MISO_1 D12
#define SCLK_1 D13
#define VDD 3.3
+#define NUM_AVGS 10 //this is for the IIR and AVG filters
+#define NUM_CHANNELS 10
+#define NUM_FILTER_TAPS 18 //this is for the FIR filter
-#define NUM_CHANNELS 10
AnalogOut vOut1(A3);
AnalogOut vOut2(A4);
@@ -29,7 +31,7 @@
int32_t data_sum[NUM_CHANNELS];
int32_t ind = 0;
int num_samples = 0;
-int num_avgs = 6;
+int num_avgs = NUM_AVGS;
int ptr =0;
void starting()
@@ -85,7 +87,6 @@
}
}
-#define NUM_FILTER_TAPS 12
float filter_buffer[NUM_CHANNELS][NUM_FILTER_TAPS];
//float lpf[NUM_FILTER_TAPS] = {
// 0.066753767,
@@ -126,22 +127,83 @@
//-0.017928065442454785,
// 0.002042214034417321
//} ;
+//float lpf[NUM_FILTER_TAPS] = {
+// 0.021922032288610106,
+// 0.092023100339809011,
+// 0.202378776983641817,
+// 0.290407730540869380,
+// 0.283915707651621163,
+// 0.175369892196639704,
+// 0.037613514962477279,
+//-0.044492932416232721,
+//-0.049124217289769172,
+//-0.017864664397846109,
+// 0.003294185403418759,
+// 0.004556873736760698
+//};
+//
+////.64Hz 12 tap Kaiser min phase
+//float lpf[NUM_FILTER_TAPS] = {
+// 0.028436833059871377,
+// 0.091256389686655018,
+// 0.176613000083015115,
+// 0.242496965064841336,
+// 0.245802310397899182,
+// 0.179348609303905254,
+// 0.080910192685212115,
+// 0.002992553404179661,
+//-0.027308910150182431,
+//-0.020673186464630775,
+//-0.004453426075815317,
+// 0.004578669005049506
+//};
+
+//0.8Hz 12 tap Kaiser min phase
+//float lpf[NUM_FILTER_TAPS] = {
+// 0.021922032288610106,
+// 0.092023100339809011,
+// 0.202378776983641817,
+// 0.290407730540869380,
+// 0.283915707651621163,
+// 0.175369892196639704,
+// 0.037613514962477279,
+//-0.044492932416232721,
+//-0.049124217289769172,
+//-0.017864664397846109,
+// 0.003294185403418759,
+// 0.004556873736760698
+//};
+
+//0.48Hz 18 tap Kaiser min phase
float lpf[NUM_FILTER_TAPS] = {
- 0.021922032288610106,
- 0.092023100339809011,
- 0.202378776983641817,
- 0.290407730540869380,
- 0.283915707651621163,
- 0.175369892196639704,
- 0.037613514962477279,
--0.044492932416232721,
--0.049124217289769172,
--0.017864664397846109,
- 0.003294185403418759,
- 0.004556873736760698
+ 0.009102526098895891,
+ 0.028984102676961963,
+ 0.062642522588951172,
+ 0.106483963299688053,
+ 0.150349565029814058,
+ 0.180469040434368927,
+ 0.185261380809190523,
+ 0.161071159985581691,
+ 0.114518186128496568,
+ 0.059873816731079298,
+ 0.012753827718163288,
+-0.016437685391431933,
+-0.025878490374639303,
+-0.021158078277756509,
+-0.010912787629287718,
+-0.002164072430294558,
+ 0.002163269630415508,
+ 0.002877752971803134
};
+void iir() {
+ for (int channel = 0; channel< NUM_CHANNELS; channel++) {
+ avg_data[channel] += (channel_data[channel]-avg_data[channel])/NUM_AVGS;
+ }
+}
+
+
void fir( ) {
int coef_ptr;
float filtered_data;
@@ -252,41 +314,6 @@
pc.printf("reg %02x, val %02x, set to %02x\r\n",REG_SOURCE,adc2.read8bits(REG_SOURCE,&int_state),source_config);
pc.printf("reg %02x, val %06x, set to %06x\r\n",REG_STATUS_IE,adc2.read24bits(REG_STATUS_IE,&int_state),status_ie_config);
pc.printf("reg %02x, val %02x, set to %02x\r\n",REG_PGA,adc2.read8bits(REG_PGA,&int_state),pga_config);
- // for each channel 1-5
-/// for(int n=0;n<5;n++)
-/// {
-/// //select channel
-/// char p_ch = 2*n<<4;
-/// char n_ch = 2*n+1;
-/// adc1.write8bitReg(REG_MUX_CTRL0, p_ch | n_ch );
-///
-/// //select data output register and begin conversion
-/// adc1.write8bitReg(REG_CONV_START, (_DEST(n) | SINGLE_CONV) );
-///
-/// //optional: cal Gain
-///
-/// //optional: cal Offset
-///
-/// //optional: store cal parameters
-///
-/// //begin conversion
-///
-/// //wait for interrupt
-/// while(!adc1.interrupt() )
-/// {
-/// wait_ms(CONV_DELAY_MS);//do nothing
-///// pc.printf("waiting for int");
-/// }
-///
-/// //read conversion
-/// channel_data[n] = adc1.read24bitsSigned(REG_DATA0+n,&int_state);
-/// pc.printf("%d, ",channel_data[n]);
-/// } //channel sweep
-/// pc.printf("\r\n");
-///
-///
-/// //config ADC 2: repeat above
-
pc.printf("beginning while loop\r\n");
@@ -346,10 +373,18 @@
// }
// checksum_byte = (char) checksum;
+//****************************************************************************
+ get_avgs(); //calculate running average
+// fir(); //run FIR filter on all channels
+// iir();
+//****************************************************************************
+
+
+
for (int n=0; n<NUM_CHANNELS; n++) {
for(int m=0; m<4; m++) {
- byte2print[n*4+m] = getNthByte(channel_data[n],3-m);
- checksum += getNthByte(channel_data[n],3-m);
+ byte2print[n*4+m] = getNthByte(avg_data[n],3-m);
+ checksum += getNthByte(avg_data[n],3-m);
}
}
checksum_byte = (char) checksum;
@@ -371,8 +406,6 @@
// }
// pc.printf("orig reconstructed\r\n");
- get_avgs(); //calculate running average
-// fir(); //run FIR filter on all channels
// pc.printf("%d, ",t.read_ms());
for (int n=0; n<NUM_CHANNELS; n++) {
