Compare commits

..

5 Commits

5 changed files with 70 additions and 20 deletions

View File

@ -1,11 +1,18 @@
#include <avr/io.h>
#include "batmon.h"
#include "systick.h"
static uint8_t vbat = 0;
void batmon_init()
{
TCCR0A = 0;
TCCR0B = (1 << CS02);
TIMSK0 = (1 << TOIE0);
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
ADCSRB = 0;
#if ADC_CHANNEL < 7
DIDR0 = (1 << ADC_CHANNEL); // Disconnect digital channel
#endif
@ -16,10 +23,30 @@ void batmon_init()
// Enable ADC
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADSC);
}
uint8_t batmon_proc()
{
static uint16_t sum = 0;
static uint8_t cnt = 0;
if (ADCSRA & (1 << ADIF)) {
sum += ADCH;
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;
}
return 0;
}
inline uint8_t batmon_get_voltage()
{
ADCSRA |= (1 << ADSC); // TODO: Check whether this increases the power consumption of the chip by much
return ADCH;
return vbat;
}

View File

@ -6,7 +6,11 @@
#define PORT_ADCC(t) t##D
#define PIN_ADCC (1 << 1)
#define BAT_THRES_POWER_ON 60 // TODO: Minimum ADC value to allow turning the device on
#define BAT_THRES_POWER_OFF 50 // TODO: ADC value to (hard) power off the device.
void batmon_init();
uint8_t batmon_get_voltage();
uint8_t batmon_proc();
#endif // BATMON_H

View File

@ -21,13 +21,16 @@ inline int8_t get_rotary_delta()
return rotary_delta;
}
#define ROTARY_DEBOUNCE 150
static inline void input_proc_rotary()
{
static int8_t last_a;
int8_t a;
int8_t b;
int8_t p;
uint8_t silly_debounce = 0;
if (silly_debounce == 0) {
p = PORT_ROTARY(PIN);
a = (p & PIN_ROTARY_A);
b = (p & PIN_ROTARY_B);
@ -35,15 +38,18 @@ static inline void input_proc_rotary()
b = !b;
if (a == 1 && last_a == 0) {
_delay_ms(1); // TODO
silly_debounce = ROTARY_DEBOUNCE;
rotary_delta += !!(a ^ b);
}
if (a == 0 && last_a == 1) {
_delay_ms(1); // TODO
silly_debounce = ROTARY_DEBOUNCE;
rotary_delta -= !(a ^ b);
}
last_a = a;
} else {
silly_debounce--;
}
}
// TODO: Pressing two keys at the same time can lead to unfortunate behavior.
@ -53,7 +59,6 @@ void input_proc_switches()
uint8_t ev;
uint8_t i;
static uint8_t sw_last = 0xFF;
uint8_t delta;
sw = PORT_KEYPAD(PIN) & PIN_KEYPAD_MASK;
ev = sw ^ sw_last;

View File

@ -83,6 +83,7 @@ void lcd_title(const char *msg1, const char *msg2)
int main()
{
char *post_msg;
uint8_t sched = 0;
MCUSR &= ~(1 << WDRF);
WDTCSR = (1 << WDCE);
@ -108,8 +109,20 @@ int main()
while (1) {
if (!spi_proc()) {
switch (sched) {
case 0:
timer_proc();
break;
case 1:
input_proc();
break;
}
}
sched = sched + 1;
if (sched == 4) {
sched = 0;
if (batmon_proc())
reset();
}
}
return 0;

View File

@ -3,5 +3,6 @@
#include <avr/io.h>
void suspend();
char* post();
#endif // PM_H