test program to power up two MAX11410 ADCs and check the inputs of ADC1. Uses MAX11410 library.
Fork of MAX11410-test by
Diff: main.cpp
- Revision:
- 12:75efd1e2314c
- Parent:
- 11:1050ffc0e9ef
- Child:
- 13:02c28a5a26c0
--- a/main.cpp Thu Mar 22 20:25:00 2018 +0000 +++ b/main.cpp Sat Feb 23 00:58:04 2019 +0000 @@ -1,14 +1,3 @@ -// I2CU - Search for devices on an I2C bus -// Copyright (c) 2009, sford -// Released under the MIT License: http://mbed.org/license/mit -// -// Goes through each device address, seeing if it gets a response -// - for every device if finds, it prints the 8-bit address -// - if the program hangs, the bus isn't working (check pull-ups etc) -// - if it doesn't find your device, check power/bus wiring etc -// -// - Note: if you plug / unplug devices try cycling power - #include "mbed.h" #include "MAX11410.h" @@ -31,6 +20,18 @@ //Serial pc(USBTX, USBRX, 9600); Serial rpi(PA_9,PA_10,115200); +#define NUM_CHANNELS 10 + +int32_t channel_data[NUM_CHANNELS]; +int32_t new_data; +int32_t avg_data; +int32t data_buffer[NUM_CHANNELS][10]; +int32t data_sum[NUM_CHANNELS]; +int32t ind = 0; +int num_samples = 0; +int num_avgs = 4; +int ptr =-1; + void starting() { pc.printf("this program has started\r\n"); @@ -67,11 +68,37 @@ } +void get_avgs() { + ptr++; //increment pointer + if (ptr == num_avgs) { + ptr = 0; //reset pointer + } + if num_samples < num_avgs { + num_samples++; //increment number of samples + } + else { + for (int n = 0; n<NUM_CHANNELS; n++) { //loop over channels + data_sum[n] -= data_buffer[n][ptr]; //subtract oldest sample + } + } + for (int n=0;n<NUM_CHANNELS;n++) { + data_buffer[n][ptr] = channel_data[n]; //put new sample in buffer + data_sum[n] += channel_data[n]; //add new sample + avg_data[n] = data_sum[n] / num_avgs; //get new average + } +{ + + int main() { - int32_t channel_data[10]; -// int32_t channel_data2[10]; - int32_t newdata; + +// int32_t channel_data2[NUM_CHANNELS]; + + + + + + int filterdiv = 4; //this is the divisor in the IIR channel filter. // It adds 1/filterdiv of the difference between the new sample and the average to the new average // This is integer math, so I am sticking to multiples of 2 for now. @@ -213,8 +240,9 @@ } else { - newdata = adc1.read24bitsSigned(REG_DATA0+n,&int_state); - channel_data[n] += (newdata-channel_data[n])/filterdiv; + new_data = adc1.read24bitsSigned(REG_DATA0+n,&int_state); +// channel_data[n] += (new_data-channel_data[n])/filterdiv; + channel_data[n] = new_data; } while(!adc2.interrupt() ) { @@ -228,19 +256,20 @@ } else { - newdata = adc2.read24bitsSigned(REG_DATA0+n,&int_state); - channel_data[n+5] += (newdata-channel_data[n+5])/filterdiv; + new_data = adc2.read24bitsSigned(REG_DATA0+n,&int_state); + //channel_data[n+5] += (new_data-channel_data[n+5])/filterdiv; + channel_data[n] = new_data; } } // calc checksum checksum = 0x66 + 0x01; -// for (int n=0; n<10; n++) +// for (int n=0; n<NUM_CHANNELS; n++) // { // checksum += channel_data[n]; // } // checksum_byte = (char) checksum; - for (int n=0; n<10; n++) + for (int n=0; n<NUM_CHANNELS; n++) { for(int m=0;m<4;m++) { @@ -262,17 +291,20 @@ rpi.putc( checksum_byte ); // now reassemble the bits and print them // -// for (int n=0;n<10;n++) +// for (int n=0;n<NUM_CHANNELS;n++) // { // channel_data2[n] = (byte2print[4*n]<<24 ) | ( byte2print[4*n+1]<<16 ) | ( byte2print[4*n+2] << 8 ) | byte2print[4*n+3]; // } // pc.printf("orig reconstructed\r\n"); -// for (int n=0;n<10;n++) -// { -// pc.printf("%d, %d, %d\r\n",n,channel_data[n],channel_data2[n]); -// } + + get_avgs(); //calculate running average + + for (int n=0;n<NUM_CHANNELS;n++) + { + pc.printf("%d, %d\r\n",n,avg_data[n]); + } // checksum2 = 0; -// for(int n=0;n<10;n++) +// for(int n=0;n<NUM_CHANNELS;n++) // { // checksum2 += channel_data2[n]; // }