semf
LedDimming

General

Using this class various waveforms on an output pin or LED can easily be generated. This is done by using two semf modules, namely the semf::SoftwareTimer and the semf::app::PwmOut module. The semf::SoftwareTimer module controls the sample and/or pulse’s timing while the semf::app::PwmOut class controls the amplitude of the signal. semf::app::PwmOut is a simple class which uses an implementation of the semf::Pwm interface encapsulating the HAL of the microcontroller.

This class can internally generate sinusoidal and triangular waveforms and approximate these according to a specified sampling frequency.

The waveform’s period duration and the waveform’s type can be configured using the setSine() or setTriangle() function.

If the purpose of the application is to flash an LED with the maximum amplitude the semf::app::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.

Initialization

The first step to set up the semf::LedDimming object is to initialize a semf::app::Timer hardware module and configure one of its channels to a pwm generation mode (usually done in HW configuration, for example Stm32CubeMX). The second step is to create a HAL semf::Pwm 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 semf::Pwm object should be initialized via 'setMinValue()' and 'setMaxValue()' functions otherwise the default parameters are considered. This semf::app::PwmOut object and an additional semf::app::Timer object (see documentation concerning systick callback) are then passed to the semf::app::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 "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%
This class uses PwmOut and a SoftwareTimer in order to generate different waveforms....
Definition: leddimming.h:25
Class for using a PWM as an output with permille value handling.
Definition: pwmout.h:22
A TimeBase is the bridge between e.g. a hardware timer (interrupt service routine) and TickReceiver o...
Definition: timebase.h:24

Usage

// 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();