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

  • I would recommend using the PWM library for generating a PWM pulse. nrf_delay_ms will keep the CPU active, and consume much more power. nrf_delay_ms is also not very accurate.

    But you can get your code to work like 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"
    
    #define pin_number1 1
    int main(){
    
    nrf_gpio_cfg_output(pin_number1);
        while(true){
    for(int i=0;i<=20;i++){
        nrf_gpio_pin_set(pin_number1);
        nrf_delay_ms(10);
        nrf_gpio_pin_clear(pin_number1);
        nrf_delay_ms(20);
        }
    }
    }
    
Reply
  • I would recommend using the PWM library for generating a PWM pulse. nrf_delay_ms will keep the CPU active, and consume much more power. nrf_delay_ms is also not very accurate.

    But you can get your code to work like 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"
    
    #define pin_number1 1
    int main(){
    
    nrf_gpio_cfg_output(pin_number1);
        while(true){
    for(int i=0;i<=20;i++){
        nrf_gpio_pin_set(pin_number1);
        nrf_delay_ms(10);
        nrf_gpio_pin_clear(pin_number1);
        nrf_delay_ms(20);
        }
    }
    }
    
Children
No Data
Related