Compare commits

...

2 Commits

Author SHA1 Message Date
a396052487 fw: Implement power-on battery check 2020-07-31 18:23:58 +02:00
b4cfb40bb7 fw: Fix battery voltage calculation 2020-07-31 18:23:39 +02:00
4 changed files with 35 additions and 5 deletions

View File

@ -33,16 +33,15 @@ uint8_t batmon_proc()
if (ADCSRA & (1 << ADIF)) {
sum += ADCH;
if (!(cnt++)) {
if (!(++cnt)) {
vbat = sum >> 8;
sum = 0;
}
vbat = ADCH;
ADCSRA &= ~(1 << ADIF);
ADCSRA |= (1 << ADSC); // TODO: I want this to work off TIMER0
if (vbat < BAT_THRES_POWER_OFF)
return 1;
}
ADCSRA &= ~(1 << ADIF);
ADCSRA |= (1 << ADSC); // TODO: I want this to work off TIMER0
}
return 0;
}
@ -50,3 +49,28 @@ inline uint8_t batmon_get_voltage()
{
return vbat;
}
static uint8_t batmon_get_voltage_sync()
{
uint16_t sum = 0;
uint8_t cnt = 255;
ADCSRA |= (1 << ADSC);
do {
if (ADCSRA & (1 << ADIF)) {
sum += ADCH;
ADCSRA &= ~(1 << ADIF);
ADCSRA |= (1 << ADSC);
cnt--;
}
} while (cnt);
return (sum >> 8);
}
// Note: Call batmon_init() after using this function before using batmon_proc() again
inline uint8_t batmon_ok_to_boot()
{
return (batmon_get_voltage_sync() > BAT_THRES_POWER_ON);
}

View File

@ -12,5 +12,6 @@
void batmon_init();
uint8_t batmon_get_voltage();
uint8_t batmon_proc();
uint8_t batmon_ok_to_boot();
#endif // BATMON_H

View File

@ -88,6 +88,7 @@ int main()
MCUSR &= ~(1 << WDRF);
WDTCSR = (1 << WDCE);
cli();
_delay_ms(25);
suspend();

View File

@ -4,6 +4,7 @@
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "pm.h"
#include "batmon.h"
static void disable_periphery()
{
@ -58,7 +59,10 @@ void reset()
char* post()
{
// TODO: Implement battery voltage check
_delay_ms(500);
if (!batmon_ok_to_boot())
return PSTR("Low battery.");
return 0;
}