-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat85_ir.c
229 lines (214 loc) · 8.08 KB
/
at85_ir.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "at85_ir.h"
#include "oled.h"
/********************************************************************************
* at85-rda5807m/sh1106.c
*
* This file is part of the at85-rda5807m distribution.
* (https://github.com/yjdwbj/at85-rda5807m).
* Copyright (c) 2021 Liu Chun Yang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
********************************************************************************/
// https://exploreembedded.com/wiki/NEC_IR_Remote_Control_Interface_with_8051
// https://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol
// https://circuitdigest.com/microcontroller-projects/build-your-own-ir-remote-decoder-using-tsop-and-pic-microcontroller
static volatile bool repeatCode = false;
static volatile bool _toggleMute = false;
// 4Byte of IR data,
// LSB first, 1 start bit + 16 bit address (or 8 bit address and 8 bit inverted address) + 8 bit command + 8 bit inverted command + 1 stop bit.
#define BUFFER_SIZE 32
// Pulse buffer index (changes from 0 to BUFFER_SIZE)
static volatile uint8_t bufferIndex = 0;
// Current state of capturing
static volatile IRCaptureState captureState = WAIT_STATE;
#define LCD_BUFFER_SIZE 16
uint8_t lcd_buffer[LCD_BUFFER_SIZE];
uint8_t irdata[4] = {0, 0, 0, 0};
IR_data *IRData = irdata;
/**
* Table 10-1. Reset and Interrupt Vectors
*
Vec No. |Program Addr| Source | Interrupt Definition
--------+------------+------------+-----------------------------
1 | 0x0000 | RESET | External pin, power-on reset, brown-out reset,watchdog reset
2 | 0x0001 | INT0 | External interrupt request 0
3 | 0x0002 | PCINT0 | Pin change interrupt request 0
4 | 0x0003 | PCINT1 | Pin change interrupt request 1
5 | 0x0004 | WDT | Watchdog time-out
6 | 0x0005 | TIMER1 | CAPT Timer/Counter1 capture event
7 | 0x0006 | TIMER1 | COMPA Timer/Counter1 compare match A
8 | 0x0007 | TIMER1 | COMPB Timer/Counter1 compare match B
9 | 0x0008 | TIMER1 | OVF Timer/Counter0 overflow
10 | 0x0009 | TIMER0 | COMPA Timer/Counter0 compare match A
11 | 0x000A | TIMER0 | COMPB Timer/Counter0 compare match B
12 | 0x000B | TIMER0 | OVF Timer/Counter0 overflow
13 | 0x000C | ANA_COMP | Analog comparator
14 | 0x000D | ADC ADC | conversion complete
15 | 0x000E | EE_RDY | EEPROM ready
16 | 0x000F | USI_START| USI START
17 | 0x0010 | USI_OVF | USI overflow
*/
void ir_bus_init(void) {
// DDRB &= ~_BV(PB2); // IR data pin ,set input
TCCR0A = 0;
TCCR0B = 0;
MCUCR |= _BV(ISC01); // The falling edge of INT0 generates an interrupt request.
GIMSK |= _BV(INT0); // External Interrupt Request 0, must give +5v power.
}
static uint8_t check_low_time(bool isLeading) {
unsigned char val;
TCCR0A = 0;
TCNT0 = 0;
// if isLeading using 1024 prescaler, one TICK is 0.000128s,MAX=32.768ms
// otherwise using 64 prescaler, one TICK is 0.0000008s, MAX=2048us,
TCCR0B |= (isLeading ? _BV(CS02) : _BV(CS01)) | _BV(CS00);
TIMSK |= _BV(TOIE0);
while (!(PINB & _BV(PB2)))
;
TCCR0B = 0;
val = TCNT0;
return val;
}
static uint8_t check_high_time(bool isLeading) {
unsigned char val;
TCCR0A = 0;
TCNT0 = 0;
// https://www.arduinoslovakia.eu/application/timer-calculator
// The CPU clock frequenct is 8MHz, so time period T = 1/8M = 0.000125ms.
// a 16-bit timer(MAX = 65535),and 8-bit timer (MAX=255).
TCCR0B |= (isLeading ? _BV(CS02) : _BV(CS01)) | _BV(CS00);
TIMSK |= _BV(TOIE0);
while ((PINB & _BV(PB2)))
;
TCCR0B = 0;
val = TCNT0;
return val;
}
ISR(TIMER0_OVF_vect) {
}
ISR(INT0_vect) {
uint8_t countNum = 0;
switch (captureState) {
// Waiting for next data
case WAIT_STATE:
countNum = check_low_time(true);
// check it is valid 9ms leading pulse burst,pulse tolerance greater than 8.5ms and less than 9.6ms.
// 67 * 128us = 8.576ms, 75 * 128us = 9.6ms
if (countNum < 67 || countNum > 75)
break;
countNum = check_high_time(true);
if (countNum > 16 && countNum < 22) {
// check it's repeat codes leading valid 2.25ms space
captureState = FINAL_PULSE_STATE;
repeatCode = true;
} else if (countNum >= 32 && countNum <= 36) {
// check it's valid 4.5ms space, 32 * 128us = 4.096ms, 36 * 128us = 4.608ms
captureState = DATA_STATE;
bufferIndex = 0;
}
break;
// Initial space or repeat space
case FINAL_PULSE_STATE:
countNum = check_low_time(false);
// a 562.5µs pulse burst to mark the end of the space (and hence end of the transmitted repeat code).
// 55 * 8us = 445us, 81 * 8us = 648us
if (countNum < 55 || countNum > 81) {
bufferState = BUF_NOT_READY;
break;
}
bufferState = repeatCode ? BUF_REPEAT : BUF_READY;
break;
// 8-bit address
case DATA_STATE:
countNum = check_low_time(false);
if (countNum < 55 || countNum > 81) {
bufferState = BUF_NOT_READY;
break;
}
countNum = check_high_time(false);
if (countNum < 55 || countNum > 254) {
bufferState = BUF_NOT_READY;
break;
}
char index = bufferIndex / 8;
irdata[index] >>= 1;
// Logical '0' – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
// Logical '1' – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms
if (countNum > 140)
irdata[index] |= 0x80; // 140 * 8us = 1.12ms, greater than 1.12ms means logical '1'.
if (bufferIndex++ >= BUFFER_SIZE) {
captureState = FINAL_PULSE_STATE;
}
break;
}
TCNT0 = 0;
}
bool ir_data_ready(void) {
if (bufferState != BUF_NOT_READY) {
cli(); // Disable INT0, prepare to LCD show string.
oled_clear();
switch (IRData->cmd) {
case 0xfa05:
seek_up();
break;
case 0xfd02:
seek_down();
break;
case 0xe11e:
volume_up();
break;
case 0xf50a:
volume_down();
break;
case 0xe916:
toggle_mute();
break;
case 0xab54:
shift_band();
break;
case 0xf30c:
shift_space();
break;
case 0xb24d:
toggle_power();
break;
default:
break;
}
if (has_poweroff()) {
oled_clear();
} else {
show_radio_info();
}
sei();
bufferIndex = 0;
bufferState = BUF_NOT_READY;
captureState = WAIT_STATE;
repeatCode = false;
}
}
void show_radio_info(void) {
sprintf(lcd_buffer, "cmd:%x", IRData->cmd);
oled_p8x16str(0, 0, lcd_buffer);
memset(lcd_buffer, 0, LCD_BUFFER_SIZE);
sprintf(lcd_buffer, "vol:%d,space:%d", (uint8_t)get_volume(), (uint8_t)get_space());
oled_p8x16str(0, 4, lcd_buffer);
memset(lcd_buffer, 0, LCD_BUFFER_SIZE);
sprintf(lcd_buffer, "mute:%c rssi:%u", get_mute() ? 't' : 'f', (uint8_t)get_rssi());
oled_p8x16str(0, 6, lcd_buffer);
memset(lcd_buffer, 0, LCD_BUFFER_SIZE);
sprintf(lcd_buffer, "ch:%d,band:%d", (uint16_t)get_frequency(), (uint8_t)get_band());
oled_p8x16str(0, 2, lcd_buffer);
memset(lcd_buffer, 0, LCD_BUFFER_SIZE);
}