optimise beeping code

This commit is contained in:
Lea 2023-08-29 20:54:39 +02:00
parent 69cf1dc936
commit 36c8a5f897
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

11
main.c
View file

@ -12,8 +12,8 @@
int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms int tick_timer_start = 0xffff - (F_CPU / 1024 / 1000); // 1ms
volatile uint32_t ticks = 0; volatile uint32_t ticks = 0;
volatile beep_time = 0;
volatile bool counting = false; volatile bool counting = false;
volatile bool beeping = false;
void start_timer() { void start_timer() {
// Sets the Clock Select. See table on page 157 the Atmel datasheet. // Sets the Clock Select. See table on page 157 the Atmel datasheet.
@ -40,8 +40,9 @@ ISR(TIMER1_OVF_vect) {
if (bit_is_set(PIND, PIND0)) { if (bit_is_set(PIND, PIND0)) {
PORTD &= 0b11111110; PORTD &= 0b11111110;
} else if (beeping) { } else if (beep_time > 0) {
PORTD |= 0b00000001; PORTD |= 0b00000001;
beep_time--;
} }
} }
@ -50,9 +51,7 @@ ISR(TIMER2_OVF_vect) {
} }
void beep() { void beep() {
beeping = true; beep_time = 50;
_delay_ms(50);
beeping = false;
} }
void main() { void main() {
@ -82,7 +81,7 @@ void main() {
ticks = 0; ticks = 0;
counting = false; counting = false;
if (!pressed_clear) beep(50); if (!pressed_clear) beep();
pressed_clear = true; pressed_clear = true;
} else pressed_clear = false; } else pressed_clear = false;
} }