STM32F103C8T6 Reference Voltage READ

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

STM32F103C8T6 Reference Voltage READ

Post by hyur »

hello,
I want to read the reference voltage value of the STM32F103C8T6 now, but I can't figure out how.

In the case of Arduino UNO, I know that the reference voltage can be read using the code below.

Code: Select all

float getRefVolt() {  
  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
    ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
    ADMUX = _BV(MUX3) | _BV(MUX2);
  #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif  

  delay(2);                           // Wait for Vref to settle
  ADCSRA |= _BV(ADSC);                // Start conversion
  while (bit_is_set(ADCSRA,ADSC));    // measuring

  uint8_t low  = ADCL;                // must read ADCL first - it then locks ADCH  
  uint8_t high = ADCH;                // unlocks both

  long result = (high<<8) | low;
  //result = 1080000L / result; 
  result = 1125300L / result;         // basic Value : Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  
  float rt = result;
  rt=rt/1000.0f;
  return rt;
}
How can I read the reference voltage on my STM32duino?
by ag123 » Thu Jun 02, 2022 4:19 am
google search for RM0008 stm32f103
you should get this
https://www.st.com/resource/en/referenc ... ronics.pdf

look in the chapter for ADC, look under temperature sensor section
in libmaple (roger's core) you may need to use the adc library or rather the adc includes in the core for that.
Go to full post
ag123
Posts: 1797
Joined: Thu Dec 19, 2019 5:30 am
Answers: 28

Re: STM32F103C8T6 Reference Voltage READ

Post by ag123 »

stm32(f103) do not use a 'reference voltage' as like the Atmega.
the top of the range is 3.3v VDD, bottom is 0v GND

if you want to read the internal AVREF in stm32f103 you could try using these
https://github.com/stm32duino/wiki/wiki ... l-channels

look up the specs sheet for what that is, it isn't the same as that used by Atmega mcu.
https://www.st.com/en/microcontrollers- ... 103c8.html
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: STM32F103C8T6 Reference Voltage READ

Post by hyur »

ag123 wrote: Tue May 31, 2022 7:52 am look up the specs sheet for what that is, it isn't the same as that used by Atmega mcu.
https://www.st.com/en/microcontrollers- ... 103c8.html
Thanks for your interest in my question.

Is it correct to refer to the Internal_channels.ino source?
ag123
Posts: 1797
Joined: Thu Dec 19, 2019 5:30 am
Answers: 28

Re: STM32F103C8T6 Reference Voltage READ

Post by ag123 »

that should work for 'official' core, e.g.

Code: Select all

uint16_t value = analogRead(AVREF);
on datasheet, section 5.3.4 Embedded reference voltage
https://www.st.com/resource/en/datashee ... f103c8.pdf
that is the adc value you get for the internal voltage 1.2v
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: STM32F103C8T6 Reference Voltage READ

Post by hyur »

ag123 wrote: Tue May 31, 2022 8:14 am that should work for 'official' core

Code: Select all

debug : 'AVREF' was not declared in this scope
It doesn't work in my source.
It doesn't know what the AVREF variable is.
ag123
Posts: 1797
Joined: Thu Dec 19, 2019 5:30 am
Answers: 28

Re: STM32F103C8T6 Reference Voltage READ

Post by ag123 »

check the core you used. it may help to have #include <Arduino.h> in there, normally not needed.
GonzoG
Posts: 460
Joined: Wed Jan 15, 2020 11:30 am
Answers: 32
Location: Prudnik, Poland

Re: STM32F103C8T6 Reference Voltage READ

Post by GonzoG »

hyur wrote: Tue May 31, 2022 8:23 am
ag123 wrote: Tue May 31, 2022 8:14 am that should work for 'official' core

Code: Select all

debug : 'AVREF' was not declared in this scope
It doesn't work in my source.
It doesn't know what the AVREF variable is.
Than it means that you're using Roger's core. It works only on official STM32 core.
On Roger's core you'll probably need to setup ADC using registers.
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: STM32F103C8T6 Reference Voltage READ

Post by hyur »

ag123 wrote: Tue May 31, 2022 8:42 am check the core you used. it may help to have #include <Arduino.h> in there, normally not needed.
I included the Arduino.h but it didn't solve the problem.
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: STM32F103C8T6 Reference Voltage READ

Post by hyur »

GonzoG wrote: Tue May 31, 2022 1:44 pm Than it means that you're using Roger's core. It works only on official STM32 core.
On Roger's core you'll probably need to setup ADC using registers.
Where can I get datasheets for ADC registers?
ag123
Posts: 1797
Joined: Thu Dec 19, 2019 5:30 am
Answers: 28

Re: STM32F103C8T6 Reference Voltage READ

Post by ag123 »

google search for RM0008 stm32f103
you should get this
https://www.st.com/resource/en/referenc ... ronics.pdf

look in the chapter for ADC, look under temperature sensor section
in libmaple (roger's core) you may need to use the adc library or rather the adc includes in the core for that.
Post Reply

Return to “General discussion”