blob: 9cb3daa3c9b66f7677a6353c4071863aa8c10fe1 (
plain)
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
|
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define TEST_LED PB1
#define LOCK_LED PD6
#define UNLOCK_LED PD7
#define SYN 0xAA
#define LOCK 0xB5
#define UNLOCK 0xAE
#define SIGPIN PB3
#define SIGLEN 200
static volatile unsigned char data = 0;
static inline void led_init(void)
{
DDRB |= (1 << TEST_LED);
DDRD |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
}
static inline void pcint2_init(void)
{
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT2);
}
int main(void)
{
DDRB &= ~(1 << SIGPIN);
PORTB &= ~(1 << SIGPIN);
led_init();
pcint2_init();
sei();
for (;;) {
if (data == LOCK) {
PORTD |= (1 << LOCK_LED);
PORTD &= ~(1 << UNLOCK_LED);
}
if (data == UNLOCK) {
PORTD &= ~(1 << LOCK_LED);
PORTD |= (1 << UNLOCK_LED);
}
data = 0;
_delay_ms(100);
}
return 0;
}
ISR(PCINT2_vect)
{
int n, bit;
for (n = 7; n >= 0; n--) {
_delay_ms(SIGLEN);
bit = ((PINB >> SIGPIN) & 1);
data = bit == 1 ? (data | (1 << n)) : (data & ~(1 << n));
}
}
|