-
Notifications
You must be signed in to change notification settings - Fork 0
/
irsend.c
122 lines (104 loc) · 2.62 KB
/
irsend.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
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
#include <msp430.h>
#include <libemb/serial/serial.h>
#include <libemb/conio/conio.h>
#define T05 300
#define T25 T05*5
#define T35 T05*7
#define T50 T05*10
#define TMAX 65000
#define IR_DETECTOR_PIN BIT1
void reset();
unsigned int rxData = 0; // received data: A4-A0 and C6-C0 0000 AAAA ACCC CCCC
unsigned int bitCounter = 0;
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop WDT
BCSCTL1 = CALBC1_1MHZ; // load calibrated data
DCOCTL = CALDCO_1MHZ;
// serial_init(9600);
P1OUT &= ~(BIT0 + BIT6); // P1.0 & P1.6 out (LP LEDs)
P1DIR |= (BIT0 + BIT6);
// cio_printf("sodfsdhkjh\n\r");
// cio_print((char *) "skfjglkfjg");
P1REN |= IR_DETECTOR_PIN; // P1.1 pull-up
P1OUT |= IR_DETECTOR_PIN; // P1.1 pull-up
P1IE |= IR_DETECTOR_PIN; // P1.1 interrupt enabled
P1IES |= IR_DETECTOR_PIN; // P1.1 high/low edge
P1IFG &= ~IR_DETECTOR_PIN; // P1.1 IFG cleared
CCR0 |= TMAX; // interrupt if no edge for T32
TACTL |= TASSEL_2 + MC_1; // SMCLK, up mode
serial_init(9600);
__bis_SR_register(LPM0_bits + GIE);
// switch to LPM0 with interrupts
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void) {
cio_printf((char *) "INTERRUPTED\n\r");
if (P1IFG & IR_DETECTOR_PIN) {
P1IE &= ~IR_DETECTOR_PIN;
if (bitCounter == 0) {
P1IES &= ~IR_DETECTOR_PIN; // P1.1 low/high edge
bitCounter++;
TACTL |= TACLR;
TACTL |= MC_1;
CCTL0 = CCIE;
} else {
switch (bitCounter) {
case 14: // received all bits
// process received data, for example toggle LEDs
switch (rxData & 0x001F) { // mask device number
case 19: // Volume - 0010011 = 19
P1OUT ^= BIT0;
break;
case 18: // Volume + 0010010 = 18
P1OUT ^= BIT6;
break;
case 21: // Power 0010101 = 21
P1OUT ^= BIT6;
P1OUT ^= BIT0;
break;
case 20: // Mute 0010100 = 20
P1OUT &= ~BIT6;
P1OUT &= ~BIT0;
break;
}
reset();
break;
case 1: // start bit?
if (TA0R < T35) { // could also add || TA0R > T50
reset();
} else {
TACTL |= TACLR;
TACTL |= MC_1;
bitCounter++;
}
break;
default: // data bit
rxData >>= 1;
if (TA0R > T25) {
rxData |= 0x0800; // set bit 12 of rxData
}
TACTL |= TACLR;
TACTL |= MC_1;
bitCounter++; // increase bit counter
break;
}
}
P1IFG &= ~IR_DETECTOR_PIN;
P1IE |= IR_DETECTOR_PIN;
}
}
void reset() {
CCTL0 &= ~CCIE;
P1IES |= IR_DETECTOR_PIN; // P1.1 high/low edge
rxData = 0;
bitCounter = 0;
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void) {
// reset
P1IE &= ~IR_DETECTOR_PIN;
reset();
P1IFG &= ~IR_DETECTOR_PIN;
P1IE |= IR_DETECTOR_PIN;
}