/************************************************************************* * * LED Light power meter * * Autor: PESV * Summer 2009 * * * ATTiny26 measures voltage at ADC input pin, representing current * flowing to flashlight power LED (255 ADC value corresponds to 2.55A). * Accumulated mAh power consumption is displayed by 6 leds connected * to output pins. Short button press displays current status, long button * press clears total mAh counter. Button being pressed during startup * causes meter to display status continuously, otherwise status leds are * turned off after predefined timeout. Status is kept in eeprom memory, * saved at every power off (power going down is detected using analog * comparator monitoring power supply voltage). * *************************************************************************/ #include #include #include #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1< 1) CLEARBIT(PORTA, PA0); else if (val < 1) SETBIT(PORTA, PA0); if (val > 3) CLEARBIT(PORTA, PA1); else if (val < 3) SETBIT(PORTA, PA1); if (val > 5) CLEARBIT(PORTA, PA2); else if (val < 5) SETBIT(PORTA, PA2); if (val > 7) CLEARBIT(PORTA, PA4); else if (val < 7) SETBIT(PORTA, PA4); if (val > 9) CLEARBIT(PORTA, PA5); else if (val < 9) SETBIT(PORTA, PA5); if (val > 11) CLEARBIT(PORTA, PA6); else if (val < 11) SETBIT(PORTA, PA6); if (val % 2 == 1) blink_led = val / 2 + 1; } int main(void) { // initialize led output pins DDRA |= 0x77; // set pins PA0, PA1, PA2, PA4, PA5, PA6 direction as output // initialize button inpit pin CLEARBIT(DDRB, PB3); // set pin PB3 direction as input SETBIT(PORTB, PB3); // enable internal pullup for PB3 if (!(PINB & (1< LED_TIMEOUT + RESET_TIMEOUT) { // button pressed for more then RESET_TIMEOUT total = 0; // reset mAh counter button = 0; } if (button > 0) // ledbar timeout coundown in progress setledbar(total / ledbar_step_mah / mah_unit); // light status ledbar else if (ignore_led_timeout) setledbar(total / ledbar_step_mah / mah_unit); // light status ledbar else setledbar(0); // ledbar off } return 0; } // timer interrupt ISR(TIMER0_OVF0_vect) { static int cnt = 0; cnt++; // monitor button status if (!(PINB & (1<