-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
85 lines (66 loc) · 1.25 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <util/atomic.h>
#include "support/uart.h"
volatile uint8_t can_switch = 1;
static void switch_leds() {
PORTC ^= _BV(PC3);
PORTC ^= _BV(PC5);
}
ISR(PCINT0_vect) {
// check that we have a falling edge (i.e. just pressed the button)
if (!(PINB & _BV(PC0))) {
if (can_switch) {
switch_leds();
}
can_switch = 0;
}
}
static void setup_button_ISR() {
PCICR |= _BV(PCIE0);
PCMSK0 |= _BV(PCINT0);
}
static void setup_pins() {
// set PB0 as input
DDRB &= ~(_BV(DDB0));
// set PC3 and PC5 as output
DDRC |= (_BV(DDC3) | _BV(DDC5));
// set PC5 as high at the start
PORTC |= _BV(PC5);
}
static void setup() {
setup_pins();
setup_button_ISR();
uart_streams_setup();
wdt_enable(WDTO_500MS);
// Enable interrupts only when setup of all handlers is done
sei();
}
int main()
{
setup();
puts("Setup done");
uint8_t can_switch_cnt = 0;
while(1)
{
_delay_ms(50);
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
if(!can_switch) {
can_switch_cnt++;
if( can_switch_cnt > 2) {
can_switch = 1;
can_switch_cnt = 0;
}
}
}
wdt_reset();
}
return 0;
}