-
Notifications
You must be signed in to change notification settings - Fork 6
/
monitor.c
68 lines (53 loc) · 1.4 KB
/
monitor.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
#define F_CPU 1200000
#include <stdint.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define OUT (1 << DDB1)
#define MANCHESTER_DELAY 500
inline void out_high() {
PORTB |= OUT;
}
inline void out_low() {
PORTB &= ~OUT;
}
void manchester_out(uint8_t bit) {
if (bit) {
out_high();
_delay_us(MANCHESTER_DELAY);
out_low();
_delay_us(MANCHESTER_DELAY);
} else {
out_low();
_delay_us(MANCHESTER_DELAY);
out_high();
_delay_us(MANCHESTER_DELAY);
}
}
int main() {
ADMUX = (1 << MUX1); // ADC2 (PB4)
ADCSRA = (1 << ADPS2) // ADC freq 9.6 MHz / 8 (CKDIV8) / 16 (ADPS) = 75 kHz
| (1 << ADEN); // enable ADC!
// Set as output.
DDRB = OUT;
out_low();
for (;;) {
// Start a conversion and wait for it to finish.
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
// Read the ADC result.
uint16_t reading = ADCL;
reading |= ((uint16_t) ADCH) << 8;
// Add a recognizable signature.
reading |= 0xB400;
// Ouput the value in Manchester code with a leading '1' bit preamble.
manchester_out(1);
for (uint8_t i=0; i<16; i++) {
manchester_out(reading & 1);
reading >>= 1;
}
// Restore the data line to the 'low' state.
out_low();
_delay_ms(100);
}
}