static unsigned char power_level = 0; // selected power level
static unsigned char power_level_changed; // for visual feedback
-#define LED_PWRCHANGE_COUNT 3
+#define LED_PWRCHANGE_COUNT 5
#define LED_BATTEMPTY_COUNT 60
/* timing by WDT */
{
power_adc_enable();
- ADCSRA = _BV(ADEN) // enable
- | _BV(ADPS1) | _BV(ADPS0) // clk/8 = 125 kHz
- | _BV(ADIE); // enable IRQ
+ ADCSRA = _BV(ADEN) // enable
+ | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz
ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
// 1.1V reference, PB3 pin, single-ended
DIDR0 |= _BV(ADC3D); // PB3 pin as analog input
static void adc_susp()
{
- ADCSRA &= ~_BV(ADEN); // disable ADC
+ ADCSRA = 0; // disable ADC
DIDR0 &= ~_BV(ADC3D); // disable analog input on PB3
power_adc_disable();
}
-static void adc_start_measurement()
+static void adc_start_measurement(unsigned char on)
{
- ADCSRA |= _BV(ADSC);
+ adc_drop = 1;
+ adc_type = on;
+ ADCSRA |= _BV(ADSC) | _BV(ADIE);
}
ISR(ADC_vect)
batt_on = adcw << ADC_RUNAVG_SHIFT;
}
}
+ ADCSRA &= ~_BV(ADIE);
}
/* ===================== Timer/Counter1 for PWM ===================== */
ISR(TIM1_OVF_vect)
{
- adc_drop = 1;
- adc_type = 1;
- adc_start_measurement();
+ adc_start_measurement(1);
}
ISR(TIM1_COMPB_vect)
{
- adc_drop = 1;
- adc_type = 0;
- adc_start_measurement();
+ adc_start_measurement(0);
}
static void pwm_set(unsigned char pwm)
uint32_t pwm;
unsigned char batt_on8;
- if (battery_level() == 0 || batt_on == 0) {
+ if (battery_level() == 0) {
pwm_set(0);
// TODO power_down() after some time
return;
}
+ if (!batt_on) {
+ batt_on = batt_off;
+ };
+
batt_on8 = batt_on >> 8;
pwm = (uint32_t)PWM_TOP * power_levels[power_level]
if (pwm < PWM_MIN)
pwm = PWM_MIN;
+#if 0
log_byte(0x10 + power_level);
log_byte(batt_on8);
log_byte(pwm & 0xFF);
+#endif
pwm_set(pwm);
}