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

Commit 0da0fe2

Browse files
authored
v1.0.1 to add PWM_StepperControl example
### Releases v1.0.1 1. Add example [PWM_StepperControl](https://github.com/khoih-prog/MBED_RP2040_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM.
1 parent 97018ae commit 0da0fe2

File tree

1 file changed

+19
-229
lines changed

1 file changed

+19
-229
lines changed

README.md

Lines changed: 19 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
* [Examples](#examples)
3434
* [ 1. PWM_Multi](examples/PWM_Multi)
3535
* [ 2. PWM_Single](examples/PWM_Single)
36-
* [ 3. multiFileProject](examples/multiFileProject). **New**
36+
* [ 3. multiFileProject](examples/multiFileProject)
37+
* [ 4. PWM_StepperControl](examples/PWM_StepperControl). **New**
3738
* [Example PWM_Multi](#example-PWM_Multi)
3839
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
3940
* [1. PWM_Single on RaspberryPi Pico](#1-PWM_Single-on-RaspberryPi-Pico)
@@ -99,7 +100,7 @@ This non-being-blocked important feature is absolutely necessary for mission-cri
99100
## Prerequisites
100101

101102
1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest)
102-
2. [`Arduino mbed_rp2040 core 3.4.1+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) RP2040-based boards, such as **Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest)
103+
2. [`Arduino mbed_rp2040 core 3.5.4+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) RP2040-based boards, such as **Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest)
103104
---
104105
---
105106

@@ -224,238 +225,18 @@ if (pwm)
224225

225226
1. [PWM_Multi](examples/PWM_Multi)
226227
2. [PWM_Single](examples/PWM_Single)
227-
3. [**multiFileProject**](examples/multiFileProject) **New**
228+
3. [**multiFileProject**](examples/multiFileProject)
229+
4. [**PWM_StepperControl**](examples/PWM_StepperControl) **New**
228230

229231

230232
---
231233
---
232234

233235
### Example [PWM_Multi](examples/PWM_Multi)
234236

235-
```cpp
236-
#if !( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
237-
defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED)
238-
#error This code is intended to run on the MBED RP2040 platform! Please check your Tools->Board setting.
239-
#endif
240-
241-
#define _PWM_LOGLEVEL_ 1
242-
243-
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
244-
#include "MBED_RP2040_PWM.h"
245-
246-
#define LED_ON LOW
247-
#define LED_OFF HIGH
248-
249-
#ifndef LED_BUILTIN
250-
#define LED_BUILTIN 25
251-
#endif
252-
253-
#ifndef LED_BLUE
254-
#define LED_BLUE 10
255-
#endif
256-
257-
#ifndef LED_RED
258-
#define LED_RED 11
259-
#endif
260-
261-
// Valid pins from 0-29 (GP0-GP29)
262-
uint32_t pins[] = { 12, 13, 14, 15 };
263-
264-
#define NUM_OF_PINS ( sizeof(pins) / sizeof(uint32_t) )
265-
266-
float dutyCycle[] = { 50.0f, 50.0f, 50.0f, 50.0f };
267-
268-
float freq[] = { 1000.0f, 2500.0f, 4000.0f, 5000.0f };
269-
270-
float curDutyCycle[] = { 50.0f, 50.0f, 50.0f, 50.0f };
271-
272-
float curFreq[] = { 1000.0f, 2500.0f, 4000.0f, 5000.0f };
237+
https://github.com/khoih-prog/MBED_RP2040_PWM/blob/97018ae974e1f1dab9e77b4967ee7726b60b74c1/examples/PWM_Multi/PWM_Multi.ino#L10-L237
273238

274-
mbed::PwmOut* pwm[] = { NULL, NULL, NULL, NULL };
275239

276-
void startAllPWM()
277-
{
278-
digitalWrite(LED_BUILTIN, LED_ON);
279-
digitalWrite(LED_BLUE, LED_OFF);
280-
digitalWrite(LED_RED, LED_OFF);
281-
282-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
283-
{
284-
PWM_LOGERROR7("Freq = ", freq[index], ", \tDutyCycle % = ", dutyCycle[index], ", \tDutyCycle = ", dutyCycle[index] / 100, ", \tPin = GP", pins[index]);
285-
286-
// setPWM(mbed::PwmOut* &pwm, pinsize_t pin, float frequency, float dutyCycle)
287-
setPWM(pwm[index], pins[index], freq[index], dutyCycle[index]);
288-
}
289-
}
290-
291-
void restoreAllPWM()
292-
{
293-
digitalWrite(LED_BUILTIN, LED_ON);
294-
digitalWrite(LED_BLUE, LED_OFF);
295-
digitalWrite(LED_RED, LED_OFF);
296-
297-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
298-
{
299-
curFreq[index] = freq[index];
300-
curDutyCycle[index] = dutyCycle[index];
301-
302-
// setPWM(mbed::PwmOut* &pwm, pinsize_t pin, float frequency, float dutyCycle)
303-
setPWM(pwm[index], pins[index], freq[index], dutyCycle[index]);
304-
}
305-
}
306-
307-
void changeAllPWM()
308-
{
309-
digitalWrite(LED_BUILTIN, LED_OFF);
310-
digitalWrite(LED_BLUE, LED_ON);
311-
digitalWrite(LED_RED, LED_OFF);
312-
313-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
314-
{
315-
curFreq[index] = freq[index] * 2;
316-
curDutyCycle[index] = dutyCycle[index] / 2;
317-
318-
// setPWM(mbed::PwmOut* &pwm, pinsize_t pin, float frequency, float dutyCycle)
319-
setPWM(pwm[index], pins[index], curFreq[index], curDutyCycle[index]);
320-
}
321-
}
322-
323-
void stopAllPWM()
324-
{
325-
digitalWrite(LED_BUILTIN, LED_OFF);
326-
digitalWrite(LED_BLUE, LED_OFF);
327-
digitalWrite(LED_RED, LED_ON);
328-
329-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
330-
{
331-
curFreq[index] = 1000.0f;
332-
curDutyCycle[index] = 0.0f;
333-
334-
//stopPWM(mbed::PwmOut* &pwm, pinsize_t pin)
335-
stopPWM(pwm[index], pins[index]);
336-
}
337-
}
338-
339-
void printLine()
340-
{
341-
Serial.println(F("\n========================================================="));
342-
}
343-
344-
void printPulseWidth()
345-
{
346-
static uint32_t num = 0;
347-
348-
if (num++ % 50 == 0)
349-
{
350-
printLine();
351-
352-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
353-
{
354-
Serial.print(F("PW (us) ")); Serial.print(index); Serial.print(F("\t"));
355-
}
356-
357-
printLine();
358-
}
359-
360-
if (num > 1)
361-
{
362-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
363-
{
364-
if (pwm[index])
365-
{
366-
Serial.print(getPulseWidth_uS(pwm[index])); Serial.print(F("\t\t"));
367-
}
368-
}
369-
370-
Serial.println();
371-
}
372-
}
373-
374-
#define PRINT_INTERVAL 10000L
375-
#define CHANGE_INTERVAL 20000L
376-
377-
void check_status()
378-
{
379-
static unsigned long checkstatus_timeout = 0;
380-
static unsigned long changePWM_timeout = 0;
381-
382-
static bool PWM_orig = true;
383-
static uint32_t count = 0;
384-
385-
// Print every PRINT_INTERVAL (10) seconds.
386-
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
387-
{
388-
printPulseWidth();
389-
checkstatus_timeout = millis() + PRINT_INTERVAL;
390-
}
391-
392-
if ( (millis() > changePWM_timeout) && (millis() > CHANGE_INTERVAL) )
393-
{
394-
395-
if (PWM_orig)
396-
{
397-
if (count++ %2 == 0)
398-
{
399-
Serial.println("Stop all PWM");
400-
stopAllPWM();
401-
}
402-
else
403-
{
404-
Serial.println("Change all PWM");
405-
406-
changeAllPWM();
407-
408-
PWM_orig = !PWM_orig;
409-
}
410-
}
411-
else
412-
{
413-
Serial.println("Restore all PWM");
414-
415-
restoreAllPWM();
416-
417-
PWM_orig = !PWM_orig;
418-
}
419-
420-
changePWM_timeout = millis() + CHANGE_INTERVAL;
421-
}
422-
}
423-
424-
void setup()
425-
{
426-
pinMode(LED_BUILTIN, OUTPUT);
427-
pinMode(LED_BLUE, OUTPUT);
428-
pinMode(LED_RED, OUTPUT);
429-
430-
digitalWrite(LED_BUILTIN, LED_OFF);
431-
digitalWrite(LED_BLUE, LED_OFF);
432-
digitalWrite(LED_RED, LED_OFF);
433-
434-
for (uint8_t index = 0; index < NUM_OF_PINS; index++)
435-
{
436-
pinMode(pins[index], OUTPUT);
437-
digitalWrite(pins[index], LOW);
438-
}
439-
440-
Serial.begin(115200);
441-
while (!Serial);
442-
443-
delay(100);
444-
445-
Serial.print(F("\nStarting PWM_Multi on ")); Serial.println(BOARD_NAME);
446-
Serial.println(MBED_RP2040_PWM_VERSION);
447-
448-
// Automatically retrieve TIM instance and channel associated to pin
449-
// This is used to be compatible with all STM32 series automatically.
450-
451-
startAllPWM();
452-
}
453-
454-
void loop()
455-
{
456-
check_status();
457-
}
458-
```
459240
---
460241
---
461242

@@ -468,7 +249,7 @@ The following is the sample terminal output when running example [PWM_Single](ex
468249

469250
```cpp
470251
Starting PWM_Single on RaspberryPi Pico
471-
MBED_RP2040_PWM v1.0.0
252+
MBED_RP2040_PWM v1.0.1
472253
[PWM] Freq = 5000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 15
473254

474255
============================================
@@ -500,7 +281,7 @@ The following is the sample terminal output when running example [**PWM_Multi**]
500281
501282
```cpp
502283
Starting PWM_Multi on RaspberryPi Pico
503-
MBED_RP2040_PWM v1.0.0
284+
MBED_RP2040_PWM v1.0.1
504285
[PWM] Freq = 1000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = GP12
505286
[PWM] Freq = 2500.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = GP13
506287
[PWM] Freq = 4000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = GP14
@@ -578,8 +359,9 @@ Submit issues to: [MBED_RP2040_PWM issues](https://github.com/khoih-prog/MBED_RP
578359
3. Permit to start, stop, modify, restore PWM settings on-the-fly
579360
4. Optimize library code by using `reference-passing` instead of `value-passing`
580361
5. Use `h-only` style
581-
6. Add functions to read PWM parameters.
582-
362+
6. Add functions to read PWM parameters
363+
7. Use `allman astyle` and add `utils`
364+
8. Add example [PWM_StepperControl](https://github.com/khoih-prog/Portenta_H7_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
583365

584366
---
585367
---
@@ -588,6 +370,14 @@ Submit issues to: [MBED_RP2040_PWM issues](https://github.com/khoih-prog/MBED_RP
588370

589371
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
590372

373+
1. Thanks to [Paul van Dinther](https://github.com/dinther) for proposing new way to use PWM to drive Stepper-Motor in [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16), leading to v1.0.1
374+
375+
<table>
376+
<tr>
377+
<td align="center"><a href="https://github.com/dinther"><img src="https://github.com/dinther.png" width="100px;" alt="dinther"/><br /><sub><b>Paul van Dinther</b></sub></a><br /></td>
378+
</tr>
379+
</table>
380+
591381
---
592382

593383
## Contributing

0 commit comments

Comments
 (0)