0

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

FASTER gravatar image

asked 2017-10-11 14:21:27 +0100

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

edit retag flag offensive close delete report spam

1 answer

Sort by ยป oldest newest most voted
0
sigurdon gravatar image

answered 2017-10-12 10:44:12 +0100

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

edit flag offensive delete publish link more

Comments

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?

ASHISH ( 2017-10-12 14:56:32 +0100 )editconvert to answer

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.

Sigurd ( 2017-10-12 15:02:48 +0100 )editconvert to answer

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?

ASHISH ( 2017-10-13 08:26:43 +0100 )editconvert to answer

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.

Sigurd ( 2017-10-13 09:11:06 +0100 )editconvert to answer

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!!!

ASHISH ( 2017-10-13 14:32:05 +0100 )editconvert to answer

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 ...
(more)
Sigurd ( 2017-10-13 15:08:40 +0100 )editconvert to answer

Thanks,Sigurd i know i understand this example but i dint want to use configured pwm example and i am just generating pwm using a simple for loop.i have problem with initialization of gpio pins .i want to connect led to gpio pin and i want set that led high for 10 ms and low for 20 ms so how should i write a code for that how to declare gpio pins: i have done this in my program but dint get any desired output :this is my code

int main(void)
{
#include pin_number 1
int main(){

nrf_gpio_cfg_output(pin_number1);
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);
    }

what are the changes i have to make i am using pin no. 5 to connect to led what should i do?what are the initialization and functions header files that i need to include ...(more)

ASHISH ( 2017-10-13 15:24:42 +0100 )editconvert to answer

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);
    }
}
}
Sigurd ( 2017-10-13 15:36:03 +0100 )editconvert to answer

Thanks man, your code is working but now i want to run my vibration motor using a ir sensor.This is my code

int main(void)
{
    adc_config();

    uart_config();
        nrf_gpio_cfg_output(pin_number1);

    printf("\n\rADC HAL simple example\r\n");

    printf("Current sample value:\r\n");

    while (true)
    {
              // trigger next ADC conversion
              nrf_adc_start();
        // enter into sleep mode
        __SEV();
        __WFE();
        __WFE();

              nrf_delay_ms(100);    
                printf("%d\r\n", (int)adc_sample); // out ADC result

            if(adc_sample<20)
            {

   // 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);
 // }
}
    }

    }
#endif /* NRF51 */
 //@}

My ir sensor gives me values between (14 to 255)as i configured it at 8 bit resolution.i want run my vibration motor whenever i am getting ir sensor values below 20.and my vibration motor should stop when my sensor giving values above 20.but in my main code after reaching value 20 my motor is just ...(more)

ASHISH ( 2017-10-15 08:56:37 +0100 )editconvert to answer

Have you tried debugging ? Do you get the desired behavior if you remove __SEV(); __WFE(); __WFE(); ?

Sigurd ( 2017-10-16 09:31:20 +0100 )editconvert to answer

hi,Sigurd thanks for everything. i have make some changes in my code and now everything is working but i am using pin no p0.05 for pwm output and output voltage is in mv so it is not sufficient to drive the vibration motor have also used transistor. i have given my pwm signal to base of transistor and load is connected to collector side but the voltage that i found at collector is 0.7 v.when i check pin p0.09 and p0.011 i found the voltage at this pin is 3.3 v and when tried to run this pin for pwm output the pin doesn't respond anything just giving the 3.3 v output.Does the nrf51822 pins have different voltage and current specification if yes then can you tell me which pins can i use for normal pwm output. i have used ...(more)

ASHISH ( 2017-10-16 12:29:07 +0100 )editconvert to answer

All the GPIO have the same voltage and current specification.

You only have PWM in mV when you use pin 5?

You get a 3.3V PWM when using ping 9 and 11?

Sigurd ( 2017-10-18 09:28:15 +0100 )editconvert to answer

The problem is solved Sigurd THANKS FOR YOUR HELP!!

ASHISH ( 2017-10-30 05:39:21 +0100 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower

Stats

Asked: 2017-10-11 14:21:27 +0100

Seen: 81 times

Last updated: okt. 12