AN2841
Application note
LED dimming implemented on STM32™ microcontroller
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.
November 2008 |
Rev 1 |
1/31 |
www.st.com
Contents |
AN2841 |
|
|
Contents
1 |
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
1 |
|
2 |
Description of LED dimming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
5 |
|
|
2.1 |
Driving multiple LED applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
6 |
|
2.2 |
Multi-channel dimming using serial-parallel conversion . . . . . . . . . . . . . . . |
6 |
|
2.3 |
Specific dimming application using an STP24DP05 . . . . . . . . . . . . . . . . . |
8 |
3 |
Dimming application using an STM32 microcontroller . . . . . . . . . . . . . |
9 |
|
|
3.1 |
Data transfers and timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
3.1.1 Dynamic data transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.1.2 Static data transfer using lookup tables . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.1.3 Single interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.4 Single interrupt with DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.1.5 Interrupt + DMA + interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
|
3.2 |
Data preparation: redundant and non redundant use of memory . . . . . . |
19 |
|
|
|
3.2.1 |
Full lookup table (redundant) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
19 |
|
|
3.2.2 |
Reduced lookup table (non redundant) . . . . . . . . . . . . . . . . . . . . . . . . . |
20 |
|
3.3 |
Timing: set up of the main dimming timer . . . . . . . . . . . . . . . . . . . . . . . . |
22 |
|
|
|
3.3.1 |
The main system timer, SysTick setting . . . . . . . . . . . . . . . . . . . . . . . . |
22 |
4 |
Achieved values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
25 |
||
|
4.1 |
Dimming approach comparison and conclusion . . . . . . . . . . . . . . . . . . . |
29 |
4.1.1Comparison between interrupt and interrupt + DMA approaches . . . . . 29
4.1.2Comparison 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
Table 1. LEDmap - requested LED brightness configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Table 2. Full lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Table 3. LEDmap - requested LED brightness configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Table 4. LEDmap - reordered requested LED brightness configuration . . . . . . . . . . . . . . . . . . . . . . 22 Table 5. Reduced lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Table 6. TotalTime necessary for requested dimming parameters. . . . . . . . . . . . . . . . . . . . . . . . . . 23 Table 7. Maximum refresh rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Table 8. SysTick-reload-time for 100 Hz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Table 9. Example of values used in Equations 3 and 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Table 10. Lookup table size (full and reduced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Table 11. Refresh frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Table 12. Refresh frequencies for better optimized code (8 and 12-bit PWM) . . . . . . . . . . . . . . . . . . 28 Table 13. Refresh frequencies for better optimized code (10 and 11-bit PWM) . . . . . . . . . . . . . . . . . 28 Table 14. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3/31
List of figures |
AN2841 |
|
|
List of figures
Figure 1. |
PWM signal with 16 steps of possible voltage values . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. 5 |
Figure 2. |
PWM serial-parallel conversion for multiple LED applications . . . . . . . . . . . . . . . . . . . . . . |
. 7 |
Figure 3. |
Example of a specific lighting setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. 7 |
Figure 4. |
Simplified setup of an application using an STP24DP05 . . . . . . . . . . . . . . . . . . . . . . . . . . |
. 8 |
Figure 5. |
STM32 evaluation board setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. 9 |
Figure 6. |
STM32 evaluation board block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
Figure 7. |
Computation of LEDdriverDATAbuffer variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
10 |
Figure 8. |
Conversion of LEDmap array to LEDdriverDATAbuffer . . . . . . . . . . . . . . . . . . . . . . . . |
11 |
Figure 9. |
Dynamic data preparation - time consumption - block diagram . . . . . . . . . . . . . . . . . . . . . |
12 |
Figure 10. |
Dynamic data preparation - time consumption - real signals . . . . . . . . . . . . . . . . . . . . . . . |
12 |
Figure 11. |
Static lookup table time consumption with 166 kHz - 333 kHz LE signal . . . . . . . . . . . . . . |
13 |
Figure 12. |
Timing diagram: Single interrupt method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
14 |
Figure 13. |
Real timing signals for single interrupt method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
14 |
Figure 14. |
Timing diagram: Single interrupt with DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
16 |
Figure 15. |
Timer interrupt + DMA transfer + DMA interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
17 |
Figure 16. |
Real signals: timer interrupt + DMA transfer + DMA interrupt model . . . . . . . . . . . . . . . . . |
18 |
4/31
AN2841 |
Description of LED dimming |
|
|
There are two categories of LED dimming:
●Analog: Uses a linear device to set the current flow through the LED. Advantages: Absence of digital noise; a simple implementation. Limitations: Color shift, low dynamics.
●Digital: Uses a switch to set nominal or zero current flow through the LED. Advantages: Accurate and fast color control.
Limitations: The presence of digital noise and necessity to use a frequency high enough to prevent flickering; a complex implementation.
This document focuses on digital dimming and the use of PWM (pulse width modulation) in common applications.
When using PWM for lighting applications, the following rules must be kept in mind:
●For the LED light to be seen as non-flickering by the human eye, in most cases the PWM frequency must be higher than 100 Hz.
●The LED light brightness depends on the PWM duty cycle. The brightness depends on the resolution of the PWM duty cycle.
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 |
|
|
Displays, indicators and panels increasingly use more and more LEDs. There are three approaches for driving higher numbers of LEDs:
1.Dedicated pin
Each LED is driven by dedicated system (microcontroller, FPGA) pin. Advantages: Simple implementation of dimming algorithm. No conversion. Disadvantages: Unusable for high LED counts. Consumes pins of microcontroller.
2.Serial-parallel conversion
Multiple LEDs are driven by a single system pin.
Advantages: Reduced number of system pins required. High LED count systems can be developed.
Disadvantages: Additional components and external buffers are needed. The data rate on a single pin must be n times greater than that for the single LED out of n, where n is the number of LEDs.
3.Matrix
LEDs are arranged in a square matrix and driven by a complex and sophisticated algorithm that combines serial-parallel conversion with additional decomposition of the serial data stream into the rows and columns by another active component.
This document focuses on serial-parallel conversion applications.
2.2Multi-channel dimming using serial-parallel conversion
The serial-parallel conversion approach is in fact a parallel-serial-parallel conversion since the data is stored in the system memory in parallel format and then is converted to serial format before its transfer over the serial interface. The serial stream is then reconverted to parallel format by the LED driver used to drive each LED. The serial data transfer speed must be n times faster than driving a single LED, where n is the number of parallel driver outputs.
Buffers (LED drivers) used for serial-parallel conversion come generally with the four most important signals:
■SDI: Serial data in
■SDO: Serial data out
■CLK: Clock signal used for loading data in the driver buffer via the SDI (input) and
sending data from the buffer via the SDO (output). It also shifts all data actually present (previously loaded) in the driver.
■ LE: |
Copies (latch enable) the actual values loaded in the driver into its output cells. |
|
The output cells drive parallel outputs. |
The standard serial peripheral interface (SPI) contains SDO, SDI and CLK signals.
It is necessary to latch (LE signal pulse) internal driver data once per n CLK cycles, where n is the total LED count. This latching rule ensures the correct mapping of the serialized PWM data coming on SDI to the corresponding parallel output. Refer to Figure 2, Figure 3 and Figure 4.
6/31
AN2841 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Description of LED dimming |
|||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 2. |
PWM serial-parallel conversion for multiple LED applications |
||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T PWM |
SERIAL DATA 1111 1101 0101 0001 |
0000 1111 or new |
|
data |
T SEND_DATA |
LEVELS |
|
|
|
t |
PARALLELDATA |
|
|
4% duty cycle |
Output 1- (LED1) |
t |
|
|
Output 2-(LED2) |
14% duty cycle |
|
t |
Output 3-(LED3) |
3% duty cycle |
|
|
|
t |
Output 4-(LED4) |
99% duty cycle |
|
t |
AM00343
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
N = 2nffSPI
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.
Figure 4 illustrates a simple dimming application using an STP24DP05 24-bit constant current LED sink driver with output error detection.
5 V |
|
|
3.3 V |
LED block |
RGB LED configuration |
|
Temperature |
|
|
sensor |
|
|
R1...8 G1...8 B1...8 |
|
|
Driving |
STP24DP05 |
|
system |
|
|
STP24DP05 |
|
|
SPI |
SPI |
|
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.
USB |
ESD for USB |
|
USBUF01P6 |
|
|
SPI |
LED driver |
STP24DP05
SPI
Power supply |
|
|
STM32 |
|
LED driver |
|
ST1S10 |
|
|
|
STP24DP05 |
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
SPI
AM00345
This section describes the two approaches used to transfer data (dynamic and static) as well as timing considerations.
When using a dynamic data approach, all data is computed just before being transfered to the LED drivers.
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 |
|
|
|
|
|
depth = 0 |
|
0 .. 255 for 8-bit PWM resolution |
||
|
|
|
0 .. LEDcount / 8 |
|
|
|
DATAchar = 0 |
|
|
|
|
|
Detail in Figure 8: |
|
|
LED |
|
|
LEDmap |
LEDdriverDATAbuffer |
SPI |
Driver |
|
|
DATAchar++ |
|
|
|
|
– |
DATAchar>3 |
|
|
|
|
|
|
|
|
|
|
|
+ |
|
|
|
|
|
depth++ |
|
|
|
|
– |
depth>255 |
|
|
|
|
|
|
|
|
|
|
|
+ |
|
|
|
|
|
|
|
|
|
AM00346 |
10/31