avr/interrupts/main.c

66 lines
1.8 KiB
C

#include <dio.h>
#include <display.h>
#include <stdbool.h>
#include <stdio.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
dio_t light = { .ddr = &DDRC, .port = &PORTC, .pin = &PINC, .addr = 7 };
dio_t interrupt_line = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 2 };
bool button_values[4] = { false, false, false, false };
dio_t buttons[4] = {
{ .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 6 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
{ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
};
ISR(INT2_vect) {
dio_set(&light, 1);
for (int i = 0; i < 4; i++) {
button_values[i] = dio_read(&buttons[i]);
}
}
void display_buttons(display_t *display) {
char msg[16];
snprintf(msg, 16, ": %d %d %d %d", button_values[0], button_values[1], button_values[2], button_values[3]);
display_clear(display);
display_write_message(display, msg);
}
int main(void) {
EIMSK = 1 << INT2;
EICRA |= 1 << ISC21 | 1 << ISC20;
display_t display = {
.reg = {
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
}
};
display_init(&display);
dio_set_direction(&light, LINE_OUT);
dio_set_direction(&interrupt_line, LINE_IN);
for (int i = 0; i < 4; i++) {
dio_set_direction(&buttons[i], LINE_IN);
}
set_sleep_mode(SLEEP_MODE_IDLE);
while (1) {
display_buttons(&display);
dio_set(&light, 0);
sei();
sleep_mode();
cli();
}
return 0;
}