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

Commit 078551c

Browse files
authored
v1.7.0 to add new PushPull mode
### Releases v1.7.0 1. Add functions `setPWMPushPull_Int`, `setPWMPushPull` and `setPWMPushPull_Period` for the new `PushPull` mode. Check [pwm_set_output_polarity #21](#21) 2. Add these examples to demo the new `PushPull` mode - [PWM_PushPull](https://github.com/khoih-prog/RP2040_PWM/tree/main/examples/PWM_PushPull) - [PWM_PushPull_DynamicDC](https://github.com/khoih-prog/RP2040_PWM/tree/main/examples/PWM_PushPull_DynamicDC) - [PWM_PushPull_DynamicFreq](https://github.com/khoih-prog/RP2040_PWM/tree/main/examples/PWM_PushPull_DynamicFreq) 3. Fix bug of half frequency when using `phaseCorrect` mode 4. Improve `README.md` so that links can be used in other sites, such as `PIO`
1 parent 00f8e95 commit 078551c

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/****************************************************************************************************************************
2+
PWM_PushPull.ino
3+
For RP2040 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
7+
Licensed under MIT license
8+
9+
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
10+
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
11+
by the PWM block
12+
*****************************************************************************************************************************/
13+
14+
#define _PWM_LOGLEVEL_ 4
15+
#include "RP2040_PWM.h"
16+
17+
//creates pwm instance
18+
RP2040_PWM* PWM_Instance;
19+
20+
float frequency = 2000.0f;
21+
float dutyCycle = 30.0f;
22+
23+
// Pins have to be in pairs of the same PWM slice / channel
24+
// Check https://github.com/khoih-prog/RP2040_PWM#programmers-model
25+
// For example: pins 0/1, 2/3, 4/5, 6/7, 8/9, 10/11, 12/13, 14/15, 16/17, 18/19, 20/21, 22/23, 24/25, 26/27, 28/29
26+
27+
#define pinToUseA 18 // PWM1A
28+
#define pinToUseB 19 // PWM1A
29+
30+
void printPWMInfo(RP2040_PWM* PWM_Instance)
31+
{
32+
uint32_t div = PWM_Instance->get_DIV();
33+
uint32_t top = PWM_Instance->get_TOP();
34+
35+
Serial.print("Actual PWM Frequency = ");
36+
Serial.println(PWM_Instance->getActualFreq());
37+
38+
PWM_LOGDEBUG5("TOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance->get_freq_CPU());
39+
}
40+
41+
void setup()
42+
{
43+
Serial.begin(115200);
44+
45+
while (!Serial && millis() < 5000);
46+
47+
delay(100);
48+
49+
Serial.print(F("\nStarting PWM_PushPull on "));
50+
Serial.println(BOARD_NAME);
51+
Serial.println(RP2040_PWM_VERSION);
52+
53+
// Use just one of these 2 for a pair
54+
// assigns pinToUseA, with frequency of 200 Hz and a duty cycle of 0%
55+
PWM_Instance = new RP2040_PWM(pinToUseA, frequency, 0);
56+
// assigns pinToUseB, with frequency of 200 Hz and a duty cycle of 0%
57+
//PWM_Instance = new RP2040_PWM(pinToUseB, frequency, 0);
58+
59+
// pinToUseA always with same polarity, pinToUseB with reversed polarity
60+
//PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
61+
// pinToUseA always with same polarity, pinToUseB shifted 180 deg
62+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
63+
64+
printPWMInfo(PWM_Instance);
65+
}
66+
67+
void loop()
68+
{
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/****************************************************************************************************************************
2+
PWM_PushPull_DynamicDC.ino
3+
For RP2040 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
7+
Licensed under MIT license
8+
9+
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
10+
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
11+
by the PWM block
12+
*****************************************************************************************************************************/
13+
14+
#define _PWM_LOGLEVEL_ 4
15+
#include "RP2040_PWM.h"
16+
17+
//creates pwm instance
18+
RP2040_PWM* PWM_Instance;
19+
20+
float frequency = 2000.0f;
21+
float dutyCycle;
22+
23+
// Pins have to be in pairs of the same PWM slice / channel
24+
// Check https://github.com/khoih-prog/RP2040_PWM#programmers-model
25+
// For example: pins 0/1, 2/3, 4/5, 6/7, 8/9, 10/11, 12/13, 14/15, 16/17, 18/19, 20/21, 22/23, 24/25, 26/27, 28/29
26+
27+
#define pinToUseA 18 // PWM1A
28+
#define pinToUseB 19 // PWM1A
29+
30+
void printPWMInfo(RP2040_PWM* PWM_Instance)
31+
{
32+
uint32_t div = PWM_Instance->get_DIV();
33+
uint32_t top = PWM_Instance->get_TOP();
34+
35+
Serial.print("Actual PWM Frequency = ");
36+
Serial.println(PWM_Instance->getActualFreq());
37+
38+
PWM_LOGDEBUG5("TOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance->get_freq_CPU());
39+
}
40+
41+
void setup()
42+
{
43+
Serial.begin(115200);
44+
45+
while (!Serial && millis() < 5000);
46+
47+
delay(100);
48+
49+
Serial.print(F("\nStarting PWM_PushPull_DynamicDC on "));
50+
Serial.println(BOARD_NAME);
51+
Serial.println(RP2040_PWM_VERSION);
52+
53+
// Use just one of these 2 for a pair
54+
// assigns pinToUseA, with frequency of 200 Hz and a duty cycle of 0%
55+
PWM_Instance = new RP2040_PWM(pinToUseA, frequency, 0);
56+
// assigns pinToUseB, with frequency of 200 Hz and a duty cycle of 0%
57+
//PWM_Instance = new RP2040_PWM(pinToUseB, frequency, 0);
58+
59+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, 0);
60+
}
61+
62+
void loop()
63+
{
64+
dutyCycle = 50.0f;
65+
66+
Serial.print(F("Change PWM DutyCycle to "));
67+
Serial.println(dutyCycle);
68+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
69+
70+
printPWMInfo(PWM_Instance);
71+
72+
delay(10000);
73+
74+
dutyCycle = 10.0f;
75+
76+
Serial.print(F("Change PWM DutyCycle to "));
77+
Serial.println(dutyCycle);
78+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
79+
printPWMInfo(PWM_Instance);
80+
81+
delay(5000);
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/****************************************************************************************************************************
2+
PWM_PushPull_DynamicFreq.ino
3+
For RP2040 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM
7+
Licensed under MIT license
8+
9+
The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency
10+
or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven
11+
by the PWM block
12+
*****************************************************************************************************************************/
13+
14+
#define _PWM_LOGLEVEL_ 4
15+
#include "RP2040_PWM.h"
16+
17+
//creates pwm instance
18+
RP2040_PWM* PWM_Instance;
19+
20+
float frequency = 2000.0f;
21+
float dutyCycle = 30.0f;
22+
23+
// Pins have to be in pairs of the same PWM slice / channel
24+
// Check https://github.com/khoih-prog/RP2040_PWM#programmers-model
25+
// For example: pins 0/1, 2/3, 4/5, 6/7, 8/9, 10/11, 12/13, 14/15, 16/17, 18/19, 20/21, 22/23, 24/25, 26/27, 28/29
26+
27+
#define pinToUseA 18 // PWM1A
28+
#define pinToUseB 19 // PWM1A
29+
30+
void printPWMInfo(RP2040_PWM* PWM_Instance)
31+
{
32+
uint32_t div = PWM_Instance->get_DIV();
33+
uint32_t top = PWM_Instance->get_TOP();
34+
35+
Serial.print("Actual PWM Frequency = ");
36+
Serial.println(PWM_Instance->getActualFreq());
37+
38+
PWM_LOGDEBUG5("TOP =", top, ", DIV =", div, ", CPU_freq =", PWM_Instance->get_freq_CPU());
39+
}
40+
41+
void setup()
42+
{
43+
Serial.begin(115200);
44+
45+
while (!Serial && millis() < 5000);
46+
47+
delay(100);
48+
49+
Serial.print(F("\nStarting PWM_PushPull_DynamicFreq on "));
50+
Serial.println(BOARD_NAME);
51+
Serial.println(RP2040_PWM_VERSION);
52+
53+
// Use just one of these 2 for a pair
54+
// assigns pinToUseA, with frequency of 200 Hz and a duty cycle of 0%
55+
PWM_Instance = new RP2040_PWM(pinToUseA, frequency, 0);
56+
// assigns pinToUseB, with frequency of 200 Hz and a duty cycle of 0%
57+
//PWM_Instance = new RP2040_PWM(pinToUseB, frequency, 0);
58+
59+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, 0);
60+
}
61+
62+
void loop()
63+
{
64+
frequency = 2000.0f;
65+
66+
Serial.print(F("Change PWM Freq to "));
67+
Serial.println(frequency);
68+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
69+
printPWMInfo(PWM_Instance);
70+
71+
delay(5000);
72+
73+
frequency = 1000.0f;
74+
75+
Serial.print(F("Change PWM Freq to "));
76+
Serial.println(frequency);
77+
PWM_Instance->setPWMPushPull(pinToUseA, pinToUseB, frequency, dutyCycle);
78+
printPWMInfo(PWM_Instance);
79+
80+
delay(5000);
81+
}

0 commit comments

Comments
 (0)