Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
ATmega2560.h
Go to the documentation of this file.
1/*
2Copyright (C) 2020 Bengt Martensson.
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 3 of the License, or (at
7your option) any later version.
8
9This program is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see http://www.gnu.org/licenses/.
16 */
17
24#pragma once
25
26#include "avr.h"
27
28#define CURRENT_CLASS ATmega2560
29
30class ATmega2560 : public Board {
31public:
32
34 };
35
36private:
38#if defined(IR_USE_TIMER1)
39// Timer1 (16 bits)
40#define TIMER_INTR_NAME TIMER1_COMPA_vect
41
42 void timerEnablePwm() {
43 TCCR1A |= _BV(COM1A1);
44 };
45
46 void timerDisablePwm() {
47 TCCR1A &= ~(_BV(COM1A1));
48 };
49
50 void timerEnableIntr() {
51 TIMSK1 = _BV(OCIE1A);
52 }
53
54 void timerDisableIntr() {
55 TIMSK1 = 0;
56 };
57
58
59
60 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
61 const uint16_t pwmval = F_CPU / 2 / frequency;
62 TCCR1A = _BV(WGM11);
63 TCCR1B = _BV(WGM13) | _BV(CS10);
64 ICR1 = pwmval;
65 OCR1A = pwmval * dutyCycle / 100;
66 };
67
68 void timerConfigNormal() {
69 TCCR1A = 0;
70 TCCR1B = _BV(WGM12) | _BV(CS10);
71 OCR1A = F_CPU * microsPerTick / 1000000;
72 TCNT1 = 0;
73 };
74
75#define PWM_PIN 11
76
78#elif defined(IR_USE_TIMER2)
79// Timer2 (8 bits)
80
81#define TIMER_INTR_NAME TIMER2_COMPA_vect
82
83 void timerEnablePwm() {
84 TCCR2A |= _BV(COM2B1);
85 }
86
87 void timerDisablePwm() {
88 TCCR2A &= ~(_BV(COM2B1));
89 }
90
91 void timerEnableIntr() {
92 TIMSK2 = _BV(OCIE2A);
93 }
94
95 void timerDisableIntr() {
96 TIMSK2 = 0;
97 }
98
99 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
100 const uint8_t pwmval = F_CPU / 2 / frequency;
101 TCCR2A = _BV(WGM20);
102 TCCR2B = _BV(WGM22) | _BV(CS20);
103 OCR2A = pwmval;
104 OCR2B = pwmval * dutyCycle / 100;
105 };
106
107#define TIMER_COUNT_TOP (F_CPU * microsPerTick / 1000000UL)
108
109 void timerConfigNormal() {
110 //#if (TIMER_COUNT_TOP < 256)
111 // TCCR2A = _BV(WGM21);
112 // TCCR2B = _BV(CS20);
113 // OCR2A = TIMER_COUNT_TOP;
114 // TCNT2 = 0;
115 //#else
116 TCCR2A = _BV(WGM21);
117 TCCR2B = _BV(CS21);
118 OCR2A = TIMER_COUNT_TOP / 8;
119 TCNT2 = 0;
120 //#endif
121 };
122
123#define PWM_PIN 9
124
126#elif defined(IR_USE_TIMER3)
127// Timer3 (16 bits)
128#define TIMER_INTR_NAME TIMER3_COMPA_vect
129
130 void timerEnablePwm() {
131 TCCR3A |= _BV(COM3A1);
132 };
133
134 void timerDisablePwm() {
135 TCCR3A &= ~(_BV(COM3A1));
136 };
137
138 void timerEnableIntr() {
139 TIMSK3 = _BV(OCIE3A);
140 };
141
142 void timerDisableIntr() {
143 TIMSK3 = 0;
144 };
145
146 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
147 const uint16_t pwmval = F_CPU / 2 / frequency;
148 TCCR3A = _BV(WGM31);
149 TCCR3B = _BV(WGM33) | _BV(CS30);
150 ICR3 = pwmval;
151 OCR3A = pwmval * dutyCycle / 100;
152 };
153
154 void timerConfigNormal() {
155 TCCR3A = 0;
156 TCCR3B = _BV(WGM32) | _BV(CS30);
157 OCR3A = F_CPU * microsPerTick / 1000000;
158 TCNT3 = 0;
159 };
160
161#define PWM_PIN 5
162
164#elif defined(IR_USE_TIMER4)
165// Timer4 (16 bits)
166#define TIMER_INTR_NAME TIMER4_COMPA_vect
167
168 void timerEnablePwm() {
169 TCCR4A |= _BV(COM4A1);
170 };
171
172 void timerDisablePwm() {
173 TCCR4A &= ~(_BV(COM4A1));
174 };
175
176 void timerEnableIntr() {
177 TIMSK4 = _BV(OCIE4A);
178 };
179
180 void timerDisableIntr() {
181 TIMSK4 = 0;
182 };
183
184 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
185 const uint16_t pwmval = F_CPU / 2 / frequency;
186 TCCR4A = _BV(WGM41);
187 TCCR4B = _BV(WGM43) | _BV(CS40);
188 ICR4 = pwmval;
189 OCR4A = pwmval * dutyCycle / 100;
190 };
191
192 void timerConfigNormal() {
193 TCCR4A = 0;
194 TCCR4B = _BV(WGM42) | _BV(CS40);
195 OCR4A = F_CPU * microsPerTick / 1000000;
196 TCNT4 = 0;
197 };
198
199#define PWM_PIN 6
200
202#elif defined(IR_USE_TIMER5)
203// Timer5 (16 bits)
204#define TIMER_INTR_NAME TIMER5_COMPA_vect
205
206 void timerEnablePwm() {
207 TCCR5A |= _BV(COM5A1);
208 };
209
210 void timerDisablePwm() {
211 TCCR5A &= ~(_BV(COM5A1));
212 };
213
214 void timerEnableIntr() {
215 TIMSK5 = _BV(OCIE5A);
216 };
217
218 void timerDisableIntr() {
219 TIMSK5 = 0;
220 };
221
222 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
223 const uint16_t pwmval = F_CPU / 2 / frequency;
224 TCCR5A = _BV(WGM51);
225 TCCR5B = _BV(WGM53) | _BV(CS50);
226 ICR5 = pwmval;
227 OCR5A = pwmval * dutyCycle / 100;
228 };
229
230 void timerConfigNormal() {
231 TCCR5A = 0;
232 TCCR5B = _BV(WGM52) | _BV(CS50);
233 OCR5A = F_CPU * microsPerTick / 1000000;
234 TCNT5 = 0;
235 };
236
237#define PWM_PIN 46
238
240#else // ! defined(IR_USE_TIMER2)
241
242#error Config error, either IR_USE_TIMER1, IR_USE_TIMER2, IR_USE_TIMER3, IR_USE_TIMER4, or IR_USE_TIMER5 must be defined.
243
244#endif
245};
246
247/* From http://busyducks.com/ascii-art-arduinos +-----+
248
249 +----[PWR]-------------------| USB |--+
250 | +-----+ |
251 | GND/RST2 [ ] [ ] |
252 | MOSI2/SCK2 [ ] [ ] SCL[ ] | D0
253 | 5V/MISO2 [ ] [ ] SDA[ ] | D1
254 | AREF[ ] |
255 | GND[ ] |
256 | [ ]N/C SCK/13[ ]~| B7
257 | [ ]v.ref MISO/12[ ]~| B6
258 | [ ]RST MOSI/11[ ]~| B5
259 | [ ]3V3 +----------+ 10[ ]~| B4
260 | [ ]5v | ARDUINO | 9[ ]~| H6
261 | [ ]GND | MEGA | 8[ ]~| H5
262 | [ ]GND +----------+ |
263 | [ ]Vin 7[ ]~| H4
264 | 6[ ]~| H3
265 | [ ]A0 5[ ]~| E3
266 | [ ]A1 4[ ]~| G5
267 | [ ]A2 INT5/3[ ]~| E5
268 | [ ]A3 INT4/2[ ]~| E4
269 | [ ]A4 TX>1[ ]~| E1
270 | [ ]A5 RX<0[ ]~| E0
271 | [ ]A6 |
272 | [ ]A7 TX3/14[ ] | J1
273 | RX3/15[ ] | J0
274 | [ ]A8 TX2/16[ ] | H1
275 | [ ]A9 RX2/17[ ] | H0
276 | [ ]A10 TX1/INT3/18[ ] | D3
277 | [ ]A11 RX1/INT2/19[ ] | D2
278 | [ ]A12 I2C-SDA/INT1/20[ ] | D1
279 | [ ]A13 I2C-SCL/INT0/21[ ] | D0
280 | [ ]A14 |
281 | [ ]A15 | Ports:
282 | RST SCK MISO | 22=A0 23=A1
283 | ICSP [ ] [ ] [ ] | 24=A2 25=A3
284 | [ ] [ ] [ ] | 26=A4 27=A5
285 | GND MOSI 5V | 28=A6 29=A7
286 | G | 30=C7 31=C6
287 | N 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 5 | 32=C5 33=C4
288 | D 2 0 8 6 4 2 0 8 6 4 2 0 8 6 4 2 V | 34=C3 35=C2
289 | ~ ~ | 36=C1 37=C0
290 | @ # # # # # # # # # # # # # # # # @ | 38=D7 39=G2
291 | @ # # # # # # # # # # # # # # # # @ | 40=G1 41=G0
292 | ~ | 42=L7 43=L6
293 | G 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 5 | 44=L5 45=L4
294 | N 3 1 9 7 5 3 1 9 7 5 3 1 9 7 5 3 V | 46=L3 47=L2
295 | D | 48=L1 49=L0 SPI:
296 | | 50=B3 51=B2 50=MISO 51=MOSI
297 | 2560 ____________/ 52=B1 53=B0 52=SCK 53=SS
298 \_______________________/
299
300*/
int8_t dutycycle_t
Type for duty cycle in percent.
Definition: InfraredTypes.h:36
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:31
This class serves as an HAL (Hardware Abstraction Layer).
Definition: Board.h:33
virtual void timerEnableIntr()=0
Start periodic sampling routine.
virtual void timerDisablePwm()=0
Turn off PWM output.
virtual void timerConfigHz(frequency_t hz, dutycycle_t dutyCycle=defaultDutyCycle)=0
Configure hardware PWM, but do not enable it.
static const unsigned long microsPerTick
Definition: Board.h:57
virtual void timerDisableIntr()=0
Turn off periodic interrupts.
virtual void timerEnablePwm()=0
Start PWM output.
virtual void timerConfigNormal()=0
Disables the PWM configuration.