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

how to define gpio pins and generate pwm on gpio pins?

Hi There, i am new to nrf51822 and new to program this kind of controller.I searched over the website, I couldn't find a good introductory documentation for programming the nrf51822, specifically the nrf51-dk. i have two problems 1.i want to connect a led and buzzer to gpio pins of nrf51822 so how to declare gpio pins in program for ex. suppose if i press button on nrf board the buzzer connected to gpio pins (for (ex. p0.0) should be on (make some sound). 2.i want to generate a pwm on gpio pin (for ex on p0.1) how to declare this pin as output for pwm how to use functions to declare gpio,led and other stuff as i gone through the tutorial it is not well defined and the tutorials are very much confused and easy to understandable can anybody give me the good explnation about this a good tutorial with each and every line explained will be great help!! Regards, Abhijeet Kapse

Parents
  • Hi,

    As an introduction, I would recommend the Getting Started guide and our examples in the SDK. For PWM generation on the nRF51, you should take a look at the PWM library example. You can find the example in the folder: <SDK_InstallFolder>\examples\peripheral\pwm_library

  • Try something like this:

    /** @file
     * @defgroup pwm_example_main main.c
     * @{
     * @ingroup pwm_example
     *
     * @brief  PWM Example Application main file.
     *
     * This file contains the source code for a sample application using PWM.
     *
     *
     */
    
    #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.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        ready_flag = true;
    }
    
    int main(void)
    {
        ret_code_t err_code;
    
        /* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 4. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(100000L, 4);
    
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
        
        //DUTY CYCLE SET TO 50%
        while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
    
       
        while (true)
        {
                // Use directly __WFE and __SEV macros since the SoftDevice is not available.
    
        // Wait for event.
        __WFE();
    
        // Clear Event Register.
        __SEV();
        __WFE();
    
        }
    
    }
    
    
    /** @} */
    
Reply
  • Try something like this:

    /** @file
     * @defgroup pwm_example_main main.c
     * @{
     * @ingroup pwm_example
     *
     * @brief  PWM Example Application main file.
     *
     * This file contains the source code for a sample application using PWM.
     *
     *
     */
    
    #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.
    
    static volatile bool ready_flag;            // A flag indicating PWM status.
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        ready_flag = true;
    }
    
    int main(void)
    {
        ret_code_t err_code;
    
        /* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 4. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(100000L, 4);
    
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
        
        //DUTY CYCLE SET TO 50%
        while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
    
       
        while (true)
        {
                // Use directly __WFE and __SEV macros since the SoftDevice is not available.
    
        // Wait for event.
        __WFE();
    
        // Clear Event Register.
        __SEV();
        __WFE();
    
        }
    
    }
    
    
    /** @} */
    
Children
No Data
Related