W
W
we12020-02-09 15:36:06
Arduino
we1, 2020-02-09 15:36:06

Why do other functions outside of the loop affect the running time of the ADC?

Inside the function, a thousand voltage measurements are taken in order to then calculate the frequency. There are no outputs or queries in this function. But delta_time can be affected by other functions that are written after or even outside of the analyze() function. For example, Serial.print() has a very strong effect.

void analyze (){
  i = BUFFER_SIZE;

  ADMUX=B11100111;
  delay(10);

  time = micros();

  while (i--)
  {
    ADCSRA=B11000010;   // 1/4
    while (ADCSRA & (1 << ADSC));   // Ожидание флага окончания преобразования
    buffer[ i ] = ADCH;
  }

  delta_time = micros() - time;
}


Obviously, during the execution of the poll, a timer is triggered, which is needed for the micros () function to work. But its influence should not increase the received time by 20-25 percent. It turns out that if I make several calls to Serial.print() in the loop() function after calling analyze(), then the delta_time time increases quite noticeably. Although I don't understand how. There is no way to turn off the timer, because without it I cannot find out the ADC polling cycle time.

Two questions:
1) why does Serial.print() and even simple arithmetic expressions "slow down" polling the ADC elsewhere in the program?
2) how can you calculate the time in another way, without using timer 0 and micros() / millis()?

PS The code example is a slightly modified AnRead() function from CyberLib.h

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2020-02-09
@gbg

Obviously, this nonsense also uses interrupts to execute Serial.println - so it loads the next character into the port after sending the previous one.
As soon as it comes to using realtime, the first thing to do is to throw the arduino environment to hell and install AVR_Studio.
There is a normal, not littered with junk for beginners, GCC compiler and normal, not dragging C ++ libraries.
Then, you need to deal with such a phenomenon as interruptions. The datasheet for the controller says how to set up the ADC to call an interrupt at the end of the next measurement, and you can also write an interrupt handler in assembler. Then you won’t need the function of counting the number of milliseconds, because you will know the program execution time for sure - it can be calculated by the number of cycles per ADC measurement (see datasheet) and the number of instructions in the interrupt handler (see datasheet again, most controller instructions does exactly in 1 cycle). In the handler, you need to quickly put the new count into the array and dump it.
As far as I remember, the maximum sampling frequency that can be squeezed out of the ADC is about 32 kHz when the controller is clocked from 20 MHz

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question