commit 4b80983f6b9a4c1b41ad8c5b7aeaba7fbc3d3a05
Author: Anton Konyahin <me@konyahin.xyz>
Date: Sun, 10 Apr 2022 15:57:30 +0300
Initial commit
Diffstat:
4 files changed, 115 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,17 @@
+PROG=USBasp
+MMCU=attiny45
+MCP=t45
+FCPU=1200000
+
+burn: main.hex
+ avrdude -c $(PROG) -p $(MCP) -U flash:w:./main.hex
+
+build: main.hex
+
+main.hex: main.c interrupt.c
+ avr-gcc -g -Os -mmcu=$(MMCU) -DF_CPU=$(FCPU) -c main.c interrupt.c
+ avr-gcc -g -mmcu=$(MMCU) -o main.elf main.o interrupt.o
+ avr-objcopy -j .text -j .data -O ihex main.elf main.hex
+
+clean:
+ rm -f *.elf *.hex *.o
diff --git a/interrupt.c b/interrupt.c
@@ -0,0 +1,34 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+#include "interrupt.h"
+
+static void (*callback)(void);
+
+void
+init_interrupt(void (*on_press) (void))
+{
+ callback = on_press;
+
+ DDRB &= ~(1 << BUTTON);
+
+ cli();
+ // enable pin-change interrupt
+ GIMSK |= (1 << PCIE);
+ // interrupt on specific pin
+ PCMSK |= (1 << INTERRUPT);
+ sei();
+}
+
+ISR (PCINT0_vect)
+{
+ int8_t pressed = !(PINB & (1 << BUTTON));
+ if (pressed) {
+ _delay_ms(30);
+ pressed = !(PINB & (1 << BUTTON));
+ if (pressed)
+ (*callback)();
+ }
+}
+
diff --git a/interrupt.h b/interrupt.h
@@ -0,0 +1,5 @@
+#define BUTTON PB3
+#define INTERRUPT PCINT3
+
+void
+init_interrupt(void (*on_press) (void));
diff --git a/main.c b/main.c
@@ -0,0 +1,59 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+#include <stdbool.h>
+
+#include "interrupt.h"
+
+#define LED_GREEN PB4
+#define LED_RED PB1
+#define SOUND PB0
+
+enum status_e {
+ ON,
+ WORK,
+ REST
+};
+
+int8_t status = ON;
+
+void set_status()
+{
+ switch (status)
+ {
+ case ON:
+ PORTB &= ~(1 << LED_RED);
+ PORTB &= ~(1 << LED_GREEN);
+ break;
+ case WORK:
+ PORTB |= (1 << LED_RED);
+ PORTB &= ~(1 << LED_GREEN);
+ break;
+ case REST:
+ PORTB &= ~(1 << LED_RED);
+ PORTB |= (1 << LED_GREEN);
+ break;
+ }
+}
+
+void switch_status()
+{
+ if (++status > REST)
+ status = ON;
+ set_status();
+}
+
+void main(void)
+{
+ DDRB |= (1 << LED_GREEN);
+ DDRB |= (1 << LED_RED);
+ DDRB |= (1 << SOUND);
+
+ init_interrupt(switch_status);
+
+ cli();
+ sei();
+
+ while (true) {
+ }
+}