test program to power up two MAX11410 ADCs and check the inputs of ADC1. Uses MAX11410 library.

Dependencies:   mbed MAX11410

Fork of MAX11410-test by Dan Allen

Revision:
11:1050ffc0e9ef
Parent:
9:5d9509c15e8a
Child:
12:75efd1e2314c
--- a/main.cpp	Fri Mar 16 20:18:44 2018 +0000
+++ b/main.cpp	Thu Mar 22 20:25:00 2018 +0000
@@ -70,7 +70,11 @@
 int main() 
 {
     int32_t channel_data[10];
-    int32_t channel_data2[10];
+//    int32_t channel_data2[10];
+    int32_t newdata;
+    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.
     int32_t checksum2 =0;
     int32_t checksum = 0;
     char checksum_byte = 0;
@@ -88,8 +92,8 @@
     pc.printf("chip ID %d\r\n",adc1.read8bits(REG_PART_ID,&int_state) );
     
     //config ADC 1
-    adc1.reset(); //POR
-//    adc2.reset();
+    adc1.reset(); //POR chip 1
+    adc2.reset(); //POR chip 2
         
     //check default register configs
     pc.printf("default regs for ADC1\r\n");
@@ -98,13 +102,12 @@
     pc.printf("default regs for ADC2\r\n");
     print8bitRegsAdc2(REG_PD,REG_WAIT_START);
     
-    
     //config interrupts, PGA, Buffer, polarity, reference    
     char mode_config = MODE_NORMAL;
     adc1.write8bitReg(REG_PD,mode_config);      //got to normal mode
     adc2.write8bitReg(REG_PD,mode_config);      //got to normal mode
     
-    char filter_config = FIR_SIXTY | _RATE(4) ; //
+    char filter_config = FIR_SIXTY | _RATE(5) ; //five is the max at 35.6 sps
     adc1.write8bitReg( REG_FILTER,filter_config );
     adc2.write8bitReg( REG_FILTER,filter_config );
  
@@ -179,14 +182,16 @@
     {
         for(int n=0; n<5; n++)
         {
-            //read each channel
+            //setup ADCs to read in parallel 
             //select channel
             char p_ch = 2*n+1<<4;
             char n_ch = 2*n;
             adc1.write8bitReg(REG_MUX_CTRL0, p_ch | n_ch );
+            adc2.write8bitReg(REG_MUX_CTRL0, p_ch | n_ch );
             
             //select data output register and begin conversion
             adc1.write8bitReg(REG_CONV_START, (_DEST(n) | SINGLE_CONV) );
+            adc2.write8bitReg(REG_CONV_START, (_DEST(n) | SINGLE_CONV) );
             
             //optional: cal Gain
             
@@ -202,42 +207,33 @@
                 wait_ms(CONV_DELAY_MS);//do nothing
 //                pc.printf("waiting for int");
             }
-            
-            //read conversion
-           channel_data[n] = adc1.read24bitsSigned(REG_DATA0+n,&int_state);
-                     
-        }
-                for(int n=0; n<5; n++)
-        {
-            //read each channel
-            //select channel
-            char p_ch = 2*n+1<<4;
-            char n_ch = 2*n;
-            adc2.write8bitReg(REG_MUX_CTRL0, p_ch | n_ch );
-            
-            //select data output register and begin conversion
-            adc2.write8bitReg(REG_CONV_START, (_DEST(n) | SINGLE_CONV) );
-            
-            //optional: cal Gain
-            
-            //optional: cal Offset
-            
-            //optional: store cal parameters
-            
-            //begin conversion
-            
-            //wait for interrupt
+            if  (channel_data[n] == 0)
+            {
+                channel_data[n] = adc1.read24bitsSigned(REG_DATA0+n,&int_state);
+            }
+            else
+            {
+                newdata = adc1.read24bitsSigned(REG_DATA0+n,&int_state);
+                channel_data[n] += (newdata-channel_data[n])/filterdiv;  
+            }
             while(!adc2.interrupt() )
             {
                 wait_ms(CONV_DELAY_MS);//do nothing
+//                pc.printf("waiting for int");
             }
-            
             //read conversion
-           channel_data[n+5] = adc2.read24bitsSigned(REG_DATA0+n,&int_state);  
+            if  (channel_data[n+5] == 0)
+            {
+                channel_data[n+5] = adc2.read24bitsSigned(REG_DATA0+n,&int_state);
+            }
+            else
+            {
+                newdata = adc2.read24bitsSigned(REG_DATA0+n,&int_state);
+                channel_data[n+5] += (newdata-channel_data[n+5])/filterdiv;  
+            }               
         }
-            
         //            calc checksum
-        checksum= 0x66 + 0x01;
+        checksum = 0x66 + 0x01;
 //        for (int n=0; n<10; n++)
 //        {
 //            checksum += channel_data[n];
@@ -265,22 +261,22 @@
         }
         rpi.putc( checksum_byte );
 //        now reassemble the bits and print them
-
-        for (int n=0;n<10;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]);
-        }
-        checksum2 = 0;
-        for(int n=0;n<10;n++)
-        {
-            checksum2 += channel_data2[n];    
-        }
-        pc.printf("checksum %02X, %08X, %08X\r\n",checksum_byte,checksum,checksum2);
+//
+//        for (int n=0;n<10;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]);
+//        }
+//        checksum2 = 0;
+//        for(int n=0;n<10;n++)
+//        {
+//            checksum2 += channel_data2[n];    
+//        }
+//        pc.printf("checksum %02X, %08X, %08X\r\n",checksum_byte,checksum,checksum2);
     } //end while
 
 } //END MAIN