webradio/firmware/batmon.c

26 lines
629 B
C
Raw Normal View History

2020-07-30 17:11:48 +02:00
#include <avr/io.h>
#include "batmon.h"
void batmon_init()
{
ADCSRA = (1 << ADPS2) | (1 << ADPS1); // 125 kHz ADC clock
ADMUX = ADC_CHANNEL | (1 << REFS0) | (1 << REFS1) | (1 << ADLAR); // Internal 1.2V bandgap, left adj.
ADCSRB = 0; // Manual start
#if ADC_CHANNEL < 7
DIDR0 = (1 << ADC_CHANNEL); // Disconnect digital channel
#endif
// Enable voltage divider
PORT_ADCC(DDR) |= PIN_ADCC;
PORT_ADCC(PORT) &= ~PIN_ADCC;
// Enable ADC
ADCSRA |= (1 << ADEN);
}
inline uint8_t batmon_get_voltage()
{
ADCSRA |= (1 << ADSC); // TODO: Check whether this increases the power consumption of the chip by much
return ADCH;
}