6 Replies Latest reply on Nov 20, 2015 3:20 AM by danescu.andrei

    Fixed time ADC sampling




      I am trying to sample 12 ADC channels and 8 I2C sensors at specific time periods. The timing is like this: 


      A - 10ms - ADC 0, 1, 2


      B - 50ms - I2C 0, 1, 2, 3


      C - 100ms - ADC 3,4,5,6,7


      D - 1000ms - ADC 8,9,10,11 and I2C 4,5,6,7


      I am using a 12ch MUX, DelSig ADC, I2C bloc and for the specific timing generation multiple Frequency Dividers from the 1Khz ILO clock. 


      The two ways I tried were: either read in each ISR all the relevant sensors  OR  set flags and execute in main using IFs ( if(ISR_flagSet -> read sensors)


      Both are suboptimal solutions as the execution time varies, A is much faster than C and they don't work very well. An ideas are more than welcome. I need to read the sensors at fixed time to be able to integrate and differentiate them. 


      I'm not sure this is possible using a single core MCU. 

        • 1. Re: Fixed time ADC sampling

          Use a fixed interrupt timing of 10ms = 100Hz and read all sensors, count the interrupts and and signal that an interrupt happened.


          In the main-loop wait for the interrupt and act on the counter which result has to be considered in the current cycle


          A - 10ms - ADC 0, 1, 2 each cycle


          B - 50ms - I2C 0, 1, 2, 3 cycle %5 == 0


          C - 100ms - ADC 3,4,5,6,7 cycle %10 == 0


          D - 1000ms - ADC 8,9,10,11 and I2C 4,5,6,7 cycle %100 == 0 and reset cycle


          When all sensor reading and calculations can be done within 10ms the time distances will be equal.





          • 2. Re: Fixed time ADC sampling

            Thanks Bob.  Reading the senors takes me different amounts of time: 
            A = 4ms 
            B = 115ms 
            C = 8ms 
            D = 140ms

            I could probably optimize this a bit, maybe reduce the B and D to half but they are still very large. There isn't anything I can do, that's the amount I need for the sensor read (specific to the sensing element)
            The idea is that I need to sample some sensors faster than others. 
            What would you say is the best approach in this case? 


            • 3. Re: Fixed time ADC sampling
                      Problem is in I2C processing speed. How possibly operation B be repeated each 50 ms, while it takes 115ms to execute it? General approach should be: (i) since I2C in non-deterministic, no point to fight for timing accuracy; use buffers to collect incoming data, add some timestamp and process when system is idle. (ii) collect ADC data from all sensors on 10 ms interval into RAM using DMA, process when idle ( ignoring unnecessary data). Other thought: it takes ~100us to acquire 16 bit DelSigADC, why it takes about 1ms in your case? How many bits do you need?   
              • 4. Re: Fixed time ADC sampling

                Hi. Thanks for the reply. It is my typo, B is only 11.5 not 115 :D 

                • 5. Re: Fixed time ADC sampling

                  Since you run interrupt driven you may trigger the I2C-readout 10ms before you need the values will reduce the wait-time for B to 1.5ms.


                  When D was a typo,too then wait-time will be 4ms.





                  • 6. Re: Fixed time ADC sampling

                    I wish it was... D is limited from the I2C sensor side to that time length. 


                    Regarding ADC sampling time - I use a moving average on the values and my code will need further improvements so it seems to add delay up. 


                    The way they were measured was using a toggle pin in the main loop before and after each batch of measurements.




                    So the best way would be to collect everything as fast as I can and just process it at desired times.