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

Control 6 channel PWM at the same time which can modify duty-cycle by app_timer

Hi,

There have 4 PWM sample code (low_power_pwm, pwm_driver, pwm_library and pwr_mgmt) in SDK 15.2.

I my use case is "Control 6 PWM output with different duty-cycle and duty-cycle can modify by app_timer".

Which sample is suitable for this situation?

Thank you,

Chianglin

  • I see. If I am not mistaken those kind of motors have a PWM period of 20ms (50Hz) and typically a PWM Duty Cycle of 1-2 ms depending on the desired angle, right?

    Do you plan to use Bluetooth Low Energy as well?

    The reason I am asking is that the app_pwm uses the application timer to toggle the pulses. This means that it uses a physical timer for the app timer, but the CPU to handle the timer interrupts, and these toggles the pins. The difference using PPI or the PWM peripheral is that you don't need to interrupt the CPU to toggle the pins. It is done "automatically" in HW. 

    The issue by toggling these pins using CPU interrupts is that if the CPU is busy doing something else, e.g. handling a SoftDevice/BLE event, then the toggling of the pin will be delayed, which may cause twitches on the motors. If you test the app_pwm using LEDs, or you motors together with some BLE activity, you will be able to see this with your eyes. If this is tolerable, then that is fine. But if these twitches are too much, you must use the TIMER+PPI. 

    Best regards,

    Edvin

  • Hi,

    How can I control PWM to send some step, then stop PWM  output.

    For example I want to send 20 pulse out by PWM

    Thank you

    Chianglin

  • Hi Edvin,,

    I am try to add a nrf timer into my original ble_app_uart project, but the system will crash when execute "ble_stack_init()" function.

    Please find following source code

    #include "nrf.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_drv_timer.h"
    #include "bsp.h"
    #include "nrf_gpio.h"
    #include "app_error.h"
    #include "TimerPtocess.h"
    
    const nrf_drv_timer_t TIMER_US_ID = NRF_DRV_TIMER_INSTANCE(0);
    void timer_us_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
        switch (event_type)
        {
            case NRF_TIMER_EVENT_COMPARE0:
            	nrf_gpio_pin_toggle(25);
        		break;
    
            default:
                //Do nothing.
                break;
        }
    }
    
    void Ctimers_init(void)
    {
    	uint32_t				time_us = 52; //Time(in miliseconds) between consecutive compare events.
    	uint32_t				time_ticks;
    	uint32_t				err_code = NRF_SUCCESS;
    	nrf_drv_timer_config_t	timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    
    	err_code = nrf_drv_timer_init(&TIMER_US_ID, &timer_cfg, timer_us_event_handler);
    	APP_ERROR_CHECK(err_code);
    	NRF_LOG_INFO("B0001");
    	time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_US_ID, time_us);
    	NRF_LOG_INFO("B0002");
    	nrf_drv_timer_extended_compare(&TIMER_US_ID, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    	NRF_LOG_INFO("B0003");
    	nrf_drv_timer_enable(&TIMER_US_ID);
    	NRF_LOG_INFO("B0004");
    }
    

    And I insert Ctimers_init() in original main.c (like following code)

    	gpio_init();
    	uart_init();
    	log_init();
    	timers_init();
    	NRF_LOG_INFO("--------------------- Start ---------------");
    	Ctimers_init();
    
    	twi_init();
    	NRF_LOG_INFO("B0006");
    	power_management_init();
    	NRF_LOG_INFO("B0010");
    	ble_stack_init();
    	NRF_LOG_INFO("B0011");

    The debug message is

    <info> app: --------------------- Start ---------------
    00> <info> app: B0006
    00> <info> app: B0010
    00> <error> app: Fatal error

    Does nrf timer can not use in ble_app_uart project?

    Because I need a quick GPIO on/off output (100KHz),, but APP timer can not create so-much quick timer.

    Would you please tell me how can I do?

    Best Regards,

    Chianglin

  • chianglin said:
    I am try to add a nrf timer into my original ble_app_uart project, but the system will crash when execute "ble_stack_init()" function.

    Add "DEBUG" to your preprocessor defines. One of your APP_ERROR_CHECK(err_code); receives an err_code != 0. this will tell you which one, and what value err_code has. What function returned the err_code != 0? and what is err_code?

  • Hi,

    Thank you for your quick answer.

    I had change nrf timer to timer-1, then the system will not crash anymore.

    Does it the correct solution?

    About the question "How can I control PWM to send some step, then stop PWM  output. (for example: 20 steps)?", would you please give my some idea?

    Thank you

Related