Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question