]> www.fi.muni.cz Git - bike-lights.git/commitdiff
ADC measurements in mA
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 8 Dec 2012 19:27:41 +0000 (20:27 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 8 Dec 2012 19:27:41 +0000 (20:27 +0100)
firmware/adc.c
firmware/lights.h
firmware/pwmled.c

index b7dad0ad59b31a940acecec248c91fd14ba8b2aa..f5d846f2b21d0340bd8f3411a1d214db5ae04583 100644 (file)
@@ -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);
index fe8b59f10730c097394cec835b3dbab7206796d1..16f262e1d159bac7278ee3ee22b6193f7cd13f6f 100644 (file)
@@ -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();
 
index d85087edcf81bc35d3765f419e8babfd24217fe2..7cc300641a5fe3fe2cf536ae32945814721aa44b 100644 (file)
@@ -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
 };