Commit 70fa3681 authored by Sven Greiner's avatar Sven Greiner

Fix #1: LVP skipped sometimes

Set a flag in the interrupt handler instead of checking the current tick
value.

Requires additional 14 bytes of flash...
parent 82fa080d
......@@ -116,6 +116,9 @@ register uint8_t output_eeprom asm("r7");
register uint8_t output_eeprom_pos asm("r8");
register uint8_t microticks asm("r9");
register uint8_t ticks asm("r10");
#ifdef LOW_VOLTAGE_PROTECTION
register uint8_t run_lvp_check asm("r11");
#endif // ifdef LOW_VOLTAGE_PROTECTION
/**
* Busy wait delay with 10 ms resolution. This function allows to choose the
......@@ -325,11 +328,16 @@ static uint8_t battery_voltage(void) {
ISR(TIM0_OVF_vect) {
if (!--microticks) {
++ticks;
}
// This will be called multiple times, but we don't care
if (ticks == 4) { // ~440 ms
fast_presses = 0;
if (ticks == 4) { // ~440 ms
fast_presses = 0;
}
#ifdef LOW_VOLTAGE_PROTECTION
if ((ticks & 0x7F) == 14) { // Every ~14 s starting after ~1.5 s
run_lvp_check = 1;
}
#endif // ifdef LOW_VOLTAGE_PROTECTION
}
}
......@@ -339,6 +347,9 @@ ISR(TIM0_OVF_vect) {
int main(void) {
microticks = 0;
ticks = 0;
#ifdef LOW_VOLTAGE_PROTECTION
run_lvp_check = 0;
#endif // ifdef LOW_VOLTAGE_PROTECTION
// Fast PWM, system clock with /8 prescaler
// Frequency will be F_CPU/(8*256) = 2343.75 Hz
......@@ -547,10 +558,7 @@ int main(void) {
}
#ifdef LOW_VOLTAGE_PROTECTION
// This condition is true for about 110 ms. If we miss this window, we have
// to wait for the next interval. If the condition is false, it might be
// checked/executed multiple times in the same interval.
if ((ticks & 0x7F) == 14) { // Every ~14 s starting after ~1.5 s
if (run_lvp_check) {
// TODO Take several measurements for noise filtering?
const uint8_t voltage = battery_voltage();
if (voltage <= BAT_CRIT) {
......@@ -572,6 +580,7 @@ int main(void) {
blink(16, FLICKER_TIME);
enable_output();
}
run_lvp_check = 0;
}
#endif // ifdef LOW_VOLTAGE_PROTECTION
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment