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

multiple channels using same PWM instance

How can i initialise all four channel of pwm1 using pwm library. i need to initialise one channel for one purpose and other channel for different purpose. whenever i am trying to initialise it is showing error what shou;d be the correct way

what are the necessory changes in PWM library example

  • it is showing redefinition of pwm1 timer

    in example we have only used two channel of pwm1

    so how can we use remaining two channnels

  • Are you using the PWM driver or PWM library ? Could you upload your project here?

  • #include <stdio.h> #include "boards.h" #include "app_uart.h" #include "app_error.h" #include <nrf_delay.h> #include "app_mpu.h" #include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "bsp.h" #include "app_pwm.h"

    /*UART buffer size. */
    #define UART_TX_BUF_SIZE 256
    #define UART_RX_BUF_SIZE 1
    
    /**
     * @brief UART events handler.
     */
    static void uart_events_handler(app_uart_evt_t * p_event)
    {
        switch (p_event->evt_type)
        {
            case APP_UART_COMMUNICATION_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }
    
    
    /**
     * @brief UART initialization.
     * Just the usual way. Nothing special here
     */
    static void uart_config(void)
    {
        uint32_t                     err_code;
        const app_uart_comm_params_t comm_params =
        {
            RX_PIN_NUMBER,
            TX_PIN_NUMBER,
            RTS_PIN_NUMBER,
            CTS_PIN_NUMBER,
            APP_UART_FLOW_CONTROL_DISABLED,
            false,
            UART_BAUDRATE_BAUDRATE_Baud115200
        };
    
        APP_UART_FIFO_INIT(&comm_params,
                           UART_RX_BUF_SIZE,
                           UART_TX_BUF_SIZE,
                           uart_events_handler,
                           APP_IRQ_PRIORITY_LOW,
                           err_code);
    
        APP_ERROR_CHECK(err_code);
    }
    
    
    
    void mpu_setup(void)
    {
        ret_code_t ret_code;
        // Initiate MPU driver
        ret_code = mpu_init();
        APP_ERROR_CHECK(ret_code); // Check for errors in return value
        
        // Setup and configure the MPU with intial values
        mpu_config_t p_mpu_config = MPU_DEFAULT_CONFIG(); // Load default values
        p_mpu_config.smplrt_div = 19;   // Change sampelrate. Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV). 19 gives a sample rate of 50Hz
        p_mpu_config.accel_config.afs_sel = AFS_2G; // Set accelerometer full scale range to 2G
        ret_code = mpu_config(&p_mpu_config); // Configure the MPU with above values
        APP_ERROR_CHECK(ret_code); // Check for errors in return value 
    }
    
    
    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;
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {    
        uint32_t err_code;
    	LEDS_CONFIGURE(LEDS_MASK);
    	LEDS_OFF(LEDS_MASK);
      uart_config();
    	
      printf("\nRiosh Technologies\n");  
    	
    	//ret_code_t err_code;
        
        /* 2-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_2 , BSP_LED_3);
        
        /* 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);
        
       uint32_t value;
       
    	mpu_setup();
        
        accel_values_t acc_values;
        uint32_t sample_number = 0;
        while(1)
        {
    			
            // Read accelerometer sensor values
            err_code = mpu_read_accel(&acc_values);
            APP_ERROR_CHECK(err_code);
            // Clear terminal and print values
            printf("\033[3;1HSample # %d\r\nX: %06d\r\nY: %06d\r\nZ: %06d", ++sample_number, acc_values.x, acc_values.y, acc_values.z);
    		   // nrf_gpio_pin_toggle(LED_1);
            nrf_delay_ms(250);
    			
    			if(acc_values.x > 10000)
    			{
    			  for (uint8_t i = 0; i < 40; ++i)
            {
                value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);
                
                ready_flag = false;
                /* Set the duty cycle - keep trying until PWM is ready... */
                while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);
                
                /* ... or wait for callback. */
                while(!ready_flag);
                APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
                nrf_delay_ms(25);
            }
    			}
    			
        }
    	
    }
    
Related