AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Macros | Functions
Pwm.h File Reference

This file provides functions that access the PWM capability of the ATmega328 and ATmega2560 microcontrollers. More...

#include <stdint.h>
#include <util/atomic.h>
#include "GpioPinMacros.h"
Include dependency graph for Pwm.h:

Go to the source code of this file.

Macros

#define writeGpioPinPwm(pinName, value)
 Write a PWM value to a pin. More...
 

Functions

void writeGpioPinPwmV (const GpioPinVariable &pinVar, uint8_t value)
 Write a PWM value to a pin. More...
 
void initPwmTimer0 ()
 Initialize timer0 for PWM. More...
 
void initPwmTimer1 ()
 Initialize timer1 for PWM. More...
 
void initPwmTimer2 ()
 Initialize timer2 for PWM. More...
 
void clearTimer0 ()
 Clear timer0. More...
 
void clearTimer1 ()
 Clear timer1. More...
 
void clearTimer2 ()
 Clear timer2. More...
 
void initPwmTimer3 ()
 Initialize timer3 for PWM. More...
 
void initPwmTimer4 ()
 Initialize timer4 for PWM. More...
 
void initPwmTimer5 ()
 Initialize timer5 for PWM. More...
 
void clearTimer3 ()
 Clear timer3. More...
 
void clearTimer4 ()
 Clear timer4. More...
 
void clearTimer5 ()
 Clear timer5. More...
 

Detailed Description

This file provides functions that access the PWM capability of the ATmega328 and ATmega2560 microcontrollers.

To use these functions, include Pwm.h in your source code and link against Pwm.cpp.

Before you use the writePinPwm() function, you must first initialize the appropriate timers using the appropriate initPwmTimerN() function.

The association between PWN pins and timers is as follows:

For Arduino Uno (ATmega328)

Arduino Uno pin ATmega328 pin Timer
3 PD3 timer2
5 PD5 timer0
6 PD6 timer0
9 PB1 timer1
10 PB2 timer1
11 PB3 timer2

For Arduino Mega (ATmega2560)

Arduino Mega pin ATmega2560 pin Timer
2 PE4 timer3
3 PE5 timer3
4 PG5 timer0
5 PE3 timer3
6 PH3 timer4
7 PH4 timer4
8 PH5 timer4
9 PH6 timer2
10 PB4 timer2
11 PB5 timer1
12 PB6 timer1
13 PB7 timer0
44 PL5 timer5
45 PL4 timer5
46 PL3 timer5
Note
Timer0 is also used by the system clock. Do not initialize or clear timer0 if you are also using the system clock function from SystemClock.h. If you are using the system clock function, you can use timer0-based PWM functions without having to call initPwmTimer0().

Macro Definition Documentation

§ writeGpioPinPwm

#define writeGpioPinPwm (   pinName,
  value 
)

Write a PWM value to a pin.

This sets the duty cycle for the PWM on the pin. Completely off is represented by 0; completely on is represented by 1.

Before calling this function, you must initialize the appropriate timer by calling initPwmTimerN(), where N = 1, 2, 3, 4, or 5 is the timer corresponding to that particular pin.

  • value a value between 0 and 255.
Warning
Timer0 is also used by the system clock. Do not initialize or clear timer0 if you are also using the system clock function from SystemClock.h. If you are using the system clock function, you can use timer0-based PWM functions without having to call initPwmTimer0().
Note
You can temporarily turn off PWM by writing a 0 to the pin with writePinPwm( pin, 0 ). In particular, this is how to turn off PWM to pins associated with timer0 when timer0 is also being used by the system clock.
This macro ensures operations on 16-bit timers are atomic (at the cost of a small amount of overhead in the case of 8-bt timers).

Function Documentation

§ clearTimer0()

void clearTimer0 ( )

Clear timer0.

This function clears timer0.

Note
Timer0 is also used by the system clock. Do not clear timer0 if you are also using the system clock function from SystemClock.h.

Only call this function if you called initPwmTimer0() instead of initSystemClock().

Note
To turn off PWM on pins associated with timer0 while also using the system clock, write a zero to the pin by calling writePinPwm( pinName, 0 ).

§ clearTimer1()

void clearTimer1 ( )

Clear timer1.

This function clears timer1, turning off the PWM functionality.

§ clearTimer2()

void clearTimer2 ( )

Clear timer2.

This function clears timer2, turning off the PWM functionality.

§ clearTimer3()

void clearTimer3 ( )

Clear timer3.

This function clears timer3, turning off the PWM functionality.

Note
This function is only available on Arduino Mega (ATmega2560).

§ clearTimer4()

void clearTimer4 ( )

Clear timer4.

This function clears timer4, turning off the PWM functionality.

Note
This function is only available on Arduino Mega (ATmega2560).

§ clearTimer5()

void clearTimer5 ( )

Clear timer5.

This function clears timer5, turning off the PWM functionality.

Note
This function is only available on Arduino Mega (ATmega2560).

§ initPwmTimer0()

void initPwmTimer0 ( )

Initialize timer0 for PWM.

This function sets timer0 for phase-correct PWM mode. You must call this function or initSystemClock() before calling writePinPwm() on a PWM pin associated with timer0.

The PWM pins supported by timer0 are:

  • Arduino Uno (ATmega328): pin 5 (PD5), pin 6 (PD6)
  • Arduino Mega (ATmega2560): pin 4 (PG5), pin 13 (PB7)
Note
Timer0 is also used by the system clock. Do not initialize timer0 if you are also using the system clock function from SystemClock.h.

The function initSystemClock() puts timer0 in fast PWM mode. While this is different than the phase-correct PWM mode preferred for PWM usage, fast PWM mode still allows PWM operations on the associated pins. However, the duty cycles may be slightly off, and calling writePinPwm( pin, 0 ) may not completely turn off output on the pins associated with timer0.

Only call initPwmTimer0() if you did not call initSystemClock() (i.e., you are not using the system clock) and you wish to use PWM on the pins associate with timer0.

Note
To turn off PWM on pins associated with timer0 while also using the system clock, write a zero to the pin by calling writePinPwm( pinName, 0 ).

§ initPwmTimer1()

void initPwmTimer1 ( )

Initialize timer1 for PWM.

This function sets timer1 for phase-correct PWM mode. You must call this function before calling writePinPwm() on a PWM pin associated with timer1.

The PWM pins supported by timer1 are:

  • Arduino Uno (ATmega328): pin 9 (PB1), pin 10 (PB2)
  • Arduino Mega (ATmega2560): pin 11 (PB5), pin 12 (PB6)

§ initPwmTimer2()

void initPwmTimer2 ( )

Initialize timer2 for PWM.

This function sets timer2 for phase-correct PWM mode. You must call this function before calling writePinPwm() on a PWM pin associated with timer2.

The PWM pins supported by timer2 are:

  • Arduino Uno (ATmega328): pin 3 (PD3), pin 11 (PB3)
  • Arduino Mega (ATmega2560): pin 9 (PH6), pin 10 (PB4)

§ initPwmTimer3()

void initPwmTimer3 ( )

Initialize timer3 for PWM.

This function sets timer3 for phase-correct PWM mode. You must call this function before calling writePinPwm() on a PWM pin associated with timer3.

The PWM pins supported by timer3 are:

  • Arduino Mega (ATmega2560): pin 2 (PE4), pin 3 (PE5)
Note
This function is only available on Arduino Mega (ATmega2560).

§ initPwmTimer4()

void initPwmTimer4 ( )

Initialize timer4 for PWM.

This function sets timer4 for phase-correct PWM mode. You must call this function before calling writePinPwm() on a PWM pin associated with timer4.

The PWM pins supported by timer4 are:

  • Arduino Mega (ATmega2560): pin 6 (PH3), pin 7 (PH4), pin 8 (PH5)
Note
This function is only available on Arduino Mega (ATmega2560).

§ initPwmTimer5()

void initPwmTimer5 ( )

Initialize timer5 for PWM.

This function sets timer5 for phase-correct PWM mode. You must call this function before calling writePinPwm() on a PWM pin associated with timer5.

The PWM pins supported by timer5 are:

  • Arduino Mega (ATmega2560): pin 44 (PL5), pin 45 (PL4), pin 46 (PL3)
Note
This function is only available on Arduino Mega (ATmega2560).

§ writeGpioPinPwmV()

void writeGpioPinPwmV ( const GpioPinVariable pinVar,
uint8_t  value 
)
inline

Write a PWM value to a pin.

This sets the duty cycle for the PWM on the pin. Completely off is represented by 0; completely on is represented by 1.

Before calling this function, you must initialize the appropriate timer by calling initPwmTimerN(), where N = 1, 2, 3, 4, or 5 is the timer corresponding to that particular pin.

  • value a value between 0 and 255.
Warning
Timer0 is also used by the system clock. Do not initialize or clear timer0 if you are also using the system clock function from SystemClock.h. If you are using the system clock function, you can use timer0-based PWM functions without having to call initPwmTimer0().
Note
You can temporarily turn off PWM by writing a 0 to the pin with writePinPwm( pin, 0 ). In particular, this is how to turn off PWM to pins associated with timer0 when timer0 is also being used by the system clock.
This function ensures operations on 16-bit timers are atomic (at the cost of a small amount of overhead in the case of 8-bt timers).