2 #include <util/delay.h>
4 #include <avr/interrupt.h>
11 static void hw_setup()
26 // set_sleep_mode(SLEEP_MODE_IDLE);
29 static void hw_suspend()
46 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
56 // allow wakeup by long button-press only
57 } while (!buttons_wait_for_release());
59 // ok, so I will wake up
65 uint16_t read_adc_sync()
69 ADCSRA |= _BV(ADSC); // start the conversion
71 // wait for the conversion to finish
72 while((ADCSRA & _BV(ADIF)) == 0)
76 ADCSRA |= _BV(ADIF); // clear the IRQ flag
84 ACSR |= _BV(ACD); // but disable the analog comparator
86 ADCSRA = _BV(ADEN) // enable
87 // | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
88 | _BV(ADPS2) // CLK/16 = 62.5 kHz
91 // Disable digital input on all bits used by ADC
92 DIDR0 = _BV(ADC3D) | _BV(ADC2D);
94 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
96 /* Do first conversion and drop the result */
99 // ADCSRA |= _BV(ADIE); // enable IRQ
104 uint16_t a, p, c, target;
118 // pwmled_set_target(1);
129 for (p = 0xd4; p <= 0xd5; p+= 0x1) {
135 for (j = 0; j < 12; j++) {
137 for (i = 0; i < 2; i++) {
138 sum += read_adc_sync();
141 for (i = 0; i < 14; i++) {
142 sum += read_adc_sync();
150 sum += target - read_adc_sync() - (sum >> 7);
155 p = ((sum >> 4) + micro) >> 3;
169 for (i = 0; i < 25; i++) {
171 val = read_adc_sync();
175 if (a < (42 << 5) && p < 250) {
177 } else if (p > 1 && a > (38 << 5)) {
183 // log_word(read_adc_sync());
193 for (t = 0; t < 3; t++) {
194 pwmled_set_target(t);
195 for (i = 0; i < 1000; i++) {
197 while (need_pwmled_adc == 1)
211 set_sleep_mode(SLEEP_MODE_IDLE);
212 } else if (adc_enabled) {
213 set_sleep_mode(SLEEP_MODE_ADC);
215 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
219 // keep BOD active, no sleep_bod_disable();
231 PORTB &=~ _BV( PB2 );