Compare commits

..

No commits in common. "0e5e87af7c17effdabda33c768167019aa1fa430" and "e26471381ab30057104465adbcfef28738171261" have entirely different histories.

5 changed files with 20 additions and 70 deletions

View File

@ -1,18 +1,11 @@
#include <avr/io.h> #include <avr/io.h>
#include "batmon.h" #include "batmon.h"
#include "systick.h"
static uint8_t vbat = 0;
void batmon_init() void batmon_init()
{ {
TCCR0A = 0;
TCCR0B = (1 << CS02);
TIMSK0 = (1 << TOIE0);
ADCSRA = (1 << ADPS2) | (1 << ADPS1); // 125 kHz ADC clock ADCSRA = (1 << ADPS2) | (1 << ADPS1); // 125 kHz ADC clock
ADMUX = ADC_CHANNEL | (1 << REFS0) | (1 << REFS1) | (1 << ADLAR); // Internal 1.2V bandgap, left adj. ADMUX = ADC_CHANNEL | (1 << REFS0) | (1 << REFS1) | (1 << ADLAR); // Internal 1.2V bandgap, left adj.
ADCSRB = 0; ADCSRB = 0; // Manual start
#if ADC_CHANNEL < 7 #if ADC_CHANNEL < 7
DIDR0 = (1 << ADC_CHANNEL); // Disconnect digital channel DIDR0 = (1 << ADC_CHANNEL); // Disconnect digital channel
#endif #endif
@ -23,30 +16,10 @@ void batmon_init()
// Enable ADC // Enable ADC
ADCSRA |= (1 << ADEN); 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() inline uint8_t batmon_get_voltage()
{ {
return vbat; ADCSRA |= (1 << ADSC); // TODO: Check whether this increases the power consumption of the chip by much
return ADCH;
} }

View File

@ -6,11 +6,7 @@
#define PORT_ADCC(t) t##D #define PORT_ADCC(t) t##D
#define PIN_ADCC (1 << 1) #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(); void batmon_init();
uint8_t batmon_get_voltage(); uint8_t batmon_get_voltage();
uint8_t batmon_proc();
#endif // BATMON_H #endif // BATMON_H

View File

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

View File

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

View File

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