Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
ATmega32U4.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
25#pragma once
26
27#include "avr.h"
28
29#if !defined(ARDUINO_AVR_LEONARDO) && !defined(ARDUINO_AVR_MICRO)
30#error Board not supported
31#endif
32
33#define CURRENT_CLASS ATmega32U4
34
35class ATmega32U4 : public Board {
36public:
38
39private:
40
42#if defined(IR_USE_TIMER1)
43// Timer1 (16 bits)
44#define TIMER_INTR_NAME TIMER1_COMPA_vect
45
46 void timerEnablePwm() {
47 TCCR1A |= _BV(COM1A1);
48 };
49
50 void timerDisablePwm() {
51 TCCR1A &= ~(_BV(COM1A1));
52 };
53
54 void timerEnableIntr() {
55 TIMSK1 = _BV(OCIE1A);
56 };
57
58 void timerDisableIntr() {
59 TIMSK1 = 0U;
60 };
61
62 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
63 const uint16_t pwmval = F_CPU / 2UL / frequency;
64 TCCR1A = _BV(WGM11);
65 TCCR1B = _BV(WGM13) | _BV(CS10);
66 ICR1 = pwmval;
67 OCR1A = pwmval * dutyCycle / 100UL;
68 };
69
70 void timerConfigNormal() {
71 TCCR1A = 0;
72 TCCR1B = _BV(WGM12) | _BV(CS10);
73 OCR1A = F_CPU * microsPerTick / 1000000UL;
74 TCNT1 = 0U;
75 };
76
77#define PWM_PIN 9
78
80#elif defined(IR_USE_TIMER2) // ! defined(IR_USE_TIMER1)
81#define TIMER_INTR_NAME TIMER2_COMPA_vect
82
83#error IR_USE_TIMER2 in Leonardo currently broken.
84
85void timerEnablePwm() {TCCR2A |= _BV(COM2B1);};
86void timerDisablePwm() { TCCR2A &= ~(_BV(COM2B1));};
87void timerEnableIntr() {TIMSK2 = _BV(OCIE2A);};
88void timerDisableIntr() {TIMSK2 = 0;};
89
90void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
91 const uint8_t pwmval = F_CPU / 2U / frequency;
92 TCCR2A = _BV(WGM20);
93 TCCR2B = _BV(WGM22) | _BV(CS20);
94 OCR2A = pwmval;
95 OCR2B = pwmval * dutyCycle / 100UL; \
96})
97
98#define TIMER_COUNT_TOP (F_CPU * microsPerTick / 1000000)
99
100void timerConfigNormal() {
101 TCCR2A = _BV(WGM21);
102 TCCR2B = _BV(CS21);
103 OCR2A = TIMER_COUNT_TOP / 8U;
104 TCNT2 = 0U;
105};
106
107#define PWM_PIN 3
108
109#else // ! defined(IR_USE_TIMER2)
110
111#error Config error, either IR_USE_TIMER1 or IR_USE_TIMER2 must be defined.
112
113#endif
114};
115
116/* http://busyducks.com/ascii-art-arduinos
117
118 D0 D1 RST
119 GND GND VCC RX TX /DTR
120 +--------------------------------+
121 | [ ] [ ] [ ] [ ] [ ] [ ] |
122 | FTDI |
123 D1 | [ ]1/TX RAW[ ] |
124 D0 | [ ]0/RX GND[ ] |
125 | [ ]RST SCL/A5[ ] RST[ ] | C6
126 | [ ]GND SDA/A4[ ] VCC[ ] |
127 D2 | [ ]2/INT0 ___ A3[ ] | C3
128 D3 |~[ ]3/INT1 / \ A2[ ] | C2
129 D4 | [ ]4 /PRO \ A1[ ] | C1
130 D5 |~[ ]5 \ MINI/ A0[ ] | C0
131 D6 |~[ ]6 \___/ SCK/13[ ] | B5
132 D7 | [ ]7 A7[ ] MISO/12[ ] | B4
133 B0 | [ ]8 A6[ ] MOSI/11[ ]~| B3
134 B1 |~[ ]9 SS/10[ ]~| B2
135 | [RST-BTN] |
136 +--------------------------------+
137*/
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.