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

adc.c: for one pwmled only

parent e207868d
...@@ -4,32 +4,13 @@ ...@@ -4,32 +4,13 @@
#include "lights.h" #include "lights.h"
#define BATTERY_ADC (N_PWMLEDS + 0) #define ZERO_ADC 1
#define BUTTON_ADC (N_PWMLEDS + 1)
#define ZERO_ADC (N_PWMLEDS + 2)
//#define NUM_ADCS ZERO_ADC //#define NUM_ADCS ZERO_ADC
#define NUM_ADCS 1 #define NUM_ADCS 1
struct {
unsigned char read_zero_log : 2;
unsigned char read_drop_log : 2;
unsigned char read_keep_log : 4;
} adc_params[NUM_ADCS] = {
{ 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1
#if 0
{ 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2
{ 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3
{ 0, 1, AMBIENT_ADC_SHIFT }, // ambient
{ 0, 1, 0 }, // battery
{ 0, 1, 0 }, // gain20
{ 0, 1, 0 }, // buttons
#endif
};
volatile static unsigned char current_adc, current_slow_adc; volatile static unsigned char current_adc, current_slow_adc;
static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log; static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
static void setup_mux(unsigned char n) static void setup_mux(unsigned char n)
{ {
...@@ -72,23 +53,14 @@ void start_next_adc() ...@@ -72,23 +53,14 @@ void start_next_adc()
#endif #endif
adc_sum = 0; adc_sum = 0;
// we use the last iteration of zero_count to set up the MUX read_zero = 0;
// to its final destination, hence the "1 +" below: drop_count = 1;
if (adc_params[current_adc].read_zero_log)
zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
else
zero_count = 1;
if (adc_params[current_adc].read_drop_log)
drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
else
drop_count = 0;
read_count = 1 << adc_params[current_adc].read_keep_log; read_count = 1 << PWMLED_ADC_SHIFT;
n_reads_log = adc_params[current_adc].read_keep_log; n_reads_log = PWMLED_ADC_SHIFT;
// set up mux, start one-shot conversion // set up mux, start one-shot conversion
if (zero_count > 1) if (read_zero)
setup_mux(ZERO_ADC); setup_mux(ZERO_ADC);
else else
setup_mux(current_adc); setup_mux(current_adc);
...@@ -96,6 +68,7 @@ void start_next_adc() ...@@ -96,6 +68,7 @@ void start_next_adc()
ADCSRA |= _BV(ADSC); ADCSRA |= _BV(ADSC);
} }
#if 0
void timer_start_slow_adcs() void timer_start_slow_adcs()
{ {
if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
...@@ -105,6 +78,7 @@ void timer_start_slow_adcs() ...@@ -105,6 +78,7 @@ void timer_start_slow_adcs()
// TODO: kick the watchdog here // TODO: kick the watchdog here
} }
} }
#endif
/* /*
* Single synchronous ADC conversion. * Single synchronous ADC conversion.
...@@ -128,7 +102,6 @@ static uint16_t read_adc_sync() ...@@ -128,7 +102,6 @@ static uint16_t read_adc_sync()
void init_adc() void init_adc()
{ {
unsigned char i;
current_slow_adc = NUM_ADCS; current_slow_adc = NUM_ADCS;
current_adc = 0; current_adc = 0;
...@@ -170,16 +143,11 @@ static void adc1_gain20_adc(uint16_t adcsum) ...@@ -170,16 +143,11 @@ static void adc1_gain20_adc(uint16_t adcsum)
ISR(ADC_vect) { // IRQ handler ISR(ADC_vect) { // IRQ handler
uint16_t adcval = ADCW; uint16_t adcval = ADCW;
if (zero_count) { if (read_zero) {
if (zero_count > 1) { setup_mux(current_adc);
ADCSRA |= _BV(ADSC); read_zero = 0;
zero_count--; ADCSRA |= _BV(ADSC); // drop this one, start the next
return; return;
} else {
setup_mux(current_adc);
zero_count = 0;
/* fall through */
}
} }
if (drop_count) { if (drop_count) {
...@@ -189,7 +157,7 @@ ISR(ADC_vect) { // IRQ handler ...@@ -189,7 +157,7 @@ ISR(ADC_vect) { // IRQ handler
} }
if (read_count) { if (read_count) {
ADCSRA |= _BV(ADSC); ADCSRA |= _BV(ADSC); // immediately start the next conversion
adc_sum += adcval; adc_sum += adcval;
read_count--; read_count--;
return; return;
......
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