-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathIRSenderPWM.cpp
More file actions
333 lines (309 loc) · 9.32 KB
/
IRSenderPWM.cpp
File metadata and controls
333 lines (309 loc) · 9.32 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "HeatpumpIRCompat.h"
#include <IRSender.h>
// ESP8266 does not have the Arduino PWM control registers
#if not defined ESP8266 && not defined ESP32 && not defined ESP_PLATFORM && not defined LIBRETINY
// Heavily based on Ken Shirriff's IRRemote library:
// https://github.com/shirriff/Arduino-IRremote
//
// For PWM on Arduino, see http://playground.arduino.cc/Main/TimerPWMCheatsheet
#if defined(SAM3X8E) || defined(SAM3X8H)
// Arduino Due
uint32_t IR_USE_PWM_PINMASK;
byte IR_USE_PWM_CH;
#endif
#if defined(ARDUINO_ARCH_RENESAS_UNO)
// Arduino Uno R4
#include "pwm.h"
PwmOut* senderPWM;
#endif
IRSenderPWM::IRSenderPWM(uint8_t pin)
: IRSender(pin) {
#if defined(ARDUINO_ARCH_RENESAS_UNO)
// Arduino Uno R4
senderPWM = new PwmOut(_pin);
#else
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW); // When not sending PWM, we want it low
#if defined(SAM3X8E) || defined(SAM3X8H)
// Arduino Due
pmc_set_writeprotect(false);
switch (_pin) {
case 6:
IR_USE_PWM_PINMASK = PIO_PC24;
IR_USE_PWM_CH = 7;
break;
case 7:
IR_USE_PWM_PINMASK = PIO_PC23;
IR_USE_PWM_CH = 6;
break;
case 8:
IR_USE_PWM_PINMASK = PIO_PC22;
IR_USE_PWM_CH = 5;
break;
case 9:
IR_USE_PWM_PINMASK = PIO_PC21;
IR_USE_PWM_CH = 4;
break;
case 34:
IR_USE_PWM_PINMASK = PIO_PC2;
IR_USE_PWM_CH = 0;
break;
case 36:
IR_USE_PWM_PINMASK = PIO_PC4;
IR_USE_PWM_CH = 1;
break;
case 38:
IR_USE_PWM_PINMASK = PIO_PC6;
IR_USE_PWM_CH = 2;
break;
case 40:
IR_USE_PWM_PINMASK = PIO_PC8;
IR_USE_PWM_CH = 3;
break;
}
#endif
#endif
}
// Set the PWM frequency. The selected pin determines which timer to use
void IRSenderPWM::setFrequency(int frequency) {
#if defined(SAM3X8E) || defined(SAM3X8H)
// Arduino Due
pmc_enable_periph_clk(PWM_INTERFACE_ID);
const uint32_t pwmval = (frequency)*2000;
PWMC_ConfigureClocks(PWM_FREQUENCY * PWM_MAX_DUTY_CYCLE, pwmval, F_CPU);
PIO_Configure(PIOC, PIO_PERIPH_B, IR_USE_PWM_PINMASK, PIO_DEFAULT);
PWMC_ConfigureChannel(PWM_INTERFACE, IR_USE_PWM_CH, PWM_CMR_CPRE_CLKB, 0, 0);
PWMC_SetPeriod(PWM_INTERFACE, IR_USE_PWM_CH, 2);
PWMC_SetDutyCycle(PWM_INTERFACE, IR_USE_PWM_CH, 1);
#else
uint8_t pwmval8 = F_CPU / 2000 / (frequency);
uint16_t pwmval16 = F_CPU / 2000 / (frequency);
switch (_pin) {
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
// Arduino Mega
case 9:
// Fall-through to 10, timer2 controls both 9 and 10
case 10:
TCCR2A = _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = pwmval8;
OCR2B = pwmval8 / 3;
break;
case 11:
// Fall-through to 12, timer1 controls both 11 and 12
case 12:
TCCR1A = _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(CS10);
ICR1 = pwmval16;
OCR1A = pwmval16 / 3;
OCR1B = pwmval16 / 3;
break;
case 44:
// Fall-through to 46, timer 5 controls pins 44, 45 and 46 on Arduino Mega
case 45:
case 46:
TCCR5A = _BV(WGM51); // This gives correct data from pins 44,45,46 and similar range to pin 9
TCCR5B = _BV(WGM53) | _BV(CS50);
ICR5 = pwmval16;
OCR5A = pwmval16 / 3;
OCR5B = pwmval16 / 3; // Also enable pin 45
OCR5C = pwmval16 / 3; // Also enable pin 44
#endif
#if defined(AVR_ATmega32U4)
// Arduino Leonardo
case 5:
TCCR3A = (1 << WGM31); // Enable Fast PWM, Clear OC3A on compare
TCCR3B = (1 << WGM33) | (1 << WGM32) | (1 << CS30); // Mode 14, No prescaler
ICR3 = 420; // TOP value for 38 kHz PWM
OCR3A = 210; // 50% duty cycle
break;
case 9:
case 10:
TCCR1A = (1 << WGM11); // Enable Fast PWM, Clear OC1B on compare
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10); // Mode 14, No prescaler
ICR1 = 420; // TOP value for 38 kHz PWM
OCR1B = 210; // 50% duty cycle
break;
case 6:
case 11:
TCCR4B = (1 << CS40); // No prescaler
TCCR4C = 0; // No special functions
TCCR4D = (1 << WGM40); // Enable Fast PWM mode
TCCR4E = 0; // No additional extensions
OCR4A = 210; // 50% duty cycle on pin 11
OCR4D = 210; // 50% duty cycle on pin 6
break;
#endif
#if defined(AVR_ATmega8) || defined(AVR_ATmega328P)
// Arduino Duemilanove or Uno R3
case 3:
// Fall-through to 11, timer2 controls both 3 and 11
case 11:
TCCR2A = _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = pwmval8;
OCR2B = pwmval8 / 3;
break;
case 9:
// Fall-through to 10, timer1 controls both 9 and 10
case 10:
TCCR1A = _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(CS10);
ICR1 = pwmval16;
OCR1A = pwmval16 / 3;
OCR1B = pwmval16 / 3;
break;
#endif
}
#endif
}
// Send an IR 'mark' symbol, i.e. transmitter ON
void IRSenderPWM::mark(int markLength) {
#if defined(ARDUINO_ARCH_RENESAS_UNO)
// Arduino Uno R4
senderPWM->begin(38000.0f, 50.0f); // Enable PWM at 38 kHz, 50% duty
#else
#if defined(SAM3X8E) || defined(SAM3X8H)
// Arduino Due
PWMC_EnableChannel(PWM_INTERFACE, IR_USE_PWM_CH); // Enable pin PWM output
#else
switch (_pin) {
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
// Arduino Mega
case 9:
(TCCR2A |= _BV(COM2B1)); // Enable pin 3 PWM output
break;
case 11:
(TCCR1A |= _BV(COM1A1)); // Enable pin 9 PWM output
break;
case 12:
(TCCR1A |= _BV(COM1B1)); // Enable pin 10 PWM output
break;
case 10:
(TCCR2A |= _BV(COM2A1)); // Enable pin 11 PWM output
break;
case 44:
(TCCR5A |= _BV(COM5C1)); // Enable pin 44 PWM output on Arduino Mega
break;
case 45:
(TCCR5A |= _BV(COM5B1)); // Enable pin 45 PWM output on Arduino Mega
break;
case 46:
(TCCR5A |= _BV(COM5A1)); // Enable pin 46 PWM output on Arduino Mega
break;
#endif
#if defined(AVR_ATmega32U4)
// Arduino Leonardo
case 5:
TCCR3A |= _BV(COM3A1); // Enable PWM on Pin 5
break;
case 6:
TCCR4A |= (1 << COM4D1); // Enable PWM on Pin 6
break;
case 9:
TCCR1A |= _BV(COM1A1); // Enable PWM on Pin 9
break;
case 10:
TCCR1A |= _BV(COM1B1); // Enable PWM on Pin 10
break;
case 11:
TCCR4A |= (1 << COM4A1); // Enable PWM on Pin 11
break;
#endif
#if defined(AVR_ATmega8) || defined(AVR_ATmega328P)
// Arduino Duemilanove or Uno R3
case 3:
(TCCR2A |= _BV(COM2B1)); // Enable pin 3 PWM output
break;
case 9:
(TCCR1A |= _BV(COM1A1)); // Enable pin 9 PWM output
break;
case 10:
(TCCR1A |= _BV(COM1B1)); // Enable pin 10 PWM output
break;
case 11:
(TCCR2A |= _BV(COM2A1)); // Enable pin 11 PWM output
break;
#endif
}
#endif
#endif
delayMicroseconds(markLength);
}
// Send an IR 'space' symbol, i.e. transmitter OFF
void IRSenderPWM::space(int spaceLength) {
#if defined(ARDUINO_ARCH_RENESAS_UNO)
// Arduino Uno R4
senderPWM->end(); // Disable PWM on Pin
#else
#if defined(SAM3X8E) || defined(SAM3X8H)
// Arduino Due
PWMC_DisableChannel(PWM_INTERFACE, IR_USE_PWM_CH); // Disable pin PWM output
#else
switch (_pin) {
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
// Arduino Mega
case 9:
(TCCR2A &= ~(_BV(COM2B1))); // Disable pin 3 PWM output
break;
case 11:
(TCCR1A &= ~(_BV(COM1A1))); // Disable pin 9 PWM output
break;
case 12:
(TCCR1A &= ~(_BV(COM1B1))); // Disable pin 10 PWM output
break;
case 10:
(TCCR2A &= ~(_BV(COM2A1))); // Disable pin 11 PWM output
break;
case 44:
(TCCR5A &= ~(_BV(COM5C1))); // Disable pin 44 PWM output on Arduino Mega
case 45:
(TCCR5A &= ~(_BV(COM5B1))); // Disable pin 45 PWM output on Arduino Mega
case 46:
(TCCR5A &= ~(_BV(COM5A1))); // Disable pin 46 PWM output on Arduino Mega
#endif
#if defined(AVR_ATmega32U4)
// Arduino Leonardo
case 5:
TCCR3A &= ~(_BV(COM3A1)); // Disable PWM (Output forced LOW)
break;
case 6:
TCCR4A &= ~(1 << COM4D1); // Disable PWM (Output forced LOW)
break;
case 9:
TCCR1A &= ~(_BV(COM1A1)); // Disable PWM (Output forced LOW)
break;
case 10:
TCCR1A &= ~(_BV(COM1B1)); // Disable PWM (Output forced LOW)
break;
case 11:
TCCR4A &= ~(1 << COM4A1); // Disable PWM (Output forced LOW)
break;
#endif
#if defined(AVR_ATmega8) || defined(AVR_ATmega328P)
// Arduino Duemilanove or Uno R3
case 3:
(TCCR2A &= ~(_BV(COM2B1))); // Disable pin 3 PWM output
break;
case 9:
(TCCR1A &= ~(_BV(COM1A1))); // Disable pin 9 PWM output
break;
case 10:
(TCCR1A &= ~(_BV(COM1B1))); // Disable pin 10 PWM output
break;
case 11:
(TCCR2A &= ~(_BV(COM2A1))); // Disable pin 11 PWM output
break;
#endif
}
#endif
#endif
// Mitsubishi heatpump uses > 16383us spaces, and delayMicroseconds only works up to 2^14 - 1 us
// Use the less accurate milliseconds delay for longer delays
if (spaceLength < 16383) {
delayMicroseconds(spaceLength);
} else {
delay(spaceLength / 1000);
}
}
#endif // ESP8266