This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM on nrf52840

i have just blinked the LED and trying to pwm 

i have written a simple led blinking program like this one below


#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_gpio.h"

#define led_pin 27
/**
* @brief Function for application main entry.
*/
int main(void)
{
/* Configure board. */
nrf_gpio_cfg_output(led_pin);

while(1)
{
nrf_gpio_pin_toggle(led_pin);
nrf_delay_ms(500);
}

}

#####################################

a simple one, 

now i need to get to do pwm like short and sweet like this one 

---------most importantly i am using segger embedded studio---------------

can anyone tell how to do pwm in this nrf52840

please!!!

Parents Reply
  • Hi,

    If you just want a simple PWM system, then you can use the PWM library. The example uses two breathing LEDs, but you can simplify it to one fixed LED glowing with 50 % intensity. Then the example could reduce to this:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "nrf_delay.h"
    #include "app_pwm.h"
    
    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    
    int main(void)
    {
        ret_code_t err_code;
    
        /* 1-channel PWM, 200Hz, output on DK LED 1 pin. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, BSP_LED_0);
    
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg, NULL);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
    
        /* Set duty cycle to 50 % */
        while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
    
        while (true)
        {
        }
    }

Children
Related