Skip to content
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

[STM32F1/STM32F4] ADC speed is very slow #1352

Closed
rtek1000 opened this issue Apr 5, 2021 · 1 comment
Closed

[STM32F1/STM32F4] ADC speed is very slow #1352

rtek1000 opened this issue Apr 5, 2021 · 1 comment
Labels
answered duplicate This issue or pull request already exists question ❓ Usually converted as a discussion

Comments

@rtek1000
Copy link

rtek1000 commented Apr 5, 2021

analogRead:

STM32F103C8T6:.......63.950us
STM32F407VG:...........53.788us
Arduino DUE:.................4.361us
Arduino Mega2560:....112.008us

code:

uint32_t adc0 = 0;
uint32_t adc1 = 0;

uint32_t micros0 = 0;
uint32_t micros1 = 0;

void setup() {
  // put your setup code here, to run once:
  delay(250);
  Serial.begin(115200);
  Serial.println("Start");

  pinMode(13, OUTPUT);
  //pinMode(PC13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, !digitalRead(13));
  //digitalWrite(PC13, !digitalRead(PC13));

  micros0 = micros();

  for (uint32_t cnt1 = 0; cnt1 < 1000; cnt1++) {
    adc0 = analogRead(A0);
    //adc0 = analogRead(PA0);
  }

  micros1 = micros();

  Serial.print(micros1 - micros0, DEC);
  Serial.println("us");

  delay(500);
}

Why is ADC so slow?

Another complaint about this:
[analogRead slow with ST libraries]
https://www.stm32duino.com/viewtopic.php?f=7&t=967&p=6393&hilit=adc+dma#p6393

Using the ST board definitions, t takes about 5645 microseconds to do the 100 conversions, so 56.45 uSec per conversion (including the loop instructions but those only take about 1 uSec, I checked). Using the stm32duino board definitions, it only takes 698 microseconds... almost 10 times faster!

@rtek1000 rtek1000 changed the title [STM32F103C8T6] ADC speed is very slow [STM32F1/STM32F4] ADC speed is very slow Apr 5, 2021
@fpistm
Copy link
Member

fpistm commented Apr 5, 2021

Hi @rtek1000

Why is ADC so slow?

Simply because when you used analogRead it initialize the ADC, made a single ended conversion then deinit.
As stated it was several time asked and always the same answer. There is already an issue for ADC #5.
As mentioned in the forum you can use directly the HAL ADC module and an example for F1 is available.
https://www.stm32duino.com/viewtopic.php?f=41&t=110
I close this issue as it is not a bug and there is already an issue related to ADC.

@fpistm fpistm closed this as completed Apr 5, 2021
@fpistm fpistm added answered duplicate This issue or pull request already exists question ❓ Usually converted as a discussion labels Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered duplicate This issue or pull request already exists question ❓ Usually converted as a discussion
Projects
None yet
Development

No branches or pull requests

2 participants