Skip to main content
Maciej Skrzypczak
Associate II
February 26, 2018
Question

ADC fluctuations problem

  • February 26, 2018
  • 2 replies
  • 2229 views
Posted on February 26, 2018 at 14:50

Hi,

I am having a problem with ADC read. The values I get from conversion are 100% random. They get from 0 to max range without any pattern. Example codes from MXCube work fine and the value is stable. My own code generated in cube is not fixing the issue. I tried to compare the example and my own code, but didnt find any difference besides CPUcache beeing enabled in example (that I ignored). Every other function in ADC works for me. Can anyone please tell me what am I doing wrong? Thanks in advance!

I am using KeiluVision 5.0 with STM32F767ZI on nucleo board. Here is my code 

.

int main()

{

   ADC_init();

   ADC_port_init();

}

.

void ADC_port_init(void)

{

   /////////// AHB for GPIOA

   RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

.

   /////////// Pin A1 analog

   GPIOA -> MODER |= GPIO_MODER_MODER1_0 | GPIO_MODER_MODER1_1;

.

   /////////// Pin A1 very high speed

   GPIOA -> OSPEEDR |= GPIO_OSPEEDER_OSPEEDR1_0 | GPIO_OSPEEDER_OSPEEDR1_1;

}

.

void ADC_init (void)

{

   /////////// Conwerter ON

   ADC1 -> CR2 |= ADC_CR2_ADON;

.

   /////////// APB for ADC init

   RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;

.

   /////////// Interrupt when EOC

   ADC1 -> CR1 |= ADC_CR1_EOCIE;

.

   /////////// 12 bit resolution (11)

   ADC1 -> CR1 &= ~ADC_CR1_RES_0 & ~ADC_CR1_RES_0;

.

   /////////// Single conversion

   ADC1 -> CR2 &= ~ADC_CR2_CONT;

.

   /////////// EOC after regular group

   ADC1 ->CR2 &= ~ADC_CR2_EOCS;

.

   /////////// Sample time 480 cycles (010)

   ADC1 ->SMPR2 |= ADC_SMPR2_SMP1_0 | ADC_SMPR2_SMP1_1 | ADC_SMPR2_SMP1_2;

.

   /////////// 1 measurement in regular group (0000)

   ADC1 -> SQR1 &= ~ADC_SQR1_L_0 & ~ADC_SQR1_L_1 & ~ADC_SQR1_L_2 & ~ADC_SQR1_L_3;

.

   /////////// First measurement on PA1 (0001)

   ADC1 -> SQR3 &= ~ADC_SQR3_SQ1_0 | ADC_SQR3_SQ1_1 & ~ADC_SQR3_SQ1_2 & ~ADC_SQR3_SQ1_3;

.

   /////////// Start regular conversion

   ADC1 -> CR2 |= ADC_CR2_SWSTART;

}

#problem #adc #fluctuations #stm32f767zi
This topic has been closed for replies.

2 replies

AvaTar
Senior III
February 26, 2018
Posted on February 26, 2018 at 15:15

I don't cube.

But what seems to stick out:

   /////////// Conwerter ON

   ADC1 -> CR2 |= ADC_CR2_ADON;

   /////////// APB for ADC init

   RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;

Always enable the peripheral first (RCC) before you write to any of it's registers.

Maciej Skrzypczak
Associate II
February 26, 2018
Posted on February 26, 2018 at 17:50

Thank You for Your reply. I realised now that the code contains some mistakes(not too big). Your solution ended up changing the behavior of ADC, but not solved that. Now instead of getting a value in full range of 12 bits I get it in first 25% that is from 0 to about 1000. I would like to ask You if there is any register for the ADC control that I should include. I saw that in older versions of the device people calibrated their ADCs but i didnt find that is datasheet.

AvaTar
Senior III
February 26, 2018
Posted on February 26, 2018 at 19:08

Initialization of peripherals is usually a onetime thing - efficiency is not real important.

So I used to take an example (SPL at this times) copied and modified it.

You could try a Cube or LL example, see if it works, and copy the init code.

Now instead of getting a value in full range of 12 bits I get it in first 25% that is from 0 to about 1000.

This sounds like a different problem.

As JW commented to your

ADC1 -> SQR3 &= ~ADC_SQR3_SQ1_0 | ADC_SQR3_SQ1_1 & ~ADC_SQR3_SQ1_2 & ~ADC_SQR3_SQ1_3;

line:

Have you checked that it actually does what you think it should ?

waclawek.jan
Super User
February 26, 2018
Posted on February 26, 2018 at 16:12

ADC1 -> SQR3 &= ~ADC_SQR3_SQ1_0 | ADC_SQR3_SQ1_1 & ~ADC_SQR3_SQ1_2 & ~ADC_SQR3_SQ1_3;

What?

JW

Maciej Skrzypczak
Associate II
February 26, 2018
Posted on February 26, 2018 at 17:51

Ye, that was bad... After changing the code ADC still not operating well. Have You got any ideas?

Andrew Neil
Super User
February 26, 2018
Posted on February 26, 2018 at 18:18

Maciej Skrzypczak wrote:

ADC still not operating well. 

What, exactly, does that mean?

Be specific.

Give numbers.

Also, have you looked at the ADC input on an oscilloscope? If it's noisy, then you will obviously get noisy results!

You need to use 

an oscilloscope - a meter reading is useless here.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.