STM32F091 ADC resolution 10 bit instead of 12 bit

STM32F103 Nucleo boards e.g. STM Nucleo F103RB
User avatar
Rick Kimball
Posts: 1058
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 1:35 pm

I'm pretty sure you should be using A0 not 0 and A1 not 1, that is how I have been testing. Otherwise it isn't going to do the right thing:

Read A0:

Code: Select all

void setup() {
  Serial.begin(9600);
  analogReadResolution(12);
}

void loop() {
  long sensorValue = analogRead(A0);
  Serial.println(sensorValue);
}
Read A1:

Code: Select all

void setup() {
  Serial.begin(9600);
  analogReadResolution(12);
}

void loop() {
  long sensorValue = analogRead(A1);
  Serial.println(sensorValue);
}
Here is the asm code produced when using 1 instead of A1, it ends up using D1(PA2) instead of D47(PA1):
...
void loop() {
8000264: b510 push {r4, lr}
delay(100);
8000266: 2064 movs r0, #100 ; 0x64
8000268: f000 f8ae bl 80003c8 <delay>
long sensorValue = analogRead(1);
800026c: 2001 movs r0, #1
800026e: f000 f8c1 bl 80003f4 <analogRead>
...

and the correct asm code when using A1:
...
long sensorValue = analogRead(A1);
8000268: 202f movs r0, #47 ; 0x2f
800026a: f000 f8db bl 8000424 <analogRead>
...
-rick

User avatar
Rick Kimball
Posts: 1058
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 2:21 pm

GianniDPC wrote:
Sun Jun 25, 2017 1:26 pm
But when I disconnect the 5V from A0 and connect it to A1 the serial monitor still shows me the same result so 4095. I don't expect this as I defined A0 as my analog pin in the code and not A1.
Also, you shouldn't be connecting 5v to any analog pin. When used as an analog input the max voltage is 3.3v. You are risking frying the analog peripheral.

From the datasheet:
TTa 3.3 V-tolerant I/O directly connected to ADC ..
PA1 is marked TTa
Last edited by Rick Kimball on Sun Jun 25, 2017 2:49 pm, edited 1 time in total.
-rick

User avatar
ahull
Posts: 1659
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by ahull » Sun Jun 25, 2017 2:25 pm

GianniDPC wrote:
Sun Jun 25, 2017 1:26 pm
Rick Kimball wrote:
Sun Jun 25, 2017 12:40 pm
GianniDPC wrote:
Sat Jun 24, 2017 4:31 pm
One more question, I defined the ADC on port A0 in my code, and it works fine when I connect a voltage to A0. But if I plug my wire into A1 for example it also shows me that ADC value even when I didn't tell my code to do that. Is this normal behaviour?
I don't understand the question. It "shows you the ADC value" what does that mean? Can you post the code you are using?
Well the code is the same as in my first post. But what I mean is that I defined A0 as my analog pin in the code and I get my output from the serial monitor as expected, which is 4095 when using a 12-bit resolution.

But when I disconnect the 5V from A0 and connect it to A1 the serial monitor still shows me the same result so 4095. I don't expect this as I defined A0 as my analog pin in the code and not A1.
I'm not sure I follow. If you are looking at pin A1, but your input is on pin A0, then if A1 is not tied high or low, it may well drift up towards the level of the adjacent pin(s).

I would get a couple of 100K linear pits, strap them between 5V and gnd, and point the wipers at the A0 and A1 pins, and slowly rotate the wiper from the gnd end towards the 5V end.

You should see the values on the analog pins follow, up till the reach a maximum when the output of the voltage divider formed by the 100k pot hits around 3.3V .. what happens next, if you move the wiper any further, rather depends on how robust the STM32F103 pins you have chosen are.

If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.
- Andy Hull -

User avatar
Rick Kimball
Posts: 1058
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 2:28 pm

ahull wrote:
Sun Jun 25, 2017 2:25 pm
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.
He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.
-rick

User avatar
ahull
Posts: 1659
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by ahull » Sun Jun 25, 2017 2:38 pm

Rick Kimball wrote:
Sun Jun 25, 2017 2:28 pm
ahull wrote:
Sun Jun 25, 2017 2:25 pm
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.
He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.
In that case, from my brief trawl through the datasheet (Chapter 6 - Electrical Characteristics) I suspect poking the analog pins with 5V is probably a pretty good way to join the magic smoke club. ;)
- Andy Hull -

GianniDPC
Posts: 16
Joined: Thu Jun 22, 2017 10:09 am

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by GianniDPC » Sun Jun 25, 2017 2:52 pm

ahull wrote:
Sun Jun 25, 2017 2:38 pm
Rick Kimball wrote:
Sun Jun 25, 2017 2:28 pm
ahull wrote:
Sun Jun 25, 2017 2:25 pm
If they are 5V tolerant (remember the STM32F103 is a 3.3V device, not a 5V one), then they will stay at the maximum value, and when you rotate the wiper back towards gnd, the value will start to drop again. If they are not 5V tolerant, there will be a lot of heat, perhaps some smoke, and you will earn yourself an honorary membership of our well attended magic smoke club. :twisted:

Better still, check the spec. of pins in the STM32F103 datasheet first to be sure they will suffer such misuse.

EDIT: Thanks Rick, I must admit I was too lazy to check the spec of the analog pins.
He is using an STM32F091RC, the cortex-m0 stuff with the STM Core.
In that case, from my brief trawl through the datasheet (Chapter 6 - Electrical Characteristics) I suspect poking the analog pins with 5V is probably a pretty good way to join the magic smoke club. ;)
Thanks for pointing that out! I'm really stupid doing things before even reading through the data sheet. At least everything still works and I didn't see any smoke haha.

User avatar
Rick Kimball
Posts: 1058
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by Rick Kimball » Sun Jun 25, 2017 3:01 pm

GianniDPC wrote:
Sun Jun 25, 2017 2:52 pm
Thanks for pointing that out! I'm really stupid doing things before even reading through the data sheet. At least everything still works and I didn't see any smoke haha.
That's what people from the 60's said about LSD :) Just wait ...
-rick

User avatar
ahull
Posts: 1659
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by ahull » Sun Jun 25, 2017 4:03 pm

Image

What no magic smoke... I'm almost disappointed. ;)
- Andy Hull -

fpiSTM
Posts: 250
Joined: Fri Sep 16, 2016 12:33 pm
Location: Le Mans, France

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by fpiSTM » Tue Jun 27, 2017 12:20 pm

wow there's lot of smoke here :)

To sum up, stm32 is 3.3V based so provide 5V as input ADC should bring some strange behavior... Mainly for ADC reading.

FYI, on the next release analogRead(0) and analogRead(A0) will be equivalent.
whereas analogWrite(0) is equivalent to analogWrite(D0) .

User avatar
RogerClark
Posts: 7489
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: STM32F091 ADC resolution 10 bit instead of 12 bit

Post by RogerClark » Tue Jun 27, 2017 12:52 pm

On the subject of magic smoke, I am sure I accidentally powered a F103VET board or several hour from 5V and it survived !

I think on the whole the STM32F103 MCU is quite robust

Post Reply