cancel
Showing results for 
Search instead for 
Did you mean: 

Adc in stm32f446re

Alex_reynold
Associate III

Analog source is potentiometer

Mcu board is nucleo f 446re

I am verifying adc code in debug mode with above hardware .as i rotate potentiometer  change in adc value can be observed.but when I don't rotate also, I am able to see the variation in 12bit value with variation about +-50d(max value)but this is not the desired operation.i need stable digital domain value for corresponding analog input how to verify it?à

 

10 REPLIES 10
TDK
Guru

Adding a capacitor on the potentiometer tap can help. As can choosing a potentiometer with lower impedance, say 10K or 1K, although that will eat up more current than one with higher impedance.

Using an encoder wheel along with a timer in encoder mode instead of a potentiometer will eliminate the jitter.

ADC values are expected to have some amount of jitter in most cases, although you can probably do better than 50 counts. Increasing sample time to max can provide improved accuracy. General tips for improving ADC accuracy are here:

https://www.st.com/resource/en/application_note/an4073-how-to-improve-adc-accuracy-when-using-stm32f2xx-and-stm32f4xx-microcontrollers-stmicroelectronics.pdf

https://www.st.com/resource/en/application_note/an2834-how-to-optimize-the-adc-accuracy-in-the-stm32-mcus-stmicroelectronics.pdf

 

If you feel a post has answered your question, please click "Accept as Solution".

Hi , I think this docx is very helpfull.

Is there any project which gives more handon on this.

Alex_reynold
Associate III

And while running the code in register level programming swstart bit of status register is not enabling to logic 1 and program is not giving output.

But when I creat the same using cube MX I am able to get the output.how to fix it 

Alex_reynold
Associate III

Sorry for the multiple queries in one thread.

My analog signal is giving signal in audio frequency band .please suggest  about configuration  such speed , resolution and cycle and mode

Compare the MX code with your register level code for missing register initializations.

 

No No,If run cube MX code in debug mode it is throwing error

TDK
Guru

> Is there any project which gives more handon on this.

There are many ADC examples available in the CubeMX repository. Use the Example Selector to find them.

The original question has been answered. If you have another question, post a thread with relevant information, including your code, if that's the problem. Can't debug code we can't see.

If you feel a post has answered your question, please click "Accept as Solution".

This is my  adc.c

#include "adc.h"

 

void ADC1_Init(void)

{

//Enable clock to ADC module

RCC->APB2ENR|=ADC1EN;

//Enable clock to GPIO a

RCC->AHB1ENR|=GPIOAEN;

// configure mode to analog

GPIOA->MODER|=(0x03<<2U);

//configure ADC parameter

//1. Sequence start

//2. Sequence length

ADC1->SQR3=ADC_CH1;

// Sequence length to 0

ADC1->SQR1=0x00;

//Enable adc

ADC1->CR2|=ADC1_ON;

}

void Start_Conv(void)

{

ADC1->CR2|=ADC_CR2_SWSTART;

}

uint32_t ADC_read(void)

{

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

return (ADC1->DR);

}

uint32_t data;

int main(void)

{

 

 

 

         ADC1_Init();

 

for(int i=0;i<4;i++)

{

Start_Conv();

data=ADC_read();

}

}
main.c file

 

I've tried your code (in attachment), and it works as expected.

Comments:

- you should have started a new thread with the new question

- when inserting code, in the forum's editor, click on "..." to expand the top menu, and in there click to "</>" to insert code, otherwise indentations etc. are not preserved and code is hard to read

- I have added definitions which you probably have in adc.h. Most of them redefine already existing symbols from the CMSIS-mandated device header; there's no reason to do that, just use the symbols from the device header directly

- I have added volatile to data (see below)

- I have added an infinite loop (while(1)) at the end of main(), as is mandatory for freestanding code (i.e. if you don't have an operating system as in PC) to not exit/return from main()

- swstart bit of status register is not enabling to logic 1- do you mean ADC_CR2.SWSTART? That bit is cleared by hardware as soon as the conversion starts, which in your case is immediately, so in debugger you won't see this bit read out as 1

- program is not giving output How exactly do you observe that? Do you read content of data variable in debugger? As the data variable is not used anywhere, an optimizing compiler may chose to optimize out writes to it entirely. Tag it as volatile.

- you haven't set sampling time, the default is the shortest one. It is OK for a test just find out that ADC works, but it may also result in the highest error of conversion, unless your input circuitry is properly designed for low-impedance

JW