cancel
Showing results for 
Search instead for 
Did you mean: 

ADC Problems

Nicholas Luke
Associate
Posted on November 22, 2017 at 19:45

hello,

I'm having a little trouble with a project I'm working on.

I'm trying to make what is essentially a thermostat using an ADC converter and an MCP9701-e/to-nd temperature sensor and an STM32401RE nucleo MCU

My plan is to use the adc to read the temperature sensor which is connected to pin A0 and have it print out the temperature. However, the program only just shows 0 degrees.

Here's what I have:

void initADC(){

RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;

ADC1->CR1 = 0;

ADC1->CR2 |= ADC_CR2_ADON;

ADC1->SMPR2 &=~ ADC_SMPR2_SMP0;

ADC1->SMPR2 |= ADC_SMPR2_SMP0_2;

ADC1->SQR1 &=~ ADC_SQR1_L;

ADC1->SQR1;

ADC1->SQR3 &=~ ADC_SQR3_SQ1;

ADC1->SQR3;

ADC->CCR |= ADC_CCR_ADCPRE_0;

}

void initGPIO(){

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

GPIOA->MODER |= (GPIO_MODER_MODER0_1)|(GPIO_MODER_MODER0_0);

}

int main() {

initADC();

initGPIO();

//GPIOA->ODR |= GPIO_ODR_ODR_3;

int32_t temperature;

ADC1->CR2 |= ADC_CR2_SWSTART;

while(1){

while(!(ADC1->SR & ADC_SR_EOC)){;}

temperature = ADC1->DR;

temperature *= 3300;

temperature -= 400;

temperature = temperature/19.5;

temperature = temperature * 1.8 + 32;

printf('temperature = %.0f Degree F\n', temperature);

ADC1->CR2 |= ADC_CR2_SWSTART;

for (int32_t i = 0; i < 10E6; i++){;}

}

}

I'm not planning on doing anything overly complicated with the ADC. I'm not using any overrun, DMA, Injected data channels, Watchdog, or battery charge monitoring. I just need to know if my configuration for my ADC is supposedly good enough or if i need to reconfigure it.

Here's the datasheet for the temperature sensor:

http://ww1.microchip.com/downloads/en/DeviceDoc/20001942G.pdf

 

if anyone can please help me that w&sharpould be appreciated.

#temperature-sensor #stm32 #enable-adc #stm32-nucleo-f401re #adc
2 REPLIES 2
AvaTar
Lead
Posted on November 23, 2017 at 09:21

Don't have the time at the moment to go through the ref manual to check your register settings.

But the GPIO settings are usually done before the ADC settings, and GPIO mode is AN (GPIO_Mode_AN).

while(!(ADC1->SR & ADC_SR_EOC)){;}

It's style, of course, but you don't need a semicolon inside the empty braces.

while (!(ADC1->SR & ADC_SR_EOC)) {}

would suffice, or even:

while (!(ADC1->SR & ADC_SR_EOC)) ;

Most often, so-called coding guidelines enforce empty compound statements ...

Nicholas Luke
Associate
Posted on November 23, 2017 at 16:39

Ok thank you,

Any help is appreciated.