How can I change the ADC Resolution?

Post here first, or if you can't find a relevant section!
Post Reply
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

How can I change the ADC Resolution?

Post by hyur »

I am using an STM32duino(STM32F103C8T6) now.

I am trying to use analogWrite, but only 8 bits (0~255) can be used.
I know that this resolution is changeable, but how can I change it?

I want to use the analogwriteresolution() function, but I get the following error:
'analogWriteResolution' was not declared in this scope
by GonzoG » Tue Jun 28, 2022 4:10 pm
1. ADC is Analog to Digital Converter. It reads analog data and converts it to digital - analogRead();
DAC is Digital to Analog, but F103C8 does not have DAC.
Using analogWrite() on MCUs without DAC you setup PWM output.

2. If analogWriteResolution() does not work (compile), it means that you use Roger's core. It doesn't have option for setting PWM resolution.
I suggest switching to official STM32 core.
Go to full post
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: How can I change the ADC Resolution?

Post by dannyf »

for lower resolution:
1. right shift;
2. pick the right alignment;
3. on some chips, you can set the resolution. not on this one.

for higher resolution:
1. oversampling;
2. use an external adc.
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: How can I change the ADC Resolution?

Post by hyur »

for lower resolution:
1. right shift;
2. pick the right alignment;
3. on some chips, you can set the resolution. not on this one.

for higher resolution:
1. oversampling;
2. use an external adc.
I'm sorry I didn't understand you well.

According to for lower resolution: 3., does that mean that the resolution cannot be changed with only the STM I use?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: How can I change the ADC Resolution?

Post by GonzoG »

1. ADC is Analog to Digital Converter. It reads analog data and converts it to digital - analogRead();
DAC is Digital to Analog, but F103C8 does not have DAC.
Using analogWrite() on MCUs without DAC you setup PWM output.

2. If analogWriteResolution() does not work (compile), it means that you use Roger's core. It doesn't have option for setting PWM resolution.
I suggest switching to official STM32 core.
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: How can I change the ADC Resolution?

Post by hyur »

1. ADC is Analog to Digital Converter. It reads analog data and converts it to digital - analogRead();
DAC is Digital to Analog, but F103C8 does not have DAC.
Using analogWrite() on MCUs without DAC you setup PWM output.

2. If analogWriteResolution() does not work (compile), it means that you use Roger's core. It doesn't have option for setting PWM resolution.
I suggest switching to official STM32 core.
Thanks to your kind explanation I understood this problem.

Thank you
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: How can I change the ADC Resolution?

Post by GonzoG »

@hyur
Oh... and I forgot the resolution issue.

If you need analog output (PWM or DAC) you don't need to worry about resolution. It's easy to calculate.
From 8b (0-255) to 10/12b (0-1023/4095): just multiply 8b value by 2 (to 10b) or 4 (12b), or shit value by 2 / 4 bits left ( value<<2, value <<4 )
From 10b / 12b to 8b: divide value by 4 / 2, or shift 4 / 2 bits right (value>>4, value>>2)
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: How can I change the ADC Resolution?

Post by hyur »

GonzoG wrote: Wed Jun 29, 2022 3:57 pm @hyur
Oh... and I forgot the resolution issue.

If you need analog output (PWM or DAC) you don't need to worry about resolution. It's easy to calculate.
From 8b (0-255) to 10/12b (0-1023/4095): just multiply 8b value by 2 (to 10b) or 4 (12b), or shit value by 2 / 4 bits left ( value<<2, value <<4 )
From 10b / 12b to 8b: divide value by 4 / 2, or shift 4 / 2 bits right (value>>4, value>>2)
Thank you @GonzoG
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: How can I change the ADC Resolution?

Post by dannyf »

if you are looking to change (pwm) DAC resolution, a few ways:

to reduce resolution:
1. change the top end of pwm - the simplest;
2. to limit the resolution of the pwm stepping: for example, to use a 16-bit pwm as 8-bit, you simply left-shift the input duty cycle;

to increase resolution:
1. increase the top end of pwm - sometimes this may not be possible;
2. use dither;
3. use external dac.
...

dithering is an interesting approach if you ask me.
Post Reply

Return to “General discussion”