]> www.fi.muni.cz Git - bike-lights.git/commitdiff
battery voltage sensor
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 12 Nov 2012 22:47:06 +0000 (23:47 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 12 Nov 2012 22:47:06 +0000 (23:47 +0100)
firmware/Makefile
firmware/adc.c
firmware/battery.c [new file with mode: 0644]
firmware/lights.h

index 6f2a946f1d9bd085d63720b24a694e39438495be..70c2c323e21a0363c97c3778037b29f20d2253de 100644 (file)
@@ -1,7 +1,7 @@
 
 PROGRAM=lights
 SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c ambient.c pattern.c \
-       buttons.c
+       buttons.c battery.c
 OBJ=$(SRC:.c=.o)
 
 
index 3fdbe3262f3d6467364cd1a73063abd0d3be75e7..8ec62e2457e8419792f4ebd438c00a281c5f9ba4 100644 (file)
@@ -99,7 +99,8 @@ ISR(ADC_vect) { // IRQ handler
                pwmled_adc(current_adc, adcval);
        if (current_adc == AMBIENT_ADC)
                ambient_adc(adcval);
-       // TODO battery sense, etc.
+       if (current_adc == BATTERY_ADC)
+               battery_adc(adcval);
        
        start_next_adc();
 }
diff --git a/firmware/battery.c b/firmware/battery.c
new file mode 100644 (file)
index 0000000..472c1c4
--- /dev/null
@@ -0,0 +1,25 @@
+#include <avr/io.h>
+
+#include "lights.h"
+
+#define RESISTOR_HI    1500    // kOhm
+#define RESISTOR_LO     150    // kOhm
+
+volatile unsigned char battery_100mv;
+
+void battery_adc(uint16_t adcval)
+{
+       /*
+        * This is tricky: we need to maintain precision, so we first
+        * multiply adcval by as big number as possible to fit uint16_t,
+        * then divide to get the final value,
+        * and finally type-cast it to unsigned char.
+        * We don't do running average, as the required precision
+        * is coarse (0.1 V).
+        */
+       battery_100mv = (unsigned char)
+               ((adcval * 11                              // 1.1V
+               * (RESISTOR_HI+RESISTOR_LO)/RESISTOR_LO    // resistor ratio
+               / 4) >> 8);                                // divide by 1024
+}
+
index c8663492c423646112a6cec26c93658f0912292f..243700f9fa44b65171e7633f2d0fa99f24cfa4c3 100644 (file)
@@ -65,6 +65,10 @@ void led_set_status(unsigned char status);
 void init_buttons();
 void timer_check_buttons();
 
+/* battery.c */
+extern volatile unsigned char battery_100mv;
+void battery_adc();
+
 /* main.c */
 void hw_setup();
 void hw_suspend();