Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
ATmega328P.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#define CURRENT_CLASS ATmega328P
30
31class ATmega328P : public Board {
32public:
33
35#ifdef IR_USE_TIMER1
36
37 void timerEnablePwm() {
38 TCCR1A |= _BV(COM1A1);
39 };
40
41 void timerDisablePwm() {
42 TCCR1A &= ~(_BV(COM1A1));
43 };
44
45 void timerEnableIntr() {
46 TIMSK1 = _BV(OCIE1A);
47 };
48
49 void timerDisableIntr() {
50 TIMSK1 = 0U;
51 };
52
53#define TIMER_INTR_NAME TIMER1_COMPA_vect
54
55 void timerConfigHz(frequency_t val, dutycycle_t dutyCycle) {
56 const uint16_t pwmval = F_CPU / 2U / val;
57 TCCR1A = _BV(WGM11);
58 TCCR1B = _BV(WGM13) | _BV(CS10);
59 ICR1 = pwmval;
60 OCR1A = pwmval * dutyCycle / 100U;
61 };
62
63 void timerConfigNormal() {
64 TCCR1A = 0U;
65 TCCR1B = _BV(WGM12) | _BV(CS10);
66 OCR1A = F_CPU * microsPerTick / 1000000UL;
67 TCNT1 = 0U;
68 };
69
70#define PWM_PIN 9
72#elif defined(IR_USE_TIMER2) || defined(DOXYGEN) // ! defined(IR_USE_TIMER1)
73
75 TCCR2A |= _BV(COM2B1);
76 }
77
79 TCCR2A &= ~(_BV(COM2B1));
80 }
81
83 TIMSK2 = _BV(OCIE2A);
84 };
85
87 TIMSK2 = 0U;
88 };
89
90#define TIMER_INTR_NAME TIMER2_COMPA_vect
91
92 void timerConfigHz(frequency_t val, dutycycle_t dutyCycle) {
93 const uint8_t pwmval = F_CPU / 2U / val;
94 TCCR2A = _BV(WGM20);
95 TCCR2B = _BV(WGM22) | _BV(CS20);
96 OCR2A = pwmval;
97 OCR2B = pwmval * dutyCycle / 100U;
98 };
99
101 TCCR2A = _BV(WGM21);
102 TCCR2B = _BV(CS21);
103 OCR2A = F_CPU * microsPerTick / 1000000UL / 8U;
104 TCNT2 = 0U;
105 }
107
108#define PWM_PIN 3
109
110#else // ! defined(IR_USE_TIMER2)
111
112#error Config error, either IR_USE_TIMER1 or IR_USE_TIMER2 must be defined.
113
114#endif
115};
116
117/* From http://busyducks.com/ascii-art-arduinos
118
119
120
121 +----[PWR]-------------------| USB |--+
122 | +-----+ |
123 | GND/RST2 [ ][ ] |
124 | MOSI2/SCK2 [ ][ ] A5/SCL[ ] | C5
125 | 5V/MISO2 [ ][ ] A4/SDA[ ] | C4
126 | AREF[ ] |
127 | GND[ ] |
128 | [ ]N/C SCK/13[ ] | B5
129 | [ ]v.ref MISO/12[ ] | .
130 | [ ]RST MOSI/11[ ]~| .
131 | [ ]3V3 +---+ 10[ ]~| .
132 | [ ]5v | A | 9[ ]~| .
133 | [ ]GND -| R |- 8[ ] | B0
134 | [ ]GND -| D |- |
135 | [ ]Vin -| U |- 7[ ] | D7
136 | -| I |- 6[ ]~| .
137 | [ ]A0 -| N |- 5[ ]~| .
138 | [ ]A1 -| O |- 4[ ] | .
139 | [ ]A2 +---+ INT1/3[ ]~| .
140 | [ ]A3 INT0/2[ ] | .
141 | [ ]A4/SDA RST SCK MISO TX>1[ ] | .
142 | [ ]A5/SCL [ ] [ ] [ ] RX<0[ ] | D0
143 | [ ] [ ] [ ] |
144 | UNO_R3 GND MOSI 5V ____________/
145 \_______________________/
146
147
148 +-----+
149 +------------| USB |------------+
150 | +-----+ |
151 B5 | [ ]D13/SCK MISO/D12[ ] | B4
152 | [ ]3.3V MOSI/D11[ ]~| B3
153 | [ ]V.ref ___ SS/D10[ ]~| B2
154 C0 | [ ]A0 / N \ D9[ ]~| B1
155 C1 | [ ]A1 / A \ D8[ ] | B0
156 C2 | [ ]A2 \ N / D7[ ] | D7
157 C3 | [ ]A3 \_0_/ D6[ ]~| D6
158 C4 | [ ]A4/SDA D5[ ]~| D5
159 C5 | [ ]A5/SCL D4[ ] | D4
160 | [ ]A6 INT1/D3[ ]~| D3
161 | [ ]A7 INT0/D2[ ] | D2
162 | [ ]5V GND[ ] |
163 C6 | [ ]RST RST[ ] | C6
164 | [ ]GND 5V MOSI GND TX1[ ] | D0
165 | [ ]Vin [ ] [ ] [ ] RX1[ ] | D1
166 | [ ] [ ] [ ] |
167 | MISO SCK RST |
168 | NANO-V3 |
169 +-------------------------------+
170
171*/
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
void timerConfigHz(frequency_t val, dutycycle_t dutyCycle)
Configure hardware PWM, but do not enable it.
Definition: ATmega328P.h:92
void timerDisablePwm()
Turn off PWM output.
Definition: ATmega328P.h:78
void timerEnableIntr()
Start periodic sampling routine.
Definition: ATmega328P.h:82
void timerConfigNormal()
Disables the PWM configuration.
Definition: ATmega328P.h:100
void timerDisableIntr()
Turn off periodic interrupts.
Definition: ATmega328P.h:86
void timerEnablePwm()
Start PWM output.
Definition: ATmega328P.h:74
This class serves as an HAL (Hardware Abstraction Layer).
Definition: Board.h:33
static const unsigned long microsPerTick
Definition: Board.h:57