
Vout Vin ────── = ────────────── Rref Rref + Rmeas
The voltage divider relationship now becomes
VDDA * ADCraw / 4095 = VDDA * Rref / (Rref + Rmeasured)
which can be further simplified as
ADCraw * Rmeasured = Rref * (4095 - ADCraw)
The resistor value is then given by
Rmeasured = Rref * (4095 - ADCraw) / ADCraw
or
Rmeasured = Rref * (4095 / ADCraw - 1)
#include <limits.h> int R = ADCraw ? Rref * (4095 / ADCraw - 1) : INT_MAX ;
int R = ADCraw ? Rref * 4095 / ADCraw - Rref : INT_MAX ;
#include <limits.h>
#include <stdio.h>
#include "system.h" /* uptime, yield(), adc_init(), adc_convert() */
#define RREF 10010 /* Rref is 10kOhm, measured @ 10.01 kOhm */
int main( void) {
unsigned last = 0 ;
const unsigned short *calp ; /* TS_CAL, VREFINT_CAL */
/* Initialize ADC and fetch calibration values */
calp = adc_init( 2 | (1 << 17)) ; /* ADC read on GPIOA1 and VREF */
short Vcal = calp[ 1] ; /* VREFINT_CAL */
printf( "factory calibration: %u, %u, %u\n", calp[ 1], calp[ 0], calp[5]) ;
for( ;;)
if( uptime == last)
yield() ;
else {
short Rsample, Vsample ;
last = uptime ;
Vsample = adc_convert() ;
Rsample = adc_convert() ;
printf( "%i, %i, %i, ", Vcal, Vsample, Rsample) ;
int res = Rsample ? RREF * 4095 / Rsample - RREF : INT_MAX ;
Vsample = 3300 * Vcal / Vsample ;
printf( "%i, %i.%i\n", res, Vsample / 1000, Vsample % 1000) ;
}
}
I add the composition in Makefile
SRCS = startup.crc.c adc.c adcext.c
Building up on the previous ADC reading of internal sensors, sampling of an external resistor pair connected to one of the IO pin is straightforward.
Next, I will show the temperature readings on a display.