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.
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.
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?
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.
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.