0
\$\begingroup\$

Currently I have some difficulties on the Discovery Board STM32F429I to control the on-board LEDs via PWM. According to the data sheet of the Discovery Board the LEDs are located on pin 13 and pin 14 (PG13 & PG14).

According to my research, it is possible to solve the PWM control with the on-board Timer to dim the LEDs. In the data sheet of the MCU, I don't see any timer assignments as an alternative function to generate the PWM signal at the LED. Enclosed is the mapping table from the data sheet of the MCU:

Alternative Function Mapping STM32F429I Disco

Is there no "elegant" possibility to control the on-board LED via PWM?

Which alternative ways exist to control the brightness via PWM of the on-board LED?


Thank you very much.

\$\endgroup\$
1
\$\begingroup\$

The hardware solution

Find two suitable timer output pins (check the STM32 pin description versus board functions table in the user manual of the board), and connect them with jumper wires to PG13 and PG14 on the board headers.

enter image description here

Then proceed to configuring the PWM outputs on the selected pins. Always set PG13 and PG14 as inputs, otherwise the MCU might be damaged.

The software solution

If your program has a functioning SysTick interrupt handler, you can toggle the pins in the handler function. If you don't want the LEDs to visibly flicker, you'd need at least 20 Hz frequency. With a SysTick frequency of 1 kHz, 51 distinct output levels are possible, 0% to 100% in 2% steps.

volatile int led1_level; // set this variable to control the duty cycle in 2% steps
void dim_leds() { // call this function from SysTick_Handler()
  static int cnt;
  cnt = (cnt + 1) % 50;
  if(cnt < led1_level)
    GPIOG->BSRR = 1 << 13;
  else
    GPIOG->BSRR = 1 << (13 + 16);
}

Controlling PG14 is left as an exercise to the reader.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for the workaround. In fact, I noticed now that the pins PG13 and PG14 are also available for the LEDS as well as for the outside. Nevertheless, I have a question whether the onboard LED lights up at all if I define it as an input. Are these connected in such a way that they also light up as input? I currently prefer the software method with the SysTick_Handler. \$\endgroup\$ – E. Yagmahan Jul 19 at 14:30
  • \$\begingroup\$ The LED would be connected to two pins at the same time. One of them, the timer output is driving the LED. The other one is a high impedance input, as if it were not there. Find the LED in the schematics at the end of the board user manual. Draw a connection to the timer output pin, parallel to PG13. Now scratch the connection to PG13, and draw a connection to GND through a 1MΩ resistor, it'd be the substitute for the input pin. What do you think now, will it work? \$\endgroup\$ – berendi Jul 19 at 16:40
0
\$\begingroup\$

Unfortunately, PG13 & PG14 can't be connected to a timer module. You can check it in CubeMX by clicking on those pins (as @muyustan has mentioned), you will not see any timer in the drop-down list. This can also be confirmed from the datasheet: The pin listing table on page 68 doesn't mention a timer connection for those pins. And finally, as you have already figured out, there is no such an option in the alternate function table.

However, I can think of a dirty hack to dim those LEDs connected to PG13 & PG14.

PG13 can be used as SPI6_SCK. So if you use SPI6 to send (or receive) dummy bytes continuously, you can obtain 50 % duty cycle on that pin. Unfortunately, that is all you can do for PG13 other than 0 % and 100 %.

The situation is better for PG14. You have 2 options: USART6_TX and SPI6_MOSI. You can send specific bit trains to obtain some duty cycle values, like sending 0b00000011 for 25 % duty cycle. If you can spare a DMA in circular mode, it can retrieve this byte from your dummy duty register and feed the SPI as needed.

I would go for the SPI option. In that case, you can control both PG13 & PG14 with SPI6. To set PG13 to 100 % or 0 %, you need to change MODER and revert it back to GPIO output mode.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for the feedback. The idea with the UART (for PG14) to generate a PWM signal is indeed very clever. Too bad that there is no elegant alternative. \$\endgroup\$ – E. Yagmahan Jul 19 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.