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 Reply Children
  • Thanks,sigurd but this example drives led using pwm and my problem is that i want to drive vibration motor using pwm waveform.so how to define gpio pins ?how to generate pwm waveform on gpio pins?can you explain me in detail how to interface vibration motor to nrf51822?

  • If you need PWM on 2 GPIO pins, use APP_PWM_DEFAULT_CONFIG_2CH(). If you need PWM on 1 GPIO pin, use APP_PWM_DEFAULT_CONFIG_1CH().

    The first argument is the period in microseconds, and second argument is the GPIO pin you want to use.

    APP_PWM_DEFAULT_CONFIG_1CH(period_in_us,#PIN_YOU_WANT_TO_USE)
    

    In the PWM library example we have this:

     app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_0, BSP_LED_1);
    

    So you can just remove the BSP_LED_0and BSP_LED_1, and replace it with the GPIO pin number you want to use.

  • Thanks for your reply i have replaced the BSP_LED_0and BSP_LED_1 with gpio pins 5 and 6 and i connected pin 6 to pwm input of vibration motor.but my vibration motor is not running .the gpio pins give me pwm voltage between 0 to 2.9v.how to increases pwm output voltage? how to change the period and duty cycle in this program? what are the changes i have to make in my program?

  • You can change the duty cycle with the function app_pwm_channel_duty_set, i.e to set it to 50% duty on PWM channel 0:

    while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
    

    To increase the voltage, you will need a higher voltage soruce, and use e.g. a transistor to amplify the PWM signal.

  • hey,Sigurd how to initialize gpio pins i want to generate a pwm on gpio pins so how to define pin number and its configuration in a function please give me a short example so i can understand it properly. i have already define pins but my program is not working properly this my program:

    #include <stdbool.h> #include <stdint.h> #include "nrf_delay.h" #include "nrf_gpio.h" #include "boards.h" #include "bsp.h" #include "nrf.h" #include "nrf_gpio.h" #include "nrf_delay.h" #define pin_number 5 //#include "nrf_drv_gpiote.h" #define NRF_GPIO_PIN_MAP(port,pin) ((port << 5) | (pin & 0x1F)) //const uint8_t leds_list[LEDS_NUMBER] = LEDS_LIST;

    /**

    • @brief Function for application main entry. */ int main(void) { for(int i=0;i<=20;i++) { nrf_gpio_cfg_output(5); nrf_gpio_pin_set(5); nrf_delay_ms(10); nrf_gpio_pin_clear(5); nrf_delay_ms(20); } whats wrong with my code ..i am using a just simple for loop for a simple pwm generation great help appreciated!!!
Related