AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
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 
176 inline 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 
229 void initPwmTimer0();
230 
231 
244 void initPwmTimer1();
245 
246 
259 void initPwmTimer2();
260 
261 
276 void clearTimer0();
277 
278 
279 
280 
287 void clearTimer1();
288 
289 
296 void clearTimer2();
297 
298 
299 #if defined(__AVR_ATmega2560__)
300 
301 
315 void initPwmTimer3();
316 
317 
331 void initPwmTimer4();
332 
333 
334 
348 void initPwmTimer5();
349 
350 
351 
352 
361 void clearTimer3();
362 
363 
372 void clearTimer4();
373 
374 
383 void clearTimer5();
384 
385 #endif
386 
387 #endif
388 
void clearTimer1()
Clear timer1.
Definition: Pwm.cpp:58
void clearTimer2()
Clear timer2.
Definition: Pwm.cpp:82
void clearTimer0()
Clear timer0.
Definition: Pwm.cpp:34
void clearTimer5()
Clear timer5.
Definition: Pwm.cpp:159
void writeGpioPinPwmV(const GpioPinVariable &pinVar, uint8_t value)
Write a PWM value to a pin.
Definition: Pwm.h:176
void clearTimer4()
Clear timer4.
Definition: Pwm.cpp:134
void initPwmTimer1()
Initialize timer1 for PWM.
Definition: Pwm.cpp:67
void initPwmTimer3()
Initialize timer3 for PWM.
Definition: Pwm.cpp:117
void initPwmTimer4()
Initialize timer4 for PWM.
Definition: Pwm.cpp:143
void clearTimer3()
Clear timer3.
Definition: Pwm.cpp:108
Gpio16Ptr ocr() const
Return a pointer to the OCR register (PWM related).
Definition: GpioPinMacros.h:461
void initPwmTimer2()
Initialize timer2 for PWM.
Definition: Pwm.cpp:91
uint8_t bitNbr() const
Return the bit number of this GPIO pin within the DDR, PORT, and PIN registers.
Definition: GpioPinMacros.h:469
Gpio8Ptr tccr() const
Return a pointer to the TCCR register (PWM related).
Definition: GpioPinMacros.h:465
Gpio8Ptr port() const
Return a pointer to the PORT register.
Definition: GpioPinMacros.h:453
void initPwmTimer5()
Initialize timer5 for PWM.
Definition: Pwm.cpp:168
uint8_t com() const
Return the bit number needed for manipulating TCCR register (PWM related).
Definition: GpioPinMacros.h:473
This class defines a type that can encode a GPIO pin as a variable. Read the section on GPIO Pin Vari...
Definition: GpioPinMacros.h:424
void initPwmTimer0()
Initialize timer0 for PWM.
Definition: Pwm.cpp:43
This file contains the primary macros for naming and manipulating GPIO pin names. ...