Commit ffb49825 authored by Jan Kasprzak's avatar Jan Kasprzak
Browse files

battery gauge

parent 51ec86a4
PROGRAM=lights PROGRAM=lights
SRC=main.c logging.c pwm.c adc.c pwmled.c pattern.c buttons.c control.c SRC=main.c logging.c pwm.c adc.c pwmled.c pattern.c buttons.c control.c \
battery.c
OBJ=$(SRC:.c=.o) OBJ=$(SRC:.c=.o)
......
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
#include "lights.h" #include "lights.h"
#define ZERO_ADC 1 #define ZERO_ADC 2
//#define NUM_ADCS ZERO_ADC //#define NUM_ADCS ZERO_ADC
#define NUM_ADCS 1 #define NUM_ADCS 2
volatile static unsigned char current_adc, current_slow_adc; volatile static unsigned char current_adc;;
static unsigned char need_battery_adc;
static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log; static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
volatile uint16_t jiffies; volatile uint16_t jiffies;
...@@ -21,6 +22,9 @@ static void setup_mux(unsigned char n) ...@@ -21,6 +22,9 @@ static void setup_mux(unsigned char n)
case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended
ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
break; break;
case 1: // battery voltage: 1.1V, ADC1 (PB2), single-ended
ADMUX = _BV(REFS1) | _BV(MUX0);
break;
case ZERO_ADC: // zero: 1.1V, GND, single-ended case ZERO_ADC: // zero: 1.1V, GND, single-ended
ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0); ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
break; break;
...@@ -29,37 +33,23 @@ static void setup_mux(unsigned char n) ...@@ -29,37 +33,23 @@ static void setup_mux(unsigned char n)
void start_next_adc() void start_next_adc()
{ {
#if 0 if (need_battery_adc) {
if (current_adc == 0) { need_battery_adc = 0;
if (current_slow_adc > N_PWMLEDS) { current_adc = 1;
// read one of the non-PWMLED ADCs read_zero = 1;
current_adc = --current_slow_adc; drop_count = 1;
} else { read_count = 1;
// no more non-PWMLEDs to do, start with PWMLEDs n_reads_log = 0;
current_adc = N_PWMLEDS-1;
}
} else if (current_adc >= N_PWMLEDS) {
// one of the non-PWMLED ADCs just finished, skip to PWMLEDs.
current_adc = N_PWMLEDS-1;
} else { } else {
// next PWMLED current_adc = 0;
current_adc--; read_zero = 0;
} drop_count = 1;
#else read_count = 1 << PWMLED_ADC_SHIFT;
// single ADC for testing only n_reads_log = PWMLED_ADC_SHIFT;
current_adc = 0;
#endif
#if 0 }
log_byte(0x90 + current_adc); // debug ADC switching
#endif
adc_sum = 0; adc_sum = 0;
read_zero = 0;
drop_count = 1;
read_count = 1 << PWMLED_ADC_SHIFT;
n_reads_log = PWMLED_ADC_SHIFT;
// set up mux, start one-shot conversion // set up mux, start one-shot conversion
if (read_zero) if (read_zero)
...@@ -104,7 +94,7 @@ static uint16_t read_adc_sync() ...@@ -104,7 +94,7 @@ static uint16_t read_adc_sync()
void init_adc() void init_adc()
{ {
current_slow_adc = NUM_ADCS; need_battery_adc = 0;
current_adc = 0; current_adc = 0;
power_adc_enable(); power_adc_enable();
...@@ -155,6 +145,9 @@ static void inline adc_based_timer() ...@@ -155,6 +145,9 @@ static void inline adc_based_timer()
count = 0; count = 0;
++jiffies; ++jiffies;
if ((jiffies & 0x007F) == 1) { // about every 1s
need_battery_adc = 1;
}
if ((jiffies & 0x0007) == 0) { if ((jiffies & 0x0007) == 0) {
patterns_next_tick(); patterns_next_tick();
} }
...@@ -195,6 +188,9 @@ ISR(ADC_vect) { // IRQ handler ...@@ -195,6 +188,9 @@ ISR(ADC_vect) { // IRQ handler
// pwmled_adc(current_adc, adc_sum); // pwmled_adc(current_adc, adc_sum);
pwmled_adc(adc_sum); pwmled_adc(adc_sum);
break; break;
case 1:
battery_adc(adc_sum);
break;
} }
start_next_adc(); start_next_adc();
......
...@@ -109,7 +109,7 @@ pattern_t *status_led_pattern_select() ...@@ -109,7 +109,7 @@ pattern_t *status_led_pattern_select()
if (e.battery_low) if (e.battery_low)
return number_pattern(1, 1); return number_pattern(1, 1);
return number_pattern(light_mode+1, 0); return number_pattern(battery_gauge(), 0);
} }
#if 0 #if 0
......
...@@ -13,7 +13,7 @@ static void hw_setup() ...@@ -13,7 +13,7 @@ static void hw_setup()
wdt_enable(WDTO_1S); wdt_enable(WDTO_1S);
//init_battery(); init_battery();
init_pwm(); init_pwm();
init_adc(); init_adc();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment