From 4098e6e6afbd698642ba1b516fa108a411ca30c7 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 9 Feb 2021 15:36:03 +0800 Subject: [PATCH] ADC calibration application to experiment with the internal temperature sensor using a DS18B20 as reference. --- Makefile | 3 ++- adccalib.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 adccalib.c diff --git a/Makefile b/Makefile index 895a054..6dfb331 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,8 @@ PROJECT = f030f4 #SRCS = startup.txeie.c txeie.c uptime.c #SRCS = startup.txeie.c gpioa.c dht11main.c dht11.c #SRCS = startup.txeie.c gpioa.c ds18b20main.c ds18b20.c -SRCS = startup.txeie.c adc.c adcmain.c +#SRCS = startup.txeie.c adc.c adcmain.c +SRCS = startup.txeie.c adc.c adccalib.c ds18b20.c OBJS = $(SRCS:.c=.o) LIBOBJS = printf.o putchar.o puts.o CPU = -mthumb -mcpu=cortex-m0 diff --git a/adccalib.c b/adccalib.c new file mode 100644 index 0000000..60afcf8 --- /dev/null +++ b/adccalib.c @@ -0,0 +1,77 @@ +/* adccalib.c -- ADC calibration of internal temperature sensor */ +/* Copyright (c) 2020-2021 Renaud Fivet */ + +#include +#include "system.h" /* uptime, adc_vnt() */ +#include "ds18b20.h" /* ds18b20_() */ + +#define ABS( i) ((i < 0) ? -i : i) + +static void track( short *minp, short *maxp, short val) { + if( val < *minp) + *minp = val ; + + if( val > *maxp) + *maxp = val ; +} + +int main( void) { + unsigned last = 0 ; + short calV, calC ; + short minC, maxC ; /* Track temperature from DS18B20 */ + short minV, maxV ; /* Track ADC raw Vref */ + short minT, maxT ; /* Track ADC raw Tsense */ + + minC = minV = minT = 0x7FFF ; + maxC = maxV = maxT = -32768 ; + +/* Initialize ADC and fetch calibration values */ + adc_vnt( VNT_INIT, &calV, &calC) ; + printf( "%u, %u\n", calV, calC) ; + int tconst = 6660 * calV / calC ; + +/* Initialize DS18B20 and initiate temperature conversion */ + ds18b20_init() ; + ds18b20_resolution( 12) ; /* Set highest resolution: 12 bits */ + ds18b20_convert() ; /* start temperature conversion */ + +/* main acquisition loop, reads samples every seconds */ + for( ;;) + if( uptime == last) + yield() ; + else { + short Vsample, Csample ; + + last = uptime ; + + /* Track DS18B20 temperature readings */ + switch( ds18b20_fetch( &Csample)) { + case DS18B20_SUCCESS: + track( &minC, &maxC, Csample) ; + printf( "%i.%i, %i, %i, ", Csample / 10, ABS( Csample % 10), + minC, maxC) ; + break ; + case DS18B20_FAIL_TOUT: + printf( "Timeout, ") ; + break ; + case DS18B20_FAIL_CRC: + printf( "CRC Error, ") ; + } + + ds18b20_convert() ; /* start temperature conversion */ + + /* Track Internal Temperature Sensor readings */ + adc_vnt( VNT_RAW, &Vsample, &Csample) ; + track( &minV, &maxV, Vsample) ; + track( &minT, &maxT, Csample) ; + printf( "%i, %i, %i, %i, %i, %i, %i, %i, ", + calV, Vsample, minV, maxV, + calC, Csample, minT, maxT) ; + Csample = 3630 - (1 + tconst * Csample / Vsample) / 2 ; + Vsample = (660 * calV / Vsample + 1) / 2 ; + printf( "%i.%i, %i.%i\n", Vsample / 100, ABS( Vsample % 100), + Csample / 10, ABS( Csample % 10)) ; + } +} + +/* end of adccalib.c */