2 years, 9 months ago.

Does PwmOut work on the K64F?


I've been trying to set up PwmOut on my K64F all day without any luck. All I am trying to do is light up an LED with a PWM signal so that the period is 2 seconds with a 50% duty cycle. Ive been using the functions from the mbed handbook (see link below) but nothing works.


No matter what I do the LED is either always ON or OFF but I can never get it to toggle. I am using an external LED not the on board as i know that one cannot be driven by PWM. I have looked around online quite a lot and some people are saying PwmOut does not work with K64F yet, is this true?

I have tried all the pins as well as both windows and Mac to compile the code and still no results. Could someone please correct the simple code below?

  1. include "mbed.h"

PwmOut led(PTC3); or any other PWM enabled pin

int main() { led.period(4.0f); 4 second period led.write(0.50f); 50% duty cycle, relative to period while(1); }

1 Answer

2 years, 9 months ago.

Hey, PWMOut does work. However, periods longer than 65 ms will not work for the reasons outlined here - https://github.com/ARMmbed/mbed-os/issues/2936#issuecomment-251883742

I have verified with a logic analyzer that a period of 65 ms works. For future reference, the mbed docs have moved here - https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/io/PwmOut/, and they reference the most recent version of mbed OS. The new docs are also linked in the warning at the top of this page that you linked - https://developer.mbed.org/handbook/PwmOut

If you are looking to blink an LED at some interval, I would recommend looking at the Blinky example - https://docs.mbed.com/docs/mbed-os-handbook/en/latest/getting_started/blinky_cli

If you need to background the blinking LED, I would look into using mbed OS to run blinky in a separate thread like this example -https://developer.mbed.org/teams/mbed_example/code/rtos_basic/file/dc33cd3f4eb9/main.cpp

I will look into getting a warning about this added to the platforms page for K64F.

Accepted Answer

Thank you Sarah, I changed the period and it works now :)

posted by Mateusz Loboda 01 Sep 2017