2024-06-16 01:52 PM
I've been stuck trying setting up an ADC pin on an STM32WLE5C8U6 MCU for several days. Although I'm new to the ST environment, I have experience setting up ADC pins on other F series chips without any issues. Am I possibly overlooking something? According to the documentation, it appears that setting up the ADC should be even simpler on the STM32WLE5. Any guidance would be greatly appreciated.
#include "stm32wle5xx.h"
void ADC_Init(void) {
// Enable GPIOB clock
RCC->AHB2ENR |= (1U<<1);
// Enable ADC clock
RCC->APB2ENR |= (1U<<9);
// Breakpoint here to check if ADC clock is enabled
// Configure PB4 as an analog input
GPIOB->MODER &= ~(3U << (2 * 4)); // Clear mode for PB4
GPIOB->MODER |= (3U << (2 * 4)); // Set PB4 to analog mode
// Ensure ADC is disabled before starting configuration
if (ADC->CR & (1U<<0)) {
ADC->CR |= (1U<<1);
while (ADC->CR & (1U<<1));
}
// Breakpoint here to check if ADC is disabled
// Enable the ADC voltage regulator
ADC->CR |= (1U<<28);
for (volatile uint32_t i = 0; i < 10000; i++); // Delay for voltage regulator
// Breakpoint here to check if voltage regulator is enabled
// Calibrate the ADC
//ADC->CR |= (1U<<31); // Start calibration
//while (ADC->CR & (1U<<31)); // Wait for calibration to complete
//execution gets stuck here. If I skip calibration, execution gets stuck in the next while()
// Breakpoint here to check if calibration is complete
// Configure the ADC
ADC->CFGR1 |= (1U<<3); // 10-bit resolution
ADC->CHSELR = (1U<<4); // Select channel for PB4
// Enable the ADC
ADC->CR |= (1U<<0); // Enable ADC
while (!(ADC->ISR & (1U<<0))); // Wait until ADC is ready
// Breakpoint here to check if ADC is ready
}
uint16_t ADC_Read(void) {
ADC->CR |= ADC_CR_ADEN;
ADC->CR |= ADC_CR_ADSTART; // Start conversion
while (!(ADC->ISR & ADC_ISR_EOC)); // Wait for conversion to complete
return ADC->DR; // Read the ADC value
}
uint16_t adcValue = 0;
int main(void) {
ADC_Init();
adcValue = ADC_Read();
// Use adc_value as needed
while (1);
}
2024-06-28 08:44 AM
Hello @Nando
I suggest you follow the configurations of this examples to implement an ADC configuration on your STM32WL MCU.
Best Regards.
STTwo-32
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.