2 #include <util/delay.h>
9 void battery_init(void)
12 battery_led(LED_ON); _delay_ms(100);
13 battery_led(LED_OFF); _delay_ms(100);
14 battery_led(LED_ON); _delay_ms(100);
15 battery_led(LED_OFF); _delay_ms(100);
16 // LED indicates charger status
17 battery_led(LED_CHARGER);
19 // ADC setting for voltage monitor
20 // Ref:2.56V band-gap, Input:ADC0(PF0), Prescale:128(16MHz/128=125KHz)
21 ADMUX = (1<<REFS1) | (1<<REFS0);
22 ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
23 // digital input buffer disable(24.9.5)
24 DIDR0 = (1<<ADC0D) | (1<<ADC4D) | (1<<ADC7D);
26 DIDR2 = (1<<ADC8D) | (1<<ADC9D) | (1<<ADC11D) | (1<<ADC12D) | (1<<ADC13D);
28 // ADC disable voltate divider(PF4)
33 // Indicator for battery
34 void battery_led(battery_led_t val)
36 if (val == LED_TOGGLE) {
40 } else if (val == LED_ON) {
41 // On overriding charger status
44 } else if (val == LED_OFF) {
45 // Off overriding charger status
49 // Display charger status
55 bool battery_charging(void)
57 if (!(USBSTA&(1<<VBUS))) return false;
60 // MCP73831 MCP73832 LTC4054 Status
61 // Hi-Z Hi-Z Hi-Z Shutdown/No Battery
62 // Low Low Low Charging
63 // Hi Hi-Z Hi-Z Charged
65 // preserve last register status
66 uint8_t ddrf_prev = DDRF;
67 uint8_t portf_prev = PORTF;
73 bool charging = PINF&(1<<5) ? false : true;
75 // restore last register status
76 DDRF = (DDRF&~(1<<5)) | (ddrf_prev&(1<<5));
77 PORTF = (PORTF&~(1<<5)) | (portf_prev&(1<<5));
79 // TODO: With MCP73831 this can not get stable status when charging.
80 // LED is powered from PSEL line(USB or Lipo)
81 // due to weak low output of STAT pin?
82 // due to pull-up'd via resitor and LED?
86 // Returns voltage in mV
87 uint16_t battery_voltage(void)
89 // ADC disable voltate divider(PF4)
93 volatile uint16_t bat;
95 _delay_ms(1); // wait for charging S/H capacitance
98 while (ADCSRA & (1<<ADSC)) ;
101 ADCSRA &= ~(1<<ADEN);
103 // ADC disable voltate divider(PF4)
107 return (bat - BATTERY_ADC_OFFSET) * BATTERY_ADC_RESOLUTION;
110 static bool low_voltage(void) {
111 static bool low = false;
112 uint16_t v = battery_voltage();
113 if (v < BATTERY_VOLTAGE_LOW_LIMIT) {
115 } else if (v > BATTERY_VOLTAGE_LOW_RECOVERY) {
121 battery_status_t battery_status(void)
123 if (USBSTA&(1<<VBUS)) {
125 return battery_charging() ? CHARGING : FULL_CHARGED;
128 return low_voltage() ? LOW_VOLTAGE : DISCHARGING;