-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slow analogRead() - wrong ADCSRA/ADPS setting #31
Comments
I think I already found it... The ADC prescaler in ADCSRA (ADPS[2:0]) is set to 7, which is the Atmega default (sysclk/128). On LGT, the default reset value is 5 (sysclk/32) (and may be even 4 (sysclk/16), depending on sysclk). But somewhere, a prescaler init of of 7 (sysclk/128 = atmega default) looks like to happen. If I run my speed test after switching the prescaler: ADCSRA=ADCSRA&0xF8|4; // 32MHz sysclk/16 == 2MHz I get which is MUCH better than the 5733 cycles before. Depending on sysclk, the prescaler should be set to: I did not see much of a difference running the ADC at 4MHz with 10 bits resolution. It then runs at 437 cpu cycles. |
As a github noob, I accidently opened the pull request as new iss #32 |
Warning: changing the prescaler settings for the ADC will likely affect the apparent input impedance of the adc. This means that the readings of high impedance sources will vary, depending on the version of the core it was compiled against. This is probably acceptable, but I just thought I'd mention it. :D |
@thegoodhen that's a good point. According to the datasheets, sample and hold takes 1.5 ADC clocks, and I assume it is implemented the typical way (load a cap and then disconnect the input switch). In this case, the signal will be less time exposed to a capacitor. Anyway, same as you:
I'd be curious to know if what I wrote is actually correct :) |
I found in wiring_analog.c from line 153 ADC readed two times. I don't know why.
It could be a bit faster and shorter: ADC is an _SFR_MEM16() macro too, like at timer registers definitions. |
From LGT8Fx8P v1.0.5 databook:
|
I tried to change the ADC prescaler and read the DAC output which is not buffered (and high impedance). The signal distortion looks exactly the same to me |
Has this discussion reached a conclusion? |
Closing, as #32 got merged. |
Out of curiosity, I did some speed tests with several boards I have (results see attachment).
The code for this numbers is based of parts from https://playground.arduino.cc/Main/ShowInfo/ and the PI calcs from https://forum.arduino.cc/index.php?topic=451743.15 from jurs.
From the sheet you can see a high cycle count for analogRead function(), independent of the CPU frequency, compared to the atmega328p nano (1792 cycles on atmega, 5733 cycles on lgt). Even 32MHz does not compensate this.
Is it because of a slower conversion on silicon, or is there some problem in the libraries?
ardu-speeds.xlsx
Feel free to add/link these sheets/numbers to the readme.md, as the paragraph
"32Mhz is twice as fast as a conventional arduino nano! Actually even faster as many operations take less clock cycles than in the atmega328p."
does not have any numbers.
The text was updated successfully, but these errors were encountered: