2
\$\begingroup\$

I am currently trying to get the ADC of a STM32F103 running. My first exercise is to read the internal temperature sensor. This is my current approach:

void initADC(){
    RCC->APB2ENR |= (1<<9);      //Enable ADC1 Clock
    RCC->CFGR |= (0b10<<14);     //set ADC Prescaler '6'
    ADC1->CR1 |= (1<<4);         //Input Channel 16
    ADC1->SMPR1 |= (0b100<<18);  //41.5 cycles sample time
    ADC1->CR2 |= (1<<23);        //Enable Temperature Sensor & Vref

    ADC1->CR2 |= (1<<0);         //Enable ADC and start conversion

    ADC1->CR2 |= (1<<3);         //Initialize calibration register
    while(ADC1->CR2 & (1<<3)){   //Wait until calibration register initialized
        ;
    }

    ADC1->CR2 |= (1<<2);         //Enable calibration
    while(ADC1->CR2 & (1<<2)){   //Wait until calibration completed
        ;
    }
}

uint32_t getADCTempValue(){
    ADC1->CR2 |= (1<<0);         //Enable ADC and start conversion
    while(!(ADC1->SR & (1<<1))){ //Wait until end of conversion
        ;
    }
    return ADC1->DR;
}

These are some sample values I get:

2428 2417 2412 2400 2389 2381 2376 2380 2392 2406 2410

But no matter what I do (touching the controller, blowing at the controller) the value doesn't change significantly.

\$\endgroup\$
  • \$\begingroup\$ Does it change more if you blast it with freezer spray? \$\endgroup\$ – Roger Rowland Sep 18 '16 at 9:24
  • \$\begingroup\$ I didn't try that but i moved it under a lamp with a lightbulb (which is quite hot in close distance) and the value didn't change at all. \$\endgroup\$ – binaryBigInt Sep 18 '16 at 9:26
5
\$\begingroup\$

You channel selection is wrong. In the ADC_CR1, you can enable analog watchdog on the selected channels. But it does not enable the channel itself only the watchdog, Page 240.

enter image description here

To select a channel you must use ADC_SQRx or ADC_JSQR registers, depending wether regular or injected group is needed, Page 218.

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.