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

Simple PWM

Hello,

I am new to Nordic devices. So it is tough for me to understand PWM examples. Can you provide me the simple code of PWM? Basically, I want to give a specific brightness to the led. With specific frequency. I am using nrf dongle (PCA10059) with segger studio.

                                     Waiting for your quick response. 

Parents
  • Hi Parman

    I shared a simple PWM example a couple of months ago:
    https://devzone.nordicsemi.com/support-private/support/221537

    Please take a look at that Slight smile

    Best regards
    Torbjørn

  • Hello,

       Glad to hear from you

    But I am unable to open the page saying the -

    Access Denied

    But I found some code -  

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "nrf_drv_clock.h"
    #include "nrf_delay.h"


    #define OUTPUT_PIN LED_1

    static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);

    // Declare variables holding PWM sequence values. In this example only one channel is used
    nrf_pwm_values_individual_t seq_values[] = {0, 0, 0, 0};
    nrf_pwm_sequence_t const seq =
    {
    .values.p_individual = seq_values,
    .length = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats = 0,
    .end_delay = 0
    };


    // Set duty cycle between 0 and 100%
    void pwm_update_duty_cycle(uint8_t duty_cycle)
    {

    // Check if value is outside of range. If so, set to 100%
    if(duty_cycle >= 100)
    {
    seq_values->channel_0 = 100;
    }
    else
    {
    seq_values->channel_0 = duty_cycle;
    }

    nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1, NRF_DRV_PWM_FLAG_LOOP);
    }

    static void pwm_init(void)
    {
    nrf_drv_pwm_config_t const config0 =
    {
    .output_pins =
    {
    OUTPUT_PIN, // channel 0
    NRF_DRV_PWM_PIN_NOT_USED, // channel 1
    NRF_DRV_PWM_PIN_NOT_USED, // channel 2
    NRF_DRV_PWM_PIN_NOT_USED, // channel 3
    },
    .irq_priority = APP_IRQ_PRIORITY_LOWEST,
    .base_clock = NRF_PWM_CLK_1MHz,
    .count_mode = NRF_PWM_MODE_UP,
    .top_value = 100,
    .load_mode = NRF_PWM_LOAD_INDIVIDUAL,
    .step_mode = NRF_PWM_STEP_AUTO
    };
    // Init PWM without error handler
    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));

    }


    int main(void)
    {

    // Start clock for accurate frequencies
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    // Wait for clock to start
    while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
    ;

    pwm_init();

    for (;;)
    {
    for(int i = 0; i <= 100; i++)
    {
    nrf_delay_ms(10);
    pwm_update_duty_cycle(i);
    }
    }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    But I got some errors -

    Rebuilding ‘blinky_pca10059_mbr’ from solution ‘blinky_pca10059_mbr’ in configuration ‘Release’
    Assembling ‘thumb_crt0.s’
    Compiling ‘nrf_log_frontend.c’
    Compiling ‘nrf_log_str_formatter.c’
    Compiling ‘boards.c’
    Compiling ‘app_error.c’
    Compiling ‘app_error_handler_gcc.c’
    Compiling ‘app_error_weak.c’
    Compiling ‘app_util_platform.c’
    Compiling ‘nrf_assert.c’
    Compiling ‘nrf_atomic.c’
    Compiling ‘nrf_balloc.c’
    Compiling ‘nrf_fprintf.c’
    Compiling ‘nrf_fprintf_format.c’
    Compiling ‘nrf_memobj.c’
    Compiling ‘nrf_ringbuf.c’
    Compiling ‘nrf_strerror.c’
    Compiling ‘nrfx_atomic.c’
    Compiling ‘main.c’
    nrf_drv_pwm.h: No such file or directory
    Build failed

    But I am new so I am not able to figure out the errors. Sorry. Kindly guide me.

                                Waiting for your quick response                           

  • nrf_drv_pwm.h: No such file or directory

    Answered in your ADC thread: https://devzone.nordicsemi.com/f/nordic-q-a/46716/simple-adc-code/184681#184681

    I am new

    Here are some 'C' learning & reference materials for you:

    http://blog.antronics.co.uk/category/c-programming/

    Also, some microcontroller "getting started" tips:

    https://www.avrfreaks.net/comment/2079906#comment-2079906

    How to properly post source code:

  • Thank You for your guidance Mr. Awneil. I will follow your steps and reply back.

Reply Children
No Data
Related