2024-12-01 09:38 AM - edited 2024-12-01 09:39 AM
I am having an STM32F411E board and I am trying to read values of MAX4466 analog mic using the onboard ADC interface(12 bit resolution). I get values starting from around 1000(0.8 V) and whenever I make sounds it slightly increases and that's it. I've checked with my logic analyzer and whenever I make sounds the output momentarily goes up and down until it goes off as expected (I know that the results of a logic analyzer are not relevant for an analog signal yet I just wanted to know if the mic reacts at all). By the way I'm using my own GPIO driver which is tested and has no issues. So what might be the problem?
#include "STM32F411xx.h"
void ADC_conversion(void);
void ADC_init(void);
float ADC_to_Voltage(uint16_t ADC_value, float Vref);
__vo uint16_t ADC_read(void);
__vo uint16_t ADC_data;
float voltage;
extern void initialise_monitor_handles(void);
int main()
{
ADC_init();
while(1)
{
ADC_conversion();
ADC_data = ADC_read();
voltage = ADC_to_Voltage(ADC_data, 3); // Vref = 3V
initialise_monitor_handles();
printf("The voltage level is %0.2f V\n", voltage);
}
return 0;
}
void ADC_init(void)
{
GPIO_Handle_t ADC_ch1;
ADC_ch1.pGPIOx = GPIOC;
ADC_ch1.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_ANALOG;
ADC_ch1.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_PIN_PD;
ADC_ch1.GPIO_PinConfig.GPIO_PinSpeed = GPIO_SPEED_FAST;
ADC_ch1.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_5;
GPIO_Init(&ADC_ch1);
RCC->APB2ENR |= (1 << 8); //Enables the clock of the bus that ADC1 is hanging on
ADC1->CR1 &= ~(0x3 << 24); //Sets 12 bit ADC resolution (15 clock cycles)
ADC1->CR2 &= ~(1 << 0); //AD converter is off
ADC1->SQR3 |= (0x0F << 0); //15th ADC1 channel is enabled
ADC1->CR2 |= (1 << 0); //AD converter is on
}
void ADC_conversion(void)
{
ADC1->SMPR2 |= (1 << 0); //Sets channel sample rate to 15 clock cycles per sample
ADC1->CR2 |= (1 << 30); //Starts regular channel conversion
}
__vo uint16_t ADC_read(void)
{
while(!(ADC1->SR & (1 << 1)));
return (ADC1->DR);
}
float ADC_to_Voltage(uint16_t ADC_value, float Vref) {
return (ADC_value / 4095.0) * Vref;
}