Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Feb 4, 2023. It is now read-only.
/STM32_PWMPublic archive

This wrapper library enables you to use Hardware-based PWM on STM32F/L/H/G/WB/MP1 boards to create and output PWM to pins. The most important feature is they're purely hardware-based PWM channels. Therefore, their executions are very precise and not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mi…

License

NotificationsYou must be signed in to change notification settings

khoih-prog/STM32_PWM

Repository files navigation

arduino-library-badgeGitHub releaseGitHubcontributions welcomeGitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need thisSTM32_PWM library

Features

Thiswrapper library enables you to use Hardware-based PWM on STM32F/L/H/G/WB/MP1 boards to create and output PWM to pins.

This library is using thesame or similar functions as other FastPWM libraries, as follows, to enable you toport your PWM code easily between platforms

  1. RP2040_PWM
  2. AVR_PWM
  3. megaAVR_PWM
  4. ESP32_FastPWM
  5. SAMD_PWM
  6. SAMDUE_PWM
  7. nRF52_PWM
  8. Teensy_PWM
  9. ATtiny_PWM
  10. Dx_PWM
  11. Portenta_H7_PWM
  12. MBED_RP2040_PWM
  13. nRF52_MBED_PWM
  14. STM32_PWM

The most important feature is they're purely hardware-based PWM channels. Therefore, their executions arevery precise and not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.

These hardware PWM channels still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other ISR-based or software-based PWM using millis() or micros(). That's necessary if you need to measure some data requiring very high frequency and much better accuracy.

ThePWMs_Array_Complex example will demonstrate the nearly perfect accuracy, compared to software timers, by printing the actual period / duty-cycle inmicrosecs of each of PWM-channels.

ThePWM_Multi_Args will demonstrate the usage of multichannel PWM using multiple Hardware Timers. The 4 independent Hardware Timers are usedto control 4 different PWM outputs, with totally independent frequencies and dutycycles.

Being hardware-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services.

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.

You'll seesoftware-based SimpleTimer is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task in loop(), using delay() function as an example. The elapsed time then is very unaccurate


Why using hardware-based PWM is the best

Imagine you have a system with amission-critical function, controlling a self-driving machine or robot or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function isblocking the loop() or setup().

So your functionmight not be executed, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use hardware-based PWM to control the PWM motors.

These hardware-based PWM channels, still work even if other functions are blocking. Moreover, they are much moreprecise (certainly depending on clock frequency accuracy) than other ISR-based or software-based PWM using millis() or micros(). That's necessary if you need to measure some data requiring very high accuracy.

Functions using normal software-based PWM, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.


Currently supported Boards

  1. STM32F/L/H/G/WB/MP1 boards such as NUCLEO_H743ZI2, NUCLEO_L552ZE_Q, NUCLEO_F767ZI, BLUEPILL_F103CB, etc., usingArduino Core for STM32

Important Notes about ISR

  1. Inside the attached function,delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare asvolatile any variables that you modify within the attached function.

  2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino.GitHub release
  2. Arduino Core for STM32 v2.4.0+ for STM32F/L/H/G/WB/MP1 boards.GitHub release
  3. To use with certain example


Installation

Use Arduino Library Manager

The best and easiest way is to useArduino Library Manager. Search forSTM32_PWM, then select / install the latest version.You can also use this linkarduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate toSTM32_PWM page.
  2. Download the latest releaseSTM32_PWM-main.zip.
  3. Extract the zip file toSTM32_PWM-main directory
  4. Copy wholeSTM32_PWM-main folder to Arduino libraries' directory such as~/Arduino/libraries/.

VS Code & PlatformIO

  1. InstallVS Code
  2. InstallPlatformIO
  3. InstallSTM32_PWM library by usingLibrary Manager. Search forSTM32_PWM inPlatform.io Author's Libraries
  4. Use includedplatformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples atProject Configuration File


More useful Information about STM32 Timers

The Timers of STM32s are numerous, yet very sophisticated and powerful.

In general, across the STM32 microcontrollers families, the timer peripherals that have the same name also have the same features set, but there are a few exceptions.

The general purpose timers embedded by the STM32 microcontrollers share the same backbone structure; they differ only on the level of features embedded by a given timer peripheral.

The level of features integration for a given timer peripheral is decided based on the applications field that it targets.

The timer peripherals can be classified as:

• Advanced-configuration timers like TIM1 and TIM8 among others.• General-purpose configuration timers like TIM2 and TIM3 among others• Lite-configuration timers like TIM9, TIM10, TIM12 and TIM16 among others• Basic-configuration timers like TIM6 and TIM7 among others.

More information can be found atEmbedded-Lab STM32 TIMERS

To be sure which Timer is available for the board you're using, check the Core Package's related files. For example, forSTM32 using STM32H747XI, check this file:

  1. ~/.arduino15/packages/STM32/hardware/stm32/2.0.0/system/Drivers/CMSIS/Device/ST/STM32H7xx/Include/stm32h7xx.h

The information will be as follows:

typedefstruct{  __IOuint32_t CR1;/*!< TIM control register 1,                   Address offset: 0x00*/  __IOuint32_t CR2;/*!< TIM control register 2,                   Address offset: 0x04*/  __IOuint32_t SMCR;/*!< TIM slave mode control register,          Address offset: 0x08*/  __IOuint32_t DIER;/*!< TIM DMA/interrupt enable register,        Address offset: 0x0C*/  __IOuint32_t SR;/*!< TIM status register,                      Address offset: 0x10*/  __IOuint32_t EGR;/*!< TIM event generation register,            Address offset: 0x14*/  __IOuint32_t CCMR1;/*!< TIM capture/compare mode register 1,      Address offset: 0x18*/  __IOuint32_t CCMR2;/*!< TIM capture/compare mode register 2,      Address offset: 0x1C*/  __IOuint32_t CCER;/*!< TIM capture/compare enable register,      Address offset: 0x20*/  __IOuint32_t CNT;/*!< TIM counter register,                     Address offset: 0x24*/  __IOuint32_t PSC;/*!< TIM prescaler,                            Address offset: 0x28*/  __IOuint32_t ARR;/*!< TIM auto-reload register,                 Address offset: 0x2C*/  __IOuint32_t RCR;/*!< TIM repetition counter register,          Address offset: 0x30*/  __IOuint32_t CCR1;/*!< TIM capture/compare register 1,           Address offset: 0x34*/  __IOuint32_t CCR2;/*!< TIM capture/compare register 2,           Address offset: 0x38*/  __IOuint32_t CCR3;/*!< TIM capture/compare register 3,           Address offset: 0x3C*/  __IOuint32_t CCR4;/*!< TIM capture/compare register 4,           Address offset: 0x40*/  __IOuint32_t BDTR;/*!< TIM break and dead-time register,         Address offset: 0x44*/  __IOuint32_t DCR;/*!< TIM DMA control register,                 Address offset: 0x48*/  __IOuint32_t DMAR;/*!< TIM DMA address for full transfer,        Address offset: 0x4C*/uint32_t      RESERVED1;/*!< Reserved, 0x50*/  __IOuint32_t CCMR3;/*!< TIM capture/compare mode register 3,      Address offset: 0x54*/  __IOuint32_t CCR5;/*!< TIM capture/compare register5,            Address offset: 0x58*/  __IOuint32_t CCR6;/*!< TIM capture/compare register6,            Address offset: 0x5C*/  __IOuint32_t AF1;/*!< TIM alternate function option register 1, Address offset: 0x60*/  __IOuint32_t AF2;/*!< TIM alternate function option register 2, Address offset: 0x64*/  __IOuint32_t TISEL;/*!< TIM Input Selection register,             Address offset: 0x68*/} TIM_TypeDef;

and

#definePERIPH_BASE0x40000000UL/*!< Base address of : AHB/ABP Peripherals/*!< Peripheral memory map*/#defineAPB1PERIPH_BASE        PERIPH_BASE/*!< APB1 peripherals*//*!< D2_APB1PERIPH peripherals*/#defineTIM2_BASE             (D2_APB1PERIPH_BASE +0x0000UL)#defineTIM3_BASE             (D2_APB1PERIPH_BASE +0x0400UL)#defineTIM4_BASE             (D2_APB1PERIPH_BASE +0x0800UL)#defineTIM5_BASE             (D2_APB1PERIPH_BASE +0x0C00UL)#defineTIM6_BASE             (D2_APB1PERIPH_BASE +0x1000UL)#defineTIM7_BASE             (D2_APB1PERIPH_BASE +0x1400UL)#defineTIM12_BASE            (D2_APB1PERIPH_BASE +0x1800UL)#defineTIM13_BASE            (D2_APB1PERIPH_BASE +0x1C00UL)#defineTIM14_BASE            (D2_APB1PERIPH_BASE +0x2000UL)/*!< APB2 peripherals*/#defineTIM1_BASE             (D2_APB2PERIPH_BASE +0x0000UL)#defineTIM8_BASE             (D2_APB2PERIPH_BASE +0x0400UL)...#defineTIM9_BASE             (APB2PERIPH_BASE +0x4000UL)#defineTIM10_BASE            (APB2PERIPH_BASE +0x4400UL)#defineTIM11_BASE            (APB2PERIPH_BASE +0x4800UL)...#defineTI15_BASE            (D2_APB2PERIPH_BASE +0x4000UL)#defineTIM16_BASE            (D2_APB2PERIPH_BASE +0x4400UL)#defineTIM17_BASE            (D2_APB2PERIPH_BASE +0x4800UL)...#defineHRTIM1_BASE           (D2_APB2PERIPH_BASE +0x7400UL)#defineHRTIM1_TIMA_BASE      (HRTIM1_BASE +0x00000080UL)#defineHRTIM1_TIMB_BASE      (HRTIM1_BASE +0x00000100UL)#defineHRTIM1_TIMC_BASE      (HRTIM1_BASE +0x00000180UL)#defineHRTIM1_TIMD_BASE      (HRTIM1_BASE +0x00000200UL)#defineHRTIM1_TIME_BASE      (HRTIM1_BASE +0x00000280UL)#defineHRTIM1_COMMON_BASE    (HRTIM1_BASE +0x00000380UL)...#defineTIM2                ((TIM_TypeDef *) TIM2_BASE)#defineTIM3                ((TIM_TypeDef *) TIM3_BASE)#defineTIM4                ((TIM_TypeDef *) TIM4_BASE)#defineTIM5                ((TIM_TypeDef *) TIM5_BASE)#defineTIM6                ((TIM_TypeDef *) TIM6_BASE)#defineTIM7                ((TIM_TypeDef *) TIM7_BASE)#defineTIM13               ((TIM_TypeDef *) TIM13_BASE)#defineTIM14               ((TIM_TypeDef *) TIM14_BASE)...#defineTIM1                ((TIM_TypeDef *) TIM1_BASE)#defineTIM8                ((TIM_TypeDef *) TIM8_BASE)...#defineTIM12               ((TIM_TypeDef *) TIM12_BASE)#defineTIM15               ((TIM_TypeDef *) TIM15_BASE)#defineTIM16               ((TIM_TypeDef *) TIM16_BASE)#defineTIM17               ((TIM_TypeDef *) TIM17_BASE)...#defineHRTIM1              ((HRTIM_TypeDef *) HRTIM1_BASE)#defineHRTIM1_TIMA         ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMA_BASE)#defineHRTIM1_TIMB         ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMB_BASE)#defineHRTIM1_TIMC         ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMC_BASE)#defineHRTIM1_TIMD         ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMD_BASE)#defineHRTIM1_TIME         ((HRTIM_Timerx_TypeDef *) HRTIM1_TIME_BASE)#defineHRTIM1_COMMON       ((HRTIM_Common_TypeDef *) HRTIM1_COMMON_BASE)

Available Timers for STM32

This is the temporary list for STM32 Timers which can be used. The available Timers certainly depends on they are being used for other purpose (core, application, libraries, etc.) or not. You have to exhausively test yourself to be sure.

1. OK to use

TIM1, TIM4, TIM7, TIM8, TIM12, TIM13, TIM14, TIM15, TIM16, TIM17

2. Not exist

TIM9, TIM10, TIM11. Only for STM32F2, STM32F4 or STM32L1

3.Not declared

TIM18, TIM19, TIM20, TIM21, TIM22

3. Not OK => conflict or crash

TIM2, TIM3, TIM5, TIM6



Usage

Before using any Timer for a PWM channel, you have to make sure the Timer has not been used by any other purpose.

1. Init Hardware Timer

// Automatically retrieve TIM instance and channel associated to pin// This is used to be compatible with all STM32 series automatically.TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pinNameToUse, PinMap_PWM);

2. Set PWM Frequency, dutycycle, attach irqCallbackStartFunc and irqCallbackStopFunc functions

voidPeriodCallback(){}voidsetup(){  ....    MyTim->setPWM(channel, pins, freq, dutyCycle, PeriodCallback);}


Examples:

  1. PWM_Multi
  2. PWM_Multi_Args
  3. PWMs_Array_Complex
  4. PWM_StepperControlNew


// Use with Stepper-Motor driver, such as TMC2209
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
defined(STM32WB) || defined(STM32MP1) || defined(STM32L5))
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP32_PWM.h"
// _PWM_LOGLEVEL_ from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define_PWM_LOGLEVEL_4
#defineUSING_MICROS_RESOLUTIONtrue//false
#include"STM32_PWM.h"
/////////////////////////////////////////////////
// Change the pin according to your STM32 board. There is no single definition for all boards.
#definepin0 PA0
#definepin1 D1
#definepin2 D2
#definepin3 D3
#definepin4 D4
#definepin5 D5
#definepin6 D6
#definepin7 D7
#definepin8 D8
#definepin9 D9
#definepin10 D10
#definepin11 D11
#definepin12 D12
#definepin13 D13
#definepin14 D14
#definepin15 D15
#definepin16 D16
//////////////////////////////////////////////////////
// Change the pin according to your STM32 board. There is no single definition for all boards.
#if ( defined(STM32F1) && ( defined(ARDUINO_BLUEPILL_F103CB) || defined(ARDUINO_BLUEPILL_F103C8) ) )
#warning Using BLUEPILL_F103CB / BLUEPILL_F103C8 pins
// For F103CB => pin0, pin4, pin10 ============>> TimerIndex = 1, 2, 0
#defineSTEP_PIN pin3
#elif ( defined(STM32F7) && defined(ARDUINO_NUCLEO_F767ZI) )
#warning Using NUCLEO_F767ZI pins
// For F767ZI => pin0, pin3, pin9/10 ============>> TimerIndex = 1, 0, 3
#defineSTEP_PIN pin3
#elif ( defined(STM32L5) && defined(ARDUINO_NUCLEO_L552ZE_Q) )
#warning Using NUCLEO_L552ZE_Q pins
// For NUCLEO_L552ZE_Q => pin0, pin3, pin9/10 ============>> TimerIndex = 1, 0, 3
#defineSTEP_PIN pin3
#elif ( defined(STM32H7) && defined(ARDUINO_NUCLEO_H743ZI2) )
#warning Using NUCLEO_H743ZI2 pins
// For NUCLEO_L552ZE_Q => pin0, pin3, pin9/10 ============>> TimerIndex = 1, 0, 3
#defineSTEP_PIN pin3
#else
// For ??? => pin0, pin3, pin9/10 ============>> TimerIndex = 1, 0, 3
#defineSTEP_PIN pin3
#endif
//////////////////////////////////////////////////////
#defineDIR_PIN pin1
TIM_TypeDef *stepperTimer;
HardwareTimer *stepper;
uint32_t channel;
int previousSpeed =0;
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
// For example, 28BYJ-48 Stepper Motor (https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/) has 32 Steps/Rev
// Speed = 640 Hz => Stepper RPM = (640 * 60 / 32) = 1200 RPM
voidsetSpeed(int speed)
{
// Do nothing if same speed
if (speed == previousSpeed)
return;
Serial.print(F("setSpeed ="));
Serial.println(speed);
// Create new instance for new speed
if (stepper)
delete stepper;
stepper =newHardwareTimer(stepperTimer);
if (speed ==0)
{
// Use DC = 0 to stop stepper
stepper->setPWM(channel, STEP_PIN,500,0,nullptr);
}
else
{
// Set the frequency of the PWM output and a duty cycle of 50%
digitalWrite(DIR_PIN, (speed <0));
stepper->setPWM(channel, STEP_PIN,abs(speed),50,nullptr);
}
previousSpeed = speed;
}
voidinitPWM(uint32_t step_pin)
{
// Using pin = PA0, PA1, etc.
PinName pinNameToUse =digitalPinToPinName(step_pin);
// Automatically retrieve TIM instance and channel associated to pin
// This is used to be compatible with all STM32 series automatically.
stepperTimer = (TIM_TypeDef *)pinmap_peripheral(pinNameToUse, PinMap_PWM);
if (stepperTimer !=nullptr)
{
uint8_t timerIndex =get_timer_index(stepperTimer);
// pin => 0, 1, etc
channel =STM_PIN_CHANNEL(pinmap_function( pinNameToUse, PinMap_PWM));
Serial.print("stepperTimer = 0x");
Serial.print( (uint32_t) stepperTimer, HEX);
Serial.print(", channel =");
Serial.print(channel);
Serial.print(", TimerIndex =");
Serial.print(get_timer_index(stepperTimer));
Serial.print(", PinName =");
Serial.println( pinNameToUse );
stepper =newHardwareTimer(stepperTimer);
// SetPWM object and passed just a random frequency of 500 steps/s
// The Stepper RPM will be ( speed * 60 ) / steps-per-revolution
// The duty cycle is how you turn the motor on and off
previousSpeed =500;
stepper->setPWM(channel, step_pin, previousSpeed,0,nullptr);
}
else
{
Serial.println("ERROR => Wrong pin, You have to select another one. Skip NULL stepperTimer");
}
}
voidsetup()
{
pinMode(STEP_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, LOW);
Serial.begin(115200);
while (!Serial &&millis() <5000);
delay(100);
Serial.print(F("\nStarting PWM_StepperControl on"));
Serial.println(BOARD_NAME);
Serial.println(STM32_PWM_VERSION);
initPWM(STEP_PIN);
}
voidloop()
{
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
setSpeed(2000);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
// Reversing
setSpeed(-1000);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
}



Debug Terminal Output Samples

1. PWMs_Array_Complex on NUCLEO_F767ZI

The following is the sample terminal output when running examplePWMs_Array_Complex onNUCLEO_F767ZI to demonstrate the accuracy of Hardware-based PWM,especially when system is very busy.

Starting PWMs_Array_Complex on NUCLEO_F767ZISTM32_PWM v1.0.1Index =0, Instance =0x40000000, channel =1, TimerIndex =1, PinName =0Index =1, Instance =0x40010000, channel =3, TimerIndex =0, PinName =77Index =2, Instance =0x40000800, channel =4, TimerIndex =3, PinName =63SimpleTimer (ms): 2000, us : 12025001, Dus : 10019423PWM Channel : 0100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 22058001, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 32091001, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 42124001, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 52157001, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 49.93SimpleTimer (ms): 2000, us : 62190001, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00

2. PWMs_Array_Complex on NUCLEO_H743ZI2

The following is the sample terminal output when running examplePWMs_Array_Complex onNUCLEO_H743ZI2 to demonstrate the accuracy of Hardware-based PWM,especially when system is very busy.

Starting PWMs_Array_Complex on NUCLEO_H743ZI2STM32_PWM v1.0.1Index =0, Instance =0x40000000, channel =1, TimerIndex =1, PinName =0Index =1, Instance =0x40010000, channel =3, TimerIndex =0, PinName =77Index =2, Instance =0x40000800, channel =4, TimerIndex =3, PinName =63SimpleTimer (ms): 2000, us : 12025000, Dus : 10019435PWM Channel : 0100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 22058000, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 32091000, Dus : 10033000PWM Channel : 0100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00

3. PWMs_Array_Complex on NUCLEO_L552ZE_Q

The following is the sample terminal output when running examplePWMs_Array_Complex onNUCLEO_L552ZE_Q to demonstrate the accuracy of Hardware-based PWM,especially when system is very busy.

Starting PWMs_Array_Complex on NUCLEO_L552ZE_QSTM32_PWM v1.0.1Index =0, Instance =0x40000000, channel =1, TimerIndex =1, PinName =0Index =1, Instance =0x40012C00, channel =3, TimerIndex =0, PinName =77Index =2, Instance =0x40000800, channel =4, TimerIndex =3, PinName =63SimpleTimer (ms): 2000, us : 12026006, Dus : 10020290PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 22060006, Dus : 10034000PWM Channel : 0100000, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 32094006, Dus : 10034000PWM Channel : 0100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 20, actual : 20.00PWM Channel : 150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 22000, programmed Period (us): 2000, actual : 1999, programmed DutyCycle : 50, actual : 50.00

4. PWMs_Array_Complex on BLUEPILL_F103CB

The following is the sample terminal output when running examplePWMs_Array_Complex onBLUEPILL_F103CB to demonstrate the accuracy of Hardware-based PWM,especially when system is very busy.

Starting PWMs_Array_Complex on BLUEPILL_F103CBSTM32_PWM v1.0.1Using pin =0,1, etc => Index =0, Instance =0x40000000, channel =1, TimerIndex =1, PinName =0Using pin =0,1, etc => Index =1, Instance =0x40000400, channel =2, TimerIndex =2, PinName =21Using pin =0,1, etc => Index =2, Instance =0x40012C00, channel =3, TimerIndex =0, PinName =10SimpleTimer (ms): 2000, us : 12970007, Dus : 9999926PWM Channel : 0, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 20, actual : 20.00PWM Channel : 1, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 2, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 22971007, Dus : 10001000PWM Channel : 0, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 1, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 2, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 32972007, Dus : 10001000PWM Channel : 0, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 1, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 2, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 42973007, Dus : 10001000PWM Channel : 0, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 1, programmed Period (us): 20000, actual : 19999, programmed DutyCycle : 30, actual : 30.00PWM Channel : 2, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00SimpleTimer (ms): 2000, us : 52974007, Dus : 10001000PWM Channel : 0, programmed Period (us): 50000, actual : 49999, programmed DutyCycle : 20, actual : 20.00PWM Channel : 1, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 30, actual : 30.00PWM Channel : 2, programmed Period (us): 2000, actual : 2000, programmed DutyCycle : 50, actual : 50.00

5. PWM_StepperControl on NUCLEO_F767ZI

The following is the sample terminal output when running examplePWM_StepperControl onNUCLEO_F767ZI to demonstrate how to control Stepper Motor using PWM

Starting PWM_StepperControl on NUCLEO_F767ZISTM32_PWM v1.0.1stepperTimer = 0x40010000, channel = 3, TimerIndex = 0, PinName = 77setSpeed = 2000setSpeed = 0setSpeed = -1000setSpeed = 0setSpeed = 2000setSpeed = 0setSpeed = -1000


Debug

Debug is enabled by default on Serial.

You can also change the debugging level_PWM_LOGLEVEL_ from 0 to 4

// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.#define_PWM_LOGLEVEL_0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to:STM32_PWM issues


TO DO

  1. Search for bug and improvement.
  2. Similar features for remaining Arduino boards

DONE

  1. Basic hardware-based multi-channel PWM forSTM32F/L/H/G/WB/MP1 boards.
  2. Add Table of Contents
  3. Add examplePWM_StepperControl to demo how to control Stepper Motor using PWM


Contributions and Thanks

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

  1. Thanks toPaul van Dinther for proposing new way to use PWM to drive Stepper-Motor inUsing PWM to step a stepper driver #16, leading to v2.0.3
dinther
Paul van Dinther


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed underMIT

Copyright

Copyright (c) 2021- Khoi Hoang

About

This wrapper library enables you to use Hardware-based PWM on STM32F/L/H/G/WB/MP1 boards to create and output PWM to pins. The most important feature is they're purely hardware-based PWM channels. Therefore, their executions are very precise and not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mi…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp