6 years, 1 month ago.

nRF51822 and PwmOut not working

Hello,

I have been trying to get Pwm to work on a raytac MDBT40 based nRF51822 board, but no success. Below is a sample code which doesnt work, 2 leds one controlled by PWM and the other in the while loop. If I comment out Pwm related lines led2 blinks, otherwise nothing blinks. I am using MBED 5.7

Here is the sample code which doesnt work

#include "mbed.h"

PwmOut led1(p18);
DigitalOut led2(p19,1);


int main() {
    // specify period first, then everything else
    led1.period(4.0f);  // 4 second period
    led1.write(0.50f);  // 50% duty cycle
    while(1)
    {
        led2 = !led2;
        wait(0.5);
    }
}

I am beginning to think that (i) there is a magic define somewhere which enables Pwm or (ii) all my dev boards are faulty (though BLE, I2C, SPI and many other things work fine), or (iii) there is bug in MBED which everyone else has missed.

Any help, tips or effort to reproduce is greatly appreciated thank you

P.S Sorry for double posting

Not necessarily a solution but a PWM period of 4 seconds is very unusual. Generally when you use a PWM for an LED you are using it to control the brightness of the LED rather than making it blink. That means a period of around 15 ms or less in order to avoid visible flicker.

Try setting the PWM period to something around 1000 times smaller than you currently have and see if the code then runs with one LED flashing and the other on all the time but a little dimmer (50% duty cycle won't look much dimmer to the human eye, you may need to drop that to more like 20% to get an obvious dimming).

posted by Andy A 28 Mar 2018

Hi Andy,

Thank you for your response, Of course what you said can be done to get a dimming effect ont he led, but my problem is after setting the PWM period and the duty cycle my MCU seems to crash and stop executing the code. In the sample code i posted led2 should blink once a second but nothing happens. Which i find to be a very weird behavior.

I will try to run this code on a STM MCU to see what happens

posted by Batu Akan 29 Mar 2018

My point was that if you can get a dimming effect then then proves that the PWM is working correctly, the code isn't missing anything and the board isn't broken. That tells you where the problem is.

A 4 second period on the PWM is well outside the normal operating modes, it may be that the internal timers simply don't allow for such a slow PWM and so attempting to set it is causing the system to crash. Such a limitation wouldn't be considered a bug because it's so far outside of the normal region, it's simply a limitation on the hardware/library.

posted by Andy A 29 Mar 2018

I have updated the sample code as below

include the mbed library with this snippet


#include "mbed.h"
 
PwmOut led1(p18);
DigitalOut led2(p30,1);
 
 
int main() {
    led1.period_us(250.0f);  // 250us second period
    led1.write(0.10f);  // 10% duty cycle
    while(1)
    {
        led2 = !led2;
        wait_ms(500);
    }
}

I gave 250us as period with 10% duty, and i see a considerable drop in the brightness of led1, Initally led2 starts high, i the while loop it is turned to low, so the led turns off, but never turns on again. My guess is that some how PWM messes up with the internal timers.

I ran the same code on STM- NUCLEO-F103RB and there i get the expected behavior. In the nrf51822 wait, eventqueue or tickers dont work once the pwmout is declared.

I have softdevice S130 installed if that matters

posted by Batu Akan 29 Mar 2018

I wonder if it is a general bug on nRF51 on MBED or is it my hardware boards? Andy if you have nRF51 board could you verify?

posted by Batu Akan 01 Apr 2018
Be the first to answer this question.