Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
v1.0.0 for hardware-PWM on AVR boards
Browse files Browse the repository at this point in the history
### Initial Releases v1.0.0

1. Initial coding to support **AVR boards, such as Mega-2560, UNO, Nano, Leonardo, etc.**, etc. using AVR core
2. The hardware-based PWM channels can generate very high frequencies.
  • Loading branch information
khoih-prog authored Oct 29, 2022
1 parent ff713bf commit 22230c2
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 252 deletions.
56 changes: 28 additions & 28 deletions examples/PWM_Basic/PWM_Basic.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************************************************************
PWM_Basic.ino
For AVR-based boards (UNO, Nano, Mega, 32U4, 16U4, etc. )
Written by Khoi Hoang
Expand All @@ -24,20 +24,20 @@
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Expand Down Expand Up @@ -76,13 +76,13 @@
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega
#elif ( PWM_USING_ATMEGA_32U4 )

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
Expand All @@ -103,21 +103,21 @@ void setup()
Serial.print(F("\nStarting PWM_Basic on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);
//assigns PWM frequency of 20 KHz and a duty cycle of 0%
PWM_Instance = new AVR_PWM(pinToUse, 20000, 0);

//assigns PWM frequency of 20 KHz and a duty cycle of 0%
PWM_Instance = new AVR_PWM(pinToUse, 20000, 0);
}

void loop()
{
delay(10000);
frequency = 20000;
dutyCycle = 90;
delay(10000);
frequency = 20000;
dutyCycle = 90;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

delay(10000);
dutyCycle = 10;
delay(10000);
dutyCycle = 10;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
}
92 changes: 46 additions & 46 deletions examples/PWM_DynamicDutyCycle/PWM_DynamicDutyCycle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Expand Down Expand Up @@ -78,14 +78,14 @@
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega
#elif ( PWM_USING_ATMEGA_32U4 )

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
////#define pinToUse 3 // Timer0B on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
Expand Down Expand Up @@ -118,56 +118,56 @@ void printPWMInfo(AVR_PWM* PWM_Instance)

void setup()
{
Serial.begin(115200);
Serial.begin(115200);

while (!Serial);
while (!Serial);

delay(100);
delay(100);

Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);

frequency = 5000;
frequency = 5000;
//frequency = 70.0f;

PWM_Instance = new AVR_PWM(pinToUse, frequency, 50);

if (PWM_Instance)
{
if (!PWM_Instance->setPWM())
PWM_Instance = new AVR_PWM(pinToUse, frequency, 50);

if (PWM_Instance)
{
if (!PWM_Instance->setPWM())
{
Serial.println(F("Stop here"));

// stop here
while (true)
delay(1000);
}
}
}

Serial.println(dashLine);
Serial.println(dashLine);
}

void loop()
{
//frequency = 10000.0f;
{
//frequency = 10000.0f;
frequency = 1000.0f;

dutyCycle = 90.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
dutyCycle = 90.0f;

printPWMInfo(PWM_Instance);
Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

delay(5000);
dutyCycle = 10.0f;
printPWMInfo(PWM_Instance);

delay(5000);
dutyCycle = 10.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);
Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);

delay(5000);
}
88 changes: 44 additions & 44 deletions examples/PWM_DynamicDutyCycle_Int/PWM_DynamicDutyCycle_Int.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
////////////////////////////////////////////
// For Mega (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46)
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 2 => TIMER3B // PE 4 ** 2 ** PWM2
Pin 3 => TIMER3C // PE 5 ** 3 ** PWM3
Pin 4 => TIMER0B // PG 5 ** 4 ** PWM4
Pin 5 => TIMER3A // PE 3 ** 5 ** PWM5
Pin 6 => TIMER4A // PH 3 ** 6 ** PWM6
Pin 7 => TIMER4B // PH 4 ** 7 ** PWM7
Pin 8 => TIMER4C // PH 5 ** 8 ** PWM8
Pin 9 => TIMER2B // PH 6 ** 9 ** PWM9
Pin 10 => TIMER2A // PB 4 ** 10 ** PWM10
Pin 11 => TIMER1A // PB 5 ** 11 ** PWM11
Pin 12 => TIMER1B // PB 6 ** 12 ** PWM12
Pin 13 => TIMER0A // PB 7 ** 13 ** PWM13
Pin 44 => TIMER5C // PL 5 ** 44 ** D44
Pin 45 => TIMER5B // PL 4 ** 45 ** D45
Pin 46 => TIMER5A // PL 3 ** 46 ** D46
////////////////////////////////////////////
// For 32u4 (3, 5, 6, 9, 10, 11, 13)
Expand Down Expand Up @@ -75,13 +75,13 @@
//#define pinToUse 46 // Timer5A on Mega
//#define pinToUse 45 // Timer5B on Mega
//#define pinToUse 44 // Timer5C on Mega
#elif ( PWM_USING_ATMEGA_32U4 )

#elif ( PWM_USING_ATMEGA_32U4 )
// Pins tested OK on 32u4
//#define pinToUse 5 // Timer3A on 32u4
#define pinToUse 9 // Timer1A on 32u4
//#define pinToUse 10 // Timer1B on 32u4

#else

// Pins tested OK on Nano / UNO
Expand Down Expand Up @@ -114,50 +114,50 @@ void printPWMInfo(AVR_PWM* PWM_Instance)

void setup()
{
Serial.begin(115200);
Serial.begin(115200);

while (!Serial);
while (!Serial);

delay(100);
delay(100);

Serial.print(F("\nStarting PWM_DynamicDutyCycle_Int on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);
Serial.print(F("\nStarting PWM_DynamicDutyCycle_Int on "));
Serial.println(BOARD_NAME);
Serial.println(AVR_PWM_VERSION);

frequency = 1000;
frequency = 1000;

PWM_Instance = new AVR_PWM(pinToUse, frequency, 50);
PWM_Instance = new AVR_PWM(pinToUse, frequency, 50);

if (PWM_Instance)
{
PWM_Instance->setPWM();
}
if (PWM_Instance)
{
PWM_Instance->setPWM();
}

Serial.println(dashLine);
Serial.println(dashLine);
}

void loop()
{
delay(5000);
delay(5000);

frequency = 5000;
frequency = 5000;

// 50% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 32768;
// 50% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 32768;

Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.println((float) dutyCycle * 100 / 65536);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);

printPWMInfo(PWM_Instance);
printPWMInfo(PWM_Instance);

delay(5000);
delay(5000);

// 20% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 13107;
// 20% dutyCycle = (real_dutyCycle * 65536) / 100
dutyCycle = 13107;

Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.println((float) dutyCycle * 100 / 65536);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);
Serial.print(F("Change PWM DutyCycle to (%) "));
Serial.println((float) dutyCycle * 100 / 65536);
PWM_Instance->setPWM_Int(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);
}
Loading

0 comments on commit 22230c2

Please sign in to comment.