From dfdde30a343b7da032bea880b0677b49ff0e5d8a Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 8 Dec 2012 20:27:41 +0100 Subject: [PATCH] ADC measurements in mA --- firmware/adc.c | 6 ++-- firmware/lights.h | 1 + firmware/pwmled.c | 80 ++++++++++++++++++++++++++++------------------- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/firmware/adc.c b/firmware/adc.c index b7dad0a..f5d846f 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -21,15 +21,15 @@ static void inline setup_mux(unsigned char n) switch (n) { case 0: // pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 20 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX1) | _BV(MUX0); - sum_shift = 3; // 8 measurements + sum_shift = PWMLED_ADC_SHIFT; break; case 1: // pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 20 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); - sum_shift = 3; // 8 measurements + sum_shift = PWMLED_ADC_SHIFT; break; case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended ADMUX = _BV(REFS1) | _BV(MUX2); - sum_shift = 3; // 8 measurements + sum_shift = PWMLED_ADC_SHIFT; break; case 3: // ambient light: 1.1V, ADC5 (PA6), single-ended ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0); diff --git a/firmware/lights.h b/firmware/lights.h index fe8b59f..16f262e 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -23,6 +23,7 @@ void inline log_word(uint16_t word) { } #endif /* adc.c */ +#define PWMLED_ADC_SHIFT 3 /* 1<<3 measurements per single callback */ void init_adc(); void susp_adc(); diff --git a/firmware/pwmled.c b/firmware/pwmled.c index d85087e..7cc3006 100644 --- a/firmware/pwmled.c +++ b/firmware/pwmled.c @@ -15,49 +15,65 @@ pwmled_t pwmleds[N_PWMLEDS]; #define PWMLED2_TESTING_WITH_350MA_LED #define SENSE_MOHM 33 /* 0.033 Ohm */ -#define MA_MOHM_GAIN_TO_ADC(ma, mohm, gain) (\ - ((unsigned long)(ma))*(mohm) /* voltage at sensing resistor in uV */ \ - /(1100000UL/gain/1024UL) /* voltage of ADC reading == 1 */ \ -) +/* + * Voltage in uV at ADC reading == 1 is 1100/gain/1024 + * ADC module returns sum of 1 << PWMLED_ADC_SHIFT measurements + * Voltage in uV measured is current in mA * sense resistance in mOhm + */ +#define MA_GAIN_TO_ADC(ma, gain) ((uint16_t) \ + ((uint32_t)(ma) \ + * (SENSE_MOHM) \ + * (1 << (PWMLED_ADC_SHIFT)) \ + * 1024 \ + / (1100000/(gain)))) + +#define PWMLED_TESTING 1 static uint16_t adc_max[N_PWMLEDS] = { - MA_MOHM_GAIN_TO_ADC( 400, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 30, SENSE_MOHM, 20), -#ifdef PWMLED2_TESTING_WITH_350MA_LED - MA_MOHM_GAIN_TO_ADC( 400, SENSE_MOHM, 1) +#ifdef PWMLED_TESTING + MA_GAIN_TO_ADC( 400, 20), + MA_GAIN_TO_ADC( 30, 20), + MA_GAIN_TO_ADC( 800, 1) #else - MA_MOHM_GAIN_TO_ADC(2500, SENSE_MOHM, 1) + MA_GAIN_TO_ADC( 900, 20), + MA_GAIN_TO_ADC( 30, 20), + MA_GAIN_TO_ADC(2500, 1) #endif }; static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = { +#ifdef PWMLED_TESTING /* pwmled0 */ - MA_MOHM_GAIN_TO_ADC( 20, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 50, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 100, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 20), + MA_GAIN_TO_ADC( 50, 20), + MA_GAIN_TO_ADC( 150, 20), + MA_GAIN_TO_ADC( 250, 20), + MA_GAIN_TO_ADC( 350, 20), /* pwmled1 */ - 16, 32, 64, 112, -#if 0 - MA_MOHM_GAIN_TO_ADC( 5, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 12, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 16, SENSE_MOHM, 20), - MA_MOHM_GAIN_TO_ADC( 20, SENSE_MOHM, 20), -#endif + MA_GAIN_TO_ADC( 5, 20), + MA_GAIN_TO_ADC( 10, 20), + MA_GAIN_TO_ADC( 15, 20), + MA_GAIN_TO_ADC( 20, 20), /* pwmled2 */ - 24, 32, 40, 48 -#if 0 -#ifdef PWMLED2_TESTING_WITH_350MA_LED - MA_MOHM_GAIN_TO_ADC( 100, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC( 140, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC( 250, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 1), + MA_GAIN_TO_ADC( 100, 1), + MA_GAIN_TO_ADC( 200, 1), + MA_GAIN_TO_ADC( 300, 1), + MA_GAIN_TO_ADC( 400, 1) #else - MA_MOHM_GAIN_TO_ADC( 150, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC( 700, SENSE_MOHM, 1), - MA_MOHM_GAIN_TO_ADC(2400, SENSE_MOHM, 1), -#endif + /* pwmled0 */ + MA_GAIN_TO_ADC( 100, 20), + MA_GAIN_TO_ADC( 300, 20), + MA_GAIN_TO_ADC( 700, 20), + MA_GAIN_TO_ADC( 800, 20), + /* pwmled1 */ + MA_GAIN_TO_ADC( 5, 20), + MA_GAIN_TO_ADC( 10, 20), + MA_GAIN_TO_ADC( 18, 20), + MA_GAIN_TO_ADC( 23, 20), + /* pwmled2 */ + MA_GAIN_TO_ADC( 200, 1), + MA_GAIN_TO_ADC( 400, 1), + MA_GAIN_TO_ADC( 800, 1), + MA_GAIN_TO_ADC(1500, 1) #endif }; -- 2.43.5