No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:aih:lab:stm32pwm
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:edu:zmitac:s2:aih:lab:stm32pwm [2024/01/13 20:28] – ktokarz | en:edu:zmitac:s2:aih:lab:stm32pwm [2024/01/16 09:11] (current) – ktokarz | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| </ | </ | ||
| At the beginning of the code, there is a definition of pin value. Although the P-Nucleo-WB55 has the LED_BUILTIN defined it can't work as the PMW timer output. We have to override it. | At the beginning of the code, there is a definition of pin value. Although the P-Nucleo-WB55 has the LED_BUILTIN defined it can't work as the PMW timer output. We have to override it. | ||
| - | < | + | < |
| // ********************************************************* | // ********************************************************* | ||
| #define LED_BUILTIN D6 //Override of the default LED_BUILTIN | #define LED_BUILTIN D6 //Override of the default LED_BUILTIN | ||
| Line 21: | Line 21: | ||
| </ | </ | ||
| In the setup part of the code, the program automatically retrieves the timer instance and PWM channel number connected to the previously defined pin. | In the setup part of the code, the program automatically retrieves the timer instance and PWM channel number connected to the previously defined pin. | ||
| - | < | + | < |
| TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), | TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), | ||
| uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), | uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), | ||
| </ | </ | ||
| While having information on the timer instance and channel number it instantiates the HardwareTimer object. | While having information on the timer instance and channel number it instantiates the HardwareTimer object. | ||
| - | < | + | < |
| | | ||
| </ | </ | ||
| The setup function oi finished with starting the timer to operate in PMW mode and configuring the PWM signal with frequency and duty cycle. | The setup function oi finished with starting the timer to operate in PMW mode and configuring the PWM signal with frequency and duty cycle. | ||
| - | < | + | < |
| MyTim-> | MyTim-> | ||
| </ | </ | ||
| It is possible to modify the duty cycle by changing the current value of the capture and compare register for specified channel. To do it we need to use the function. | It is possible to modify the duty cycle by changing the current value of the capture and compare register for specified channel. To do it we need to use the function. | ||
| - | < | + | < |
| MyTim-> | MyTim-> | ||
| </ | </ | ||
| - | The program that controls all three LEDs has two files shown below PwmLedTest.h and PwmLedTest.cpp. | ||
| - | ===== PwmLedTest.h ===== | ||
| - | < | ||
| - | #include " | ||
| - | #include < | ||
| - | #ifndef _PWMLED_TestLib | ||
| - | #define _PWMLED_TestLib | ||
| - | |||
| - | // Pins definition for RGB LED | ||
| - | #define LED_PWM_RED | ||
| - | #define LED_PWM_GREEN D6 //Arduino numberring D6, STM numberring PA_8 | ||
| - | #define LED_PWM_BLUE | ||
| - | |||
| - | // PWM variables definitions | ||
| - | HardwareTimer *MyTimLED; | ||
| - | uint32_t channelR, channelG, channelB; //RGB cahnnels | ||
| - | |||
| - | #endif | ||
| - | </ | ||
| - | ===== PwmLedTest.cpp ===== | ||
| - | < | ||
| - | #include " | ||
| - | | ||
| - | void PwmLedSetup() | ||
| - | { | ||
| - | //Create Timer instance type based on the servo pin (the same timer is used for fan). | ||
| - | TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(LED_PWM_RED), | ||
| - | | ||
| - | //Define two separate channels for servo and fan | ||
| - | channelR = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_RED), | ||
| - | channelG = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_GREEN), | ||
| - | channelB = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(LED_PWM_BLUE), | ||
| - | |||
| - | // Instantiate HardwareTimer object. Thanks to ' | ||
| - | MyTimLED = new HardwareTimer(Instance); | ||
| - | |||
| - | // Configure and start PWM | ||
| - | MyTimLED-> | ||
| - | MyTimLED-> | ||
| - | MyTimLED-> | ||
| - | } | ||
| - | |||
| - | void fRGBLEDSet(int red, int green, int blue) | ||
| - | { | ||
| - | MyTimLED-> | ||
| - | MyTimLED-> | ||
| - | MyTimLED-> | ||
| - | } | ||
| - | </ | ||
en/edu/zmitac/s2/aih/lab/stm32pwm.1705174124.txt.gz · Last modified: by ktokarz
