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

52832 PWM delay

I use timer+ppi+pwm model.my init code like this

void PWM_init()
{
/* ƵµÀµÄ PWM, 200Hz£¨5000us=5ms), ͨ¹ý ¿ª·¢°åLED ¹Ü½ÅÊä³ö. */
	app_pwm_config_t pwm0_cfg = APP_PWM_DEFAULT_CONFIG_1CH(250L, PWM_PIN);

	/* Çл»ÆµµÀµÄ¼«ÐÔ */
	pwm0_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;

	/* ³õʼ»¯ºÍʹÄÜPWM. */
	err_code = app_pwm_init(&PWM0,&pwm0_cfg,pwm_ready_callback);
	APP_ERROR_CHECK(err_code);
	app_pwm_enable(&PWM0);//ʹÄÜPWM
	while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY); 
}

I use some code to close and open PWM ,like this

void PWM_stop()
{
    app_pwm_disable(&PWM0);
}

void PWM_start()
{
    app_pwm_enable(&PWM0);
	while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY);
}

question:I use oscilloscopes to check PWM, I find that when I start PWM,I must to wait about 260us before 52832 product PWM.

                 why I need to wait 260us?

Parents
  • Hi,

    I've included a screenshot of the measured delay between the pin set and the PWM signal being produced on the pin. The only difference is the change in period from 250 µs to 10 µs. I used your code but configured it a bit to get a more accurate measurement

    #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(PWM0,1);                   // ´´½¨Ò»¸öʹÓö¨Ê±Æ÷1²úÉúPWM²¨µÄʵÀý
    
    static volatile bool ready_flag;            // ʹÓÃÒ»¸ö±ê־λ±íʾPWM״̬
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM»Øµ÷¹¦ÄÜ
    {
        ready_flag = true;
    }
    
    void ADC_PWM_START(void)
    {
    	app_pwm_enable(&PWM0);
    	while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY);
    	nrf_gpio_pin_set(16);
    }
    
    void ADC_PWM_STOP(void)
    {
    	app_pwm_disable(&PWM0);
    	nrf_gpio_pin_clear(16);
    }
    
    int main(void)
    {
        ret_code_t err_code;
        
        /* 2-¸öƵµÀµÄ PWM, 200Hz£¨5000us=5ms), ͨ¹ý ¿ª·¢°åLED ¹Ü½ÅÊä³ö. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(10, 26);
        
        /* Çл»Á½¸öƵµÀµÄ¼«ÐÔ */
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* ³õʼ»¯ºÍʹÄÜPWM. */
        err_code = app_pwm_init(&PWM0,&pwm1_cfg,NULL);
        APP_ERROR_CHECK(err_code);
        nrf_gpio_cfg_output(4);
        app_pwm_enable(&PWM0);//ʹÄÜPWM
       		
                
    		/* ÉèÖÃÕ¼¿Õ±È - ²»Í£ÉèÖÃÖ±µ½PWM×¼±¸ºÃ. */
    		while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY);  
    		/* µÈ´ý»Øµ÷ */
    		//nrf_delay_ms(25);
    		nrf_gpio_pin_set(4);
    	
       /* while(true)
        {
    				nrf_delay_ms(1);
    				ADC_PWM_STOP();
    				nrf_delay_ms(1);
            ADC_PWM_START();							
        }*/
        
    }

    Regards

    Jared

Reply
  • Hi,

    I've included a screenshot of the measured delay between the pin set and the PWM signal being produced on the pin. The only difference is the change in period from 250 µs to 10 µs. I used your code but configured it a bit to get a more accurate measurement

    #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(PWM0,1);                   // ´´½¨Ò»¸öʹÓö¨Ê±Æ÷1²úÉúPWM²¨µÄʵÀý
    
    static volatile bool ready_flag;            // ʹÓÃÒ»¸ö±ê־λ±íʾPWM״̬
    
    void pwm_ready_callback(uint32_t pwm_id)    // PWM»Øµ÷¹¦ÄÜ
    {
        ready_flag = true;
    }
    
    void ADC_PWM_START(void)
    {
    	app_pwm_enable(&PWM0);
    	while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY);
    	nrf_gpio_pin_set(16);
    }
    
    void ADC_PWM_STOP(void)
    {
    	app_pwm_disable(&PWM0);
    	nrf_gpio_pin_clear(16);
    }
    
    int main(void)
    {
        ret_code_t err_code;
        
        /* 2-¸öƵµÀµÄ PWM, 200Hz£¨5000us=5ms), ͨ¹ý ¿ª·¢°åLED ¹Ü½ÅÊä³ö. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(10, 26);
        
        /* Çл»Á½¸öƵµÀµÄ¼«ÐÔ */
        pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* ³õʼ»¯ºÍʹÄÜPWM. */
        err_code = app_pwm_init(&PWM0,&pwm1_cfg,NULL);
        APP_ERROR_CHECK(err_code);
        nrf_gpio_cfg_output(4);
        app_pwm_enable(&PWM0);//ʹÄÜPWM
       		
                
    		/* ÉèÖÃÕ¼¿Õ±È - ²»Í£ÉèÖÃÖ±µ½PWM×¼±¸ºÃ. */
    		while (app_pwm_channel_duty_set(&PWM0, 0, 50) == NRF_ERROR_BUSY);  
    		/* µÈ´ý»Øµ÷ */
    		//nrf_delay_ms(25);
    		nrf_gpio_pin_set(4);
    	
       /* while(true)
        {
    				nrf_delay_ms(1);
    				ADC_PWM_STOP();
    				nrf_delay_ms(1);
            ADC_PWM_START();							
        }*/
        
    }

    Regards

    Jared

Children
No Data
Related