AnsweredAssumed Answered

ADC Problems

Question asked by Nicholas Luke on Nov 22, 2017
Latest reply on Nov 23, 2017 by Nicholas Luke

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#ould be appreciated.

Outcomes