An2841 Led Dimming Implemented On stm32 Microcontroller Stmicroelectronics
An2841 Led Dimming Implemented On stm32 Microcontroller Stmicroelectronics
Application note
1 Introduction
This application note deals with the general principles of LED dimming. This note also
focuses on the use of pulse width modulation (PWM) for digital dimming. Its implementation
on the STM32™ microcontroller in combination with the STP24DP05 LED driver is
described in detail and the most common design approaches are compared.
The conclusion helps developers choose the best approach for their LED dimming
application.
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
4 Achieved values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.1 Dimming approach comparison and conclusion . . . . . . . . . . . . . . . . . . . 29
4.1.1 Comparison between interrupt and interrupt + DMA approaches . . . . . 29
4.1.2 Comparison between dynamic preparation, static, and reduced static
lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5 References documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2/31
AN2841 List of tables
List of tables
3/31
List of figures AN2841
List of figures
4/31
AN2841 Description of LED dimming
Step: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 ...
TPWM
AM00341
Using a 4-bit PWM resolution, there can be 16 different light intensity (energy) values.
Concerning color applications, 16 million (256 × 256 × 256) colors can be reached using
tricolor LEDs with an 8-bit dimming resolution for each single color LED.
5/31
Description of LED dimming AN2841
6/31
AN2841 Description of LED dimming
T PWM
SERIAL DATA 1111 1101 0101 0001 0000 1111 or new
data
LEVELS
T SEND_DATA
t
PARALLELDATA
4% duty cycle
Output 1- (LED1)
t
The following equation is used to determine how many LEDs can used in an application with
specific maximum SPI speed and PWM resolution requirements:
Equation 1
f SPI
N= n
2 f PWM
where;
N is the maximum number of LEDs
fSPI is the SPI communication speed
n is the PWM resolution
fPWM is the dimming frequency
7/31
Description of LED dimming AN2841
The outcome of Equation 1 is valid only for the best-case scenario when the data are sent
to the LED driver without any delay between blocks of data. This example, of course, is not
a real one. The values for a real application can only get close to the value determined
above.
5V
3.3 V LED block RGB LED configuration
Temperature
sensor
OE-R/G/B LE
More drivers in cascade…
AM00344
8/31
AN2841 Dimming application using an STM32 microcontroller
This section describes a dimming application that uses an STM32 microcontroller (MCU).
All the computations, results and tests were performed on an evaluation board (reference:
STEVAL-ILL015V1) containing an STM32 MCU running at a clock frequency of 48 MHz.
For more information about the use of this evaluation board and dimming applications,
please refer to user manuals UM0588 and UM0574 listed in Section 5: References
documents.
Figure 5 and Figure 6 provide a brief overview of the STM32 evaluation board.
SPI
AM00345
9/31
Dimming application using an STM32 microcontroller AN2841
The LED brightness value is defined by the user in the LEDmap array. The LEDmap is an
array of 8-bit numbers (when using an 8-bit dimming resolution). Each value is dedicated
to one single LED (Figure 7).
The dimming algorithm converts the LEDmap array into a serial stream. The serial stream
in combination with the LE signal generates requested PWM outputs on the LED drivers
as shown in Figure 8.
Begin
0 .. LEDcount / 8
DATAchar = 0
DATAchar++
–
DATAchar>3
+
depth++
– depth>255
+
AM00346
10/31
AN2841 Dimming application using an STM32 microcontroller
AM00347
11/31
Dimming application using an STM32 microcontroller AN2841
12/31
AN2841 Dimming application using an STM32 microcontroller
Once the brightness data is determined, the repetitive dimming procedure needs only to
take the prepared data and send it to the LED driver.
Figure 11 shows the calculation time of the algorithm.
Figure 11. Static lookup table time consumption with 166 kHz - 333 kHz LE signal
Accurate LE timing and the dimming part running in the application background are
necessary requirements for a successful application. These targets can be achieved using
the following methods:
● Single interrupt
● Single interrupt with DMA
● Interrupt + DMA + interrupt
13/31
Dimming application using an STM32 microcontroller AN2841
Figure 12 shows a timing diagram for a dimming application using this method.
Figure 13 shows the waveforms of the signals output by the MCUs controlling the LED
drivers.
14/31
AN2841 Dimming application using an STM32 microcontroller
depth+=4;
15/31
Dimming application using an STM32 microcontroller AN2841
16/31
AN2841 Dimming application using an STM32 microcontroller
Figure 16 shows the waveform of the outbound signals from the microcontrollers controlling
the LED drivers. An example of the C code implementation of the timer routine is given
below. A detailed description follows the example.
17/31
Dimming application using an STM32 microcontroller AN2841
Figure 16. Real signals: timer interrupt + DMA transfer + DMA interrupt model
Step "1" in Figure 16 is not necessary, but it is used for a better visualization of the tracking
of signals on the oscilloscope.
void SysTickHandler(void)
{
LE_LOW();(Step 1)
LE_HIGH();
depth+=4;
if(depth > 0x400)
depth = 0;
LE_LOW();
}
void DMAChannel3_IRQHandler(void)(Step 5)
{
while(!SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE));(Step 6)
LE_HIGH();(Step 7)
LE_LOW();
DMA_ClearITPendingBit(DMA_IT_GL3);
}
18/31
AN2841 Dimming application using an STM32 microcontroller
In the above example, the waveform and program code are described in the following steps:
1. The generation of the LE after entering the interrupt.
2. Setting the DMA: source address, data length, and Start flag.
3. Data is sent automatically by DMA.
4. Data is sent automatically by DMA.
5. DMA interrupt routine is called when the last byte is sent by DMA.
6. Wait until the data from the SPI TX buffer is delivered to the driver.
(Step 6 can be exchanged with calculations that take the amount of time necessary for
the SPI transfer to be finished).
7. LE generation.
19/31
Dimming application using an STM32 microcontroller AN2841
0 1 1 1 1
1 1 1 1 1
2 1 1 1 1
3 0 1 1 1
4 0 1 1 1
5 0 1 0 1
6 0 1 0 1
… 0 0 0 1
… 0 0 0 1
2n 0 0 0 1
20/31
AN2841 Dimming application using an STM32 microcontroller
21/31
Dimming application using an STM32 microcontroller AN2841
0 0 1 1 1 1 1 1
1 2 1 1 1 1 1 1
2 4 0 1 1 1 1 1
3 6 0 1 0 1 1 1
… … 0 0 0 1 1 1
… … 0 0 0 0 1 1
m(1) x+1 0 0 0 0 0 1
1. "m" is less or equal to number of LEDs = i
Equation 2
TotalTime = tDMAen + toneSPI × LEDs + tLEgen
= 1.25 µs + 55 ns × LEDs + 1 µs
= 2.25 µs + 55 ns × LEDs
The number of LEDs must be rounded up to nearest number commensurable by the length
of an SPI buffer. The SPI buffer length is 8 either or 16 bits in most cases.
22/31
AN2841 Dimming application using an STM32 microcontroller
More examples of TotalTime for different number of LEDs and PWM resolution can be seen
in Table 6.
Table 6 shows the time required for processing one PWM step, one buffer load and one
buffer latch. With an 8-bit PWM resolution, 256 buffer latches are needed; the maximum LE
(Latch Enable) frequency must be divided by 256. These results are shown in Table 7.
.
Table 7 shows the maximum refresh frequencies for controlled LEDs. But the inverse
computation must be used to solve our task. Table 8 shows the inversion of the Table 7
values and gives the required refresh frequency. The LE frequency must be derived and the
SysTick timer preload value (SysTick-preload-value) set to generate the LE pulse by
generating an interrupt at every counter reload event.
Table 7 and Table 8 show that SysTick-reload-frequency will be set between 25.6 kHz and
371.7 kHz according to requested PWM resolution and LED count.
The STM32 SysTick timer uses a reference clock of 72 MHz when set to the maximum
value. The interrupt period for this timer is the SysTick-preload-value multiplied by 1 / (72
000 000).
The reference clock of STM32 evaluation board is set to 48 MHz for the USB to operate
correctly. This value should be taken into the account while making measurements on this
evaluation board as their computation will influence Equation 3 .
The reference clock can be also divided by 8 by using the hardware divider. The divided
reference clock is then 9 MHz. In this case, the interrupt period for this timer is equal to
a preload value multiplied by 1 / (9 000 000).
Preload value = requested SysTick interrupt time / reference period
Preload value = reference frequency / requested SysTick interrupt frequency.
23/31
Dimming application using an STM32 microcontroller AN2841
Equation 3
SysTick-preload-value = 72 000 000 / 25 600 = 2812
The frequency ranges 25.6 kHz and 371.7 kHz required for all possible applications ranges
can be achieved using preload values: 2812 and 194, or with divider by 8: 351 or 24.
SysTick timer setting with no divider on the reference clock:
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
SysTick_SetReload(900);
generates pulses with the period of 12 µs.
SysTick timer setting with the divider by 8 on the reference clock:
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick_SetReload(900);
generates pulses with the period of 100 µs.
The setup algorithm for the SysTick timer configuration has to check if it is possible to set
preload value to keep enough time for processing (TotalTime) together with reaching the
requested refresh frequency:
TotalTime = 2.25 + 0.055 × LEDs
SysTick-reload-time = 1 / SysTick-reload-frequency
TotalTime is the time slot, during which the SPI transfer and all the arbitrations needs to be
achieved. The SysTick time is the time slot which must be respected to reach the required
refresh rate.
Equation 4
SysTick-reload-time > TotalTime
To reach the requested refresh rate and set up the SysTick timer preload value:
SysTick-reload-frequency = requested-refresh-rate × PWMdepth (100 Hz × 256 steps).
24/31
AN2841 Achieved values
4 Achieved values
Dynamic computing takes a lot of time and 120 Hz can be reached only for 32 LEDs. In
addition, no other foreground or background tasks can be performed during computation.
As a consequence, this solution is recommended only for simple applications that use only
a few diodes. For other more complicated applications, it is recommended to use the lookup
table approach using a single interrupt only or interrupt + DMA (if DMA is available).
The LED brightness resolution is limited by the size of the lookup table (Table 10). For every
level of brightness, you need a single string of zeros and ones for all the LEDs.
8 32 256 × 4 = 1 kB 32 × 4 = 128 B
12 32 4096 × 4 = 16 kB 32 × 4 = 128 B
8 128 256 × 16 = 4 kB 128 × 16 = 2 kB
12 128 4096 × 16 = 64 kB 128 × 16 = 2 kB
8 512 256 × 64 = 16 kB 256 × 64 = 16 kB
12 512 4096 × 64 = 256 kB 512 × 64 = 32 kB
1. Values indicates the maximum size.
From Figure 13, the maximum refresh frequency can be computed. The time between two
interrupts is 6 µs. This is a refresh rate for one brightness level, for 32 LEDs, 166 kHz.
Table 11. shows the possible refresh rates for whole brightness depth and different LED
count.
25/31
Achieved values AN2841
26/31
AN2841 Achieved values
27/31
Achieved values AN2841
536 124 62 31 15 8
544 122 61 30 15 8
552 120 60 30 15 8
560 119 59 30 15 7
568 117 59 29 15 7
576 116 58 29 14 7
584 114 57 29 14 7
592 113 56 28 14 7
600 111 56 28 14 7
608 110 55 27 14 7
616 110 55 27 14 7
624 110 55 27 14 7
632 106 53 26 13 7
640 105 52 26 13 7
648 103 52 26 13 6
656 102 51 26 13 6
664 101 51 25 13 6
672 100 50 25 12 6
680 99 49 25 12 6
688 98 49 24 12 6
1. 100 Hz can be reached with very high level of optimization.
Table 12. Refresh frequencies for better optimized code (8- and 12-bit PWM)
LEDs 8-bit PWM freq. [Hz] 12-bit PWM freq. [Hz]
Table 13. Refresh frequencies for better optimized code (10- and 11-bit PWM)
LEDs 10-bit PWM freq. [Hz] 11-bit PWM freq. [Hz]
28/31
AN2841 Achieved values
Table 12 and Table 13 show the results in case you remove the additional LE edge used for
the measurement of the interrupt end and optimize time between two interrupts (used for
SPI in 16-bit mode).
29/31
References documents AN2841
5 References documents
1. User manual UM0588 - Multilayer C library for LED dimming used on systems with SPI
and DMA capabilities
2. User manual UM0574 - STEVAL-ILL015V1 - LED dimmer demonstration board based
on the STP24DP05 and STM32™.
6 Revision history
30/31
AN2841
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
31/31