/
antispark.c
51 lines (47 loc) · 986 Bytes
/
antispark.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
#include <avr/io.h>
int main(void) {
ADCSRA &= ~(1 << ADEN);
DDRB = 0xFF ^ (1 << DDB4);
PORTB = 0x00;
OCR1A = 60;
TCCR1 = (1 << CTC1) | (1 << CS11) | (1 << CS12) | (1 << CS13);
uint8_t buttonDown = 0;
uint8_t on = 0;
uint8_t halfSeconds = 0;
for(;;) {
if (bit_is_set(PINB, PB4)) {
if (buttonDown == 0) {
TCCR1 = (1 << CTC1) | (1 << CS11) | (1 << CS12) | (1 << CS13);
buttonDown = 1;
if (on == 0) {
PORTB = (1 << PORTB1);
}
} else {
if (TIFR & (1 << OCF1A)) {
halfSeconds += 1;
TIFR = (1 << OCF1A);
TCNT1 = 0;
}
if (halfSeconds == 2 && on == 0) {
PORTB = (1 << PORTB2);
on = 1;
halfSeconds = 0;
TCCR1 = 0x00;
} else if (halfSeconds == 3 && on == 1) {
PORTB = 0x00;
on = 0;
halfSeconds = 0;
TCCR1 = 0x00;
}
}
} else if (bit_is_clear(PINB, PB4)) {
buttonDown = 0;
halfSeconds = 0;
TCCR1 = 0x00;
if (on == 0) {
PORTB = 0x00;
}
}
}
return 0;
}