-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiegand.c
52 lines (43 loc) · 1.12 KB
/
wiegand.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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
volatile uint64_t wiegand_result;
volatile int wiegand_count;
volatile int wiegand_timeout;
ISR (INT0_vect) {
wiegand_timeout = 0;
wiegand_result <<= 1;
wiegand_count++;
}
ISR (INT1_vect) {
wiegand_timeout = 0;
wiegand_result <<= 1;
wiegand_result |= 1;
wiegand_count++;
}
ISR (TIMER0_OVF_vect) {
if (wiegand_count) wiegand_timeout++;
}
void wiegand_init() {
DDRD |= 0xe0; // configure INT0, INT1 as input
GICR = 0xc0; // enable INT0, INT1 as interrupts
MCUCR = 0x0a; // INT0, INT1 on falling edge
}
uint64_t wiegand_read(int *count) {
wiegand_result = 0;
wiegand_count = 0;
wiegand_timeout = 0;
TCCR0 |= 0x05; // enable timer0 at f_clk/1024
TIMSK |= (1 << TOIE0); // enable timer0 interrupts
while (1) {
sleep_enable(); // set SE bit
sei(); // enable global interrupts
sleep_cpu(); // sleep until interrupt
sleep_disable(); // clear SE bit
if (wiegand_timeout > 8) break;
}
TCCR0 = 0; // disable timer0
TIMSK &= ~(1 << TOIE0); // disable timer0 interrupts
*count = wiegand_count;
return wiegand_result;
}