commit b73db5264b064cdb89e10c45750806b48ec3a488
parent e7d4be86c104bdd2c83a9e95102987b52259fd31
Author: Anton Konyahin <me@konyahin.xyz>
Date: Thu, 14 Apr 2022 20:38:42 +0300
fix awake by interrupt
Diffstat:
5 files changed, 29 insertions(+), 32 deletions(-)
diff --git a/interrupt.c b/interrupt.c
@@ -1,5 +1,6 @@
#include <avr/io.h>
#include <avr/interrupt.h>
+#include <avr/sleep.h>
#include <util/delay.h>
#include "interrupt.h"
@@ -12,27 +13,18 @@ init_interrupt(void (*on_press) (void))
callback = on_press;
// as input
- DDRB &= ~(1 << BUTTON);
- // activate inner pull-up resitor
- PORTB |= (1 << BUTTON);
-
- cli();
- // enable pin-change interrupt
- GIMSK |= (1 << PCIE);
- // interrupt on specific pin
- PCMSK |= (1 << INTERRUPT);
- sei();
+ DDRB &= ~(1 << PB2);
+ // enable external interrupt
+ GIMSK |= (1 << INT0);
}
-ISR (PCINT0_vect)
+ISR (INT0_vect)
{
- int8_t pressed = !(PINB & (1 << BUTTON));
- if (pressed)
- {
- _delay_ms(30);
- pressed = !(PINB & (1 << BUTTON));
- if (pressed)
- (*callback)();
- }
+ if (PINB & (1 << PB2))
+ return;
+ _delay_ms(30);
+ if (PINB & (1 << PB2))
+ return;
+ (*callback)();
}
diff --git a/interrupt.h b/interrupt.h
@@ -1,8 +1,5 @@
-#ifndef INTERRUPTH
-#define INTERRUPTH
-
-#define BUTTON PB1
-#define INTERRUPT PCINT1
+#ifndef INTERRUPT_H_
+#define INTERRUPT_H_
void
init_interrupt(void (*on_press) (void));
diff --git a/main.c b/main.c
@@ -1,5 +1,6 @@
#include <avr/io.h>
#include <avr/sleep.h>
+#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stddef.h>
@@ -28,6 +29,7 @@ enum status_e
int8_t status = ON;
+int8_t sleep = false;
void status_to_on()
{
status = ON;
@@ -35,10 +37,7 @@ void status_to_on()
PORTB &= ~(1 << LED_GREEN);
play_melody(&end_melody);
set_timer_callback(0, NULL);
-
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- sleep_cpu();
+ sleep = true;
}
void status_to_rest()
@@ -72,6 +71,10 @@ int main(void)
DDRB |= (1 << LED_GREEN);
DDRB |= (1 << LED_RED);
+ set_sleep_mode(SLEEP_MODE_PWR_DOWN);
+ sleep_enable();
+ sei();
+
init_interrupt(switch_status);
init_music();
init_timer();
@@ -83,6 +86,11 @@ int main(void)
PORTB &= ~(1 << LED_RED);
while (true) {
+ if (sleep)
+ {
+ sleep = false;
+ sleep_cpu();
+ }
}
return 0;
diff --git a/music.h b/music.h
@@ -1,5 +1,5 @@
-#ifndef MUSICH
-#define MUSICH
+#ifndef MUSIC_H_
+#define MUSIC_H_
#define SOUND PB0
diff --git a/timer.h b/timer.h
@@ -1,5 +1,5 @@
-#ifndef TIMERH
-#define TIMERH
+#ifndef TIMER_H_
+#define TIMER_H_
void
init_timer(void);