AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
|
This file provides functions that access the PWM capability of the ATmega328 and ATmega2560 microcontrollers. More...
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... | |
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 |
#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.
pinName
a pin name macro generated by GpioPinPwm().value
a value between 0 and 255.void clearTimer0 | ( | ) |
Clear timer0.
This function clears timer0.
Only call this function if you called initPwmTimer0() instead of initSystemClock().
void clearTimer1 | ( | ) |
Clear timer1.
This function clears timer1, turning off the PWM functionality.
void clearTimer2 | ( | ) |
Clear timer2.
This function clears timer2, turning off the PWM functionality.
void clearTimer3 | ( | ) |
Clear timer3.
This function clears timer3, turning off the PWM functionality.
void clearTimer4 | ( | ) |
Clear timer4.
This function clears timer4, turning off the PWM functionality.
void clearTimer5 | ( | ) |
Clear timer5.
This function clears timer5, turning off the PWM functionality.
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:
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.
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:
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:
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:
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:
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:
|
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.
pinVar
a pin variable that has PWM capabilities (i.e., initialized with makeGpioVarFromGpioPinPwm()).value
a value between 0 and 255.