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

  • 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!!!
  • 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();
    
        }
    
    }
    
    
    /** @} */
    
  • 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 in my program? please give a demo code for this really helpful to me if you can!

  • 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);
        }
    }
    }
    
  • 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 running and code get stuck and sensor does not giving me the values what are the changes i have to make in my code to get desired output.

Related