AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Loading...
Searching...
No Matches
Pwm.h
Go to the documentation of this file.
1/*
2 Pwm.h - Macros and Functions for accessing the PMW capabilities of AVRs.
3 For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4 This is part of the AVRTools library.
5 Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22
76#ifndef Pwm_h
77#define Pwm_h
78
79
80#include <stdint.h>
81
82#include <util/atomic.h>
83
84#include "GpioPinMacros.h"
85
86
87
88#define _writeGpioPinPwm( ddr, port, pin, nbr, chl, ocr, com, tccr, value ) \
89 do \
90 { \
91 if ( value <= 0 ) \
92 { \
93 tccr &= ~(1<<com); \
94 port &= ~(1<<nbr); \
95 } \
96 else if ( value >= 255 ) \
97 { \
98 tccr &= ~(1<<com); \
99 port |= (1<<nbr); \
100 } \
101 else \
102 { \
103 tccr |= (1<<com); \
104 ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) \
105 { \
106 ocr = value; \
107 } \
108 } \
109 } \
110 while ( 0 )
111
112
113
143#define writeGpioPinPwm( pinName, value ) _writeGpioPinPwm( pinName, value )
144
145
146
147
176inline void writeGpioPinPwmV( const GpioPinVariable& pinVar, uint8_t value )
177{
178 if ( value == 0 )
179 {
180 *(pinVar.tccr()) &= ~( 1 << pinVar.com() );
181 *(pinVar.port()) &= ~( 1 << pinVar.bitNbr() );
182
183 }
184 else if ( value == 255 )
185 {
186 *(pinVar.tccr()) &= ~( 1 << pinVar.com() );
187 *(pinVar.port()) |= ( 1 << pinVar.bitNbr() );
188 }
189 else
190 {
191 *(pinVar.tccr()) |= ( 1 << pinVar.com() );
192 // Provide atomicity for 16-bit timers (not needed for 8-bit timers, but be safe)
193 ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
194 {
195 *(pinVar.ocr()) = value;
196 }
197 }
198}
199
200
201
229void initPwmTimer0();
230
231
244void initPwmTimer1();
245
246
259void initPwmTimer2();
260
261
276void clearTimer0();
277
278
279
280
287void clearTimer1();
288
289
296void clearTimer2();
297
298
299#if defined(__AVR_ATmega2560__)
300
301
315void initPwmTimer3();
316
317
331void initPwmTimer4();
332
333
334
348void initPwmTimer5();
349
350
351
352
361void clearTimer3();
362
363
372void clearTimer4();
373
374
383void clearTimer5();
384
385#endif
386
387#endif
388
This file contains the primary macros for naming and manipulating GPIO pin names.
void writeGpioPinPwmV(const GpioPinVariable &pinVar, uint8_t value)
Write a PWM value to a pin.
Definition Pwm.h:176
void initPwmTimer5()
Initialize timer5 for PWM.
Definition Pwm.cpp:168
void clearTimer4()
Clear timer4.
Definition Pwm.cpp:134
void initPwmTimer4()
Initialize timer4 for PWM.
Definition Pwm.cpp:143
void initPwmTimer3()
Initialize timer3 for PWM.
Definition Pwm.cpp:117
void initPwmTimer0()
Initialize timer0 for PWM.
Definition Pwm.cpp:43
void clearTimer0()
Clear timer0.
Definition Pwm.cpp:34
void initPwmTimer1()
Initialize timer1 for PWM.
Definition Pwm.cpp:67
void clearTimer1()
Clear timer1.
Definition Pwm.cpp:58
void clearTimer5()
Clear timer5.
Definition Pwm.cpp:159
void clearTimer2()
Clear timer2.
Definition Pwm.cpp:82
void clearTimer3()
Clear timer3.
Definition Pwm.cpp:108
void initPwmTimer2()
Initialize timer2 for PWM.
Definition Pwm.cpp:91
This class defines a type that can encode a GPIO pin as a variable. Read the section on [GPIO Pin Var...
Definition GpioPinMacros.h:425
uint8_t bitNbr() const
Return the bit number of this GPIO pin within the DDR, PORT, and PIN registers.
Definition GpioPinMacros.h:469
Gpio8Ptr port() const
Return a pointer to the PORT register.
Definition GpioPinMacros.h:453
Gpio16Ptr ocr() const
Return a pointer to the OCR register (PWM related).
Definition GpioPinMacros.h:461
uint8_t com() const
Return the bit number needed for manipulating TCCR register (PWM related).
Definition GpioPinMacros.h:473
Gpio8Ptr tccr() const
Return a pointer to the TCCR register (PWM related).
Definition GpioPinMacros.h:465