With the help of this class various waveforms to an output pin or LED can be easily generated. It does this by using two hardware modules, namely the SoftwareTimer and the PwmOut module. The SoftwareTimer module controls the sample and/or pulse’s timing, while the PwmOut class controls the amplitude of the signal. PwmOut is a simple class, which uses an implementation of the pwm-interface encapsulating the HAL of the microcontroller.
If the purpose of the application is to flash an LED with the maximum amplitude, the LedBlinking class is better suited because less resources are required (SoftwareTimer module only). If the purpose of the application is to adjust the amplitude (or generally generate square-wave pulses), it is better to use the PwmOut class.
The first step for setting up the LedDimming object is to initialize a timer hardware module and configure one of its channels to an pwm generation mode (usually done in HW configuration, for example Stm32CubeMX). The second step is creating a HAL Pwm‘s object (e.g. Stm32f4Pwm object) referencing the timer handle and the preferred timer channel. This object is then passed to the PwmOut‘s Object Maximum and minimum values of the Pwm‘s object should be initialized via setMinValue(unsigned int minValue) and setMaxValue(unsigned int maxValue) functions, otherwise the default parameters are considered. This PwmOut object and an additional Timer object (see documentation concerning systick callback) are then passed to the LedDimming object’s constructor.
In this example, we consider the hardware timer module TIM2 and its first channel TIM_CHANNEL_1, which has been set to pwm generation mode.
// Inclusion of the predeclared and predefined class in the project. #include "HardwareAbstraction/Stm32/stm32f4pwm.h" #include "HardwareAbstraction/Stm32/stm32f4systick.h" #include "Components/Output/pwmout.h" #include "Components/Output/leddimming.h" #include "main.h" // Hardware handler which is predefined in 'main.c' file. extern TIM_HandleTypeDef htim2; Stm32F4Pwm pwm(htim2, TIM_CHANNEL_1); // Interface class for the timer. semf::PwmOut pwmOut(pwm); semf::TimeBase timebase1ms(semf::Stm32F4SysTick::instance(), true); semf::LedDimming led(pwmOut, timebase1ms, false); led.setBrightness(500); // dutycycle 50%
// Generates a square-wave, with 100ms on time, and the same for off time. led.setBlinking(100); // or // Generates a rectangular-wave, with 100ms on time, and the 200ms off time led.setBlinking(100, 200); // or // Changes the brightness promil from 500 to 1000 led.setBrightness(1000); // or // Generates an sinus-wave with 500ms period or (frequency of 2Hz) led.setSine(500); // or // Generates an triangle-wave with 2000ms period or (frequency of 0.5Hz) // and start at the peak value decreasing afterwards led.setTriangle(2000, 1000); // or // Sets the led constant on led.setOn(); // or // Sets the led constant off led.setOff();