2023-11-24 07:14 AM
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?à
2023-11-24 08:12 AM
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:
2023-11-24 08:49 AM
Hi , I think this docx is very helpfull.
Is there any project which gives more handon on this.
2023-11-24 08:51 AM
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
2023-11-24 08:54 AM
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
2023-11-24 10:04 AM
Compare the MX code with your register level code for missing register initializations.
2023-11-24 10:27 AM
No No,If run cube MX code in debug mode it is throwing error
2023-11-24 10:35 AM
> 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.
2023-11-24 08:15 PM
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
2023-11-25 12:48 AM - edited 2023-11-25 12:49 AM
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