analogReference() and Vref for LibMaple Core for voltage measurement

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
i998
Posts: 9
Joined: Wed Feb 23, 2022 2:14 am

analogReference() and Vref for LibMaple Core for voltage measurement

Post by i998 »

Hi,
Do you know if STM23F1 (Maple Mini) has an internal source for voltage reference - like Arduino Pro Micro:

Code: Select all

// an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P
//and 2.56 volts on the ATmega32U4 and ATmega8 (not available on the Arduino Mega)

analogReference(INTERNAL);  
which is stable and not depend on VCC ?


UPDATE: There is VREFINT at ADC17. This link https://www.eevblog.com/forum/microcont ... msg1910771 has a code for stm32duino that allows accurately measure VDD, then you can use accurate VDD value as a reference
to measure any other ADC input by analogRead (x) as usual.

Could someone help with porting the code to LibMaple core please?

Thanks in advance, i998
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: analogReference() and Vref for LibMaple Core for voltage measurement

Post by GonzoG »

I think that the example you linked is for libMaple / Roger's core. In 2018 "stm32duino" name referred to Roger's core.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: analogReference() and Vref for LibMaple Core for voltage measurement

Post by ag123 »

Code: Select all

void setup() {

	pinMode(LED_BUILTIN, OUTPUT);

	Serial.begin();
	
	// vrefint
	adc_reg_map *regs = ADC1->regs;
	regs->CR2 |= ADC_CR2_TSVREFE;    // enable VREFINT and temp sensor
	regs->SMPR1 = ADC_SMPR1_SMP17;  // sample rate for VREFINT ADC channel
}

void loop() {

		uint16_t vrefint = adc_read(ADC1, 17);
		Serial.print(F("Vref int (1.2v):"));
		Serial.print(vrefint);
		Serial.println();

		uint16_t vtemp = adc_read(ADC1, 16);
		Serial.print(F("temp sensor:"));
		Serial.print(vtemp);
		Serial.println();

		uint16_t mv = (1200 * vtemp) / vrefint;
		Serial.print(F("mvolt:"));
		Serial.print(mv);
		Serial.println();

		// specs 5.3.19 temp sensor characteristics
		// V 25 deg ~ 1.43v
		// slope 4.3 mv/C
		float temp = (mv - 1430) * 1.0 / 4.3 + 25.0;
		Serial.print(F("temp:"));
		Serial.print(temp);
		Serial.println();

		delay(1000);
		digitalWrite(LED_BUILTIN, ! digitalRead(LED_BUILTIN));
		Serial.println("Hello world from maple mini");
}
hardly very arduinoish after all ;)
for clues to the code, you would need to review the specsheet and ref manual rm0008
and libmaple codes
i998
Posts: 9
Joined: Wed Feb 23, 2022 2:14 am

Re: analogReference() and Vref for LibMaple Core for voltage measurement

Post by i998 »

Hi ag123,
thank you for the reply! Your code compiled fine, thanks a lot. I'll test the actual voltage measurement asap and place the results here.

Question - what do do with a calibration procedure in the original code?
adc_calibrate(ADC1);


RE "example you linked is for libMaple / Roger's core":
I tried to compile the original code with Roger's core and unfortunately it did not compile with as follows:

Code: Select all

C:\Users\xxx\Documents\Arduino\STM32_Vref_test_v01\STM32_Vref_test_v01.ino: In function 'void setup_vdd_tempr_sensor()':
STM32_Vref_test_v01:13:18: error: 'ADC_CR2_TSEREFE' was not declared in this scope
     regs->CR2 |= ADC_CR2_TSEREFE;    // enable VREFINT and Temperature sensor
                  ^
exit status 1
'ADC_CR2_TSEREFE' was not declared in this scope
Kind regards,
i998
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: analogReference() and Vref for LibMaple Core for voltage measurement

Post by ag123 »

Actually, I hardly calibrate the adc maybe as I'm lazy to do it and hence, don't know about it.
the ref manual rm0008 is a place to start looking up about it, the chapter about ADC etc.

those register patches simply configure the registers to enable VREFINT and the temperature sensor.
and setting the sample rates.

if you read the spec sheets VREFINT is 1.2v - check the specs, that is a voltage reference. So i'd just take it as fixed.

if you make an assumption it is proportional, so if you read the adc e.g. with v = analogRead(pin), that works as well,
then v is the counts that represent 1.2v
i.e. each count is 1.2 / v
and you can then just measure another voltage say x = analogRead(another_pin);
then the voltage at another_pin is 1.2 * x / v

when I'm lazy, I didn't even bother with the voltage reference and simply assume a count of 4096 (i.e. 12 bits) is 3.3v (VDD),
so the voltage on any pin is simply
3.3 x analogRead(pin) / 4096
This is less accurate than the voltage reference method. It works ok if say I simply want to observe the waveform and is not bothered about the absolute voltages.
i998
Posts: 9
Joined: Wed Feb 23, 2022 2:14 am

Re: analogReference() and Vref for LibMaple Core for voltage measurement

Post by i998 »

Hi ag123,

Checked the code - all works and measured voltage and temp fine - thank you!

That helped with the original snippet - it has a typo - ADC_CR2_TSEREFE instead of ADC_CR2_TSVREFE

Once fixed, the snippet works too.

Regards, i998
Post Reply

Return to “General discussion”