Page 1 of 1

How can I change the ADC Resolution?

Posted: Tue Jun 28, 2022 12:09 am
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

Re: How can I change the ADC Resolution?

Posted: Tue Jun 28, 2022 12:40 am
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.

Re: How can I change the ADC Resolution?

Posted: Tue Jun 28, 2022 1:04 am
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?

Re: How can I change the ADC Resolution?

Posted: Tue Jun 28, 2022 4:10 pm
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.

Re: How can I change the ADC Resolution?

Posted: Tue Jun 28, 2022 11:19 pm
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

Re: How can I change the ADC Resolution?

Posted: Wed Jun 29, 2022 3:57 pm
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)

Re: How can I change the ADC Resolution?

Posted: Mon Jul 04, 2022 2:13 am
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

Re: How can I change the ADC Resolution?

Posted: Mon Jul 04, 2022 11:24 am
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.