-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
124 lines (93 loc) · 1.78 KB
/
main.c
File metadata and controls
124 lines (93 loc) · 1.78 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define NEXT PB2
#define PREV PB0
typedef enum {NONE, RIGHT, LEFT} STATUS;
void enable_pwm () {
TCCR0B |= (1 << CS01); /* prescaler 1:1 */
DDRB |= (1 << PB1);
}
void disable_pwm () {
TCCR0B = 0;
DDRB &= ~(1 << PB1);
}
void send_bit (uint8_t v) {
enable_pwm ();
_delay_ms (0.56);
disable_pwm ();
if (v)
_delay_ms (1.69);
else
_delay_ms (0.56);
}
void send_byte (uint8_t b) {
register uint8_t i = 0 ;
for (i =0 ; i < 8; ++i) {
send_bit (b & (1 << i));
}
}
void send_code (uint8_t code) {
uint8_t addr = 0xaa;
enable_pwm ();
_delay_ms (9);
disable_pwm ();
_delay_ms (4.5);
send_byte (addr);
send_byte (0xff ^ addr);
send_byte (code);
send_byte (code ^ 0xff);
//send_byte (0xaa);
send_bit (1);
}
int main (void) {
PRR = 0;
/* init Timer0 for PWM on OC0A */
/* of the 38khz carrier for IR NEC */
TCCR0A |= (1 << COM0B0) | (1 << WGM01); /* CTC, toggle on match */
OCR0A = 12; // (1 MHZ / 26 ) = 38.4 Khz ~ 38 khz carrier
DDRB |= (1 << PB1);
#if 0
while (1) {
enable_pwm ();
_delay_ms (1);
disable_pwm ();
_delay_ms (1);
}
#endif
#if 0
uint8_t i = 0;
while (1) {
send_code ((i++)%2 ? 0x08 : 0x5a);
_delay_ms (400);
}
#endif
/* set up pullups for next/prev buttons */
PORTB |= (1 << NEXT) | (1 << PREV);
STATUS doing = NONE;
while (1) {
uint8_t code = 0;
if (doing == NONE) {
if ( (PINB & (1 << NEXT)) == 0) {
doing = RIGHT;
code = 0x08;
}
else if ( (PINB & (1 << PREV)) == 0) {
doing = LEFT;
code = 0x5a;
}
if (doing != NONE) {
enable_pwm ();
send_code ( code );
disable_pwm ();
}
}
else {
_delay_ms (40);
if ( (PINB & (1 << PREV)) &&
(PINB & (1 << NEXT)) )
doing = NONE;
}
}
return 0;
}