]> www.fi.muni.cz Git - tinyboard.git/blobdiff - projects/step-up/adc.c
Buttons, status LED, control implemented.
[tinyboard.git] / projects / step-up / adc.c
index d7679d30b09cff20d7038629cca4d334b8a04b06..194b00e0b0de070d3ab0369b287560df8f93f37a 100644 (file)
@@ -1,5 +1,6 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
+#include <avr/power.h>
 #include <avr/sleep.h>
 
 #include "lights.h"
@@ -11,6 +12,7 @@
 
 volatile static unsigned char current_adc, current_slow_adc;
 static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
+volatile uint16_t jiffies;
 
 static void setup_mux(unsigned char n)
 {
@@ -105,6 +107,9 @@ void init_adc()
        current_slow_adc = NUM_ADCS;
        current_adc = 0;
 
+       power_adc_enable();
+       ACSR |= _BV(ACD);       // but disable the analog comparator
+
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
                // | _BV(ADPS2)                 // CLK/16 = 62.5 kHz
@@ -140,9 +145,27 @@ static void adc1_gain20_adc(uint16_t adcsum)
 }
 #endif
 
+static void inline adc_based_timer()
+{
+       static unsigned char count;
+
+       if (++count < 40) // about 100 Hz jiffies
+               return;
+
+       count = 0;
+       ++jiffies;
+
+       if ((jiffies & 0x0007) == 0) {
+               patterns_next_tick();
+       }
+       timer_check_buttons();
+}
+
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
+       adc_based_timer();
+
        if (read_zero) {
                setup_mux(current_adc);
                read_zero = 0;