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

Nrf52810 PWM Driver frequency is not stable

Using PWM control 2.7KHZ buzzer, it can work normally when I don't turn on the broadcast.Every time I turn on the radio, the buzzer seems to blend in with different frequencies, making it sound more than just a sound.

The phenomenon occurs during broadcasting, and the number of times different frequencies are mixed is also related to the broadcast interval.At intervals of 40ms, different frequencies sound mixed all the time;When the broadcast interval is 200ms, it sounds like intermingling is spaced.

How do developers solve this problem?Here is my control file.

#include "beep.h"
#include "bsp.h"
#include "nrf_drv_pwm.h"
#include "app_timer.h"
#include "nrf_delay.h"
#include "app_pwm.h"
#include "low_power_pwm.h"

//#define APP_PWM
#define PWM_DRIVER

#define EN_BEEP    19

#ifdef PWM_DRIVER
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
#endif
static uint8_t beep_start_is = 0;

#ifdef APP_PWM
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;
}
#endif

#ifdef PWM_DRIVER
/* This array cannot be allocated on stack (hence "static") and it must
   be in RAM (hence no "const", though its content is not changed). */
static nrf_pwm_values_common_t /*const*/ seq_values[] =
{
    0x8000,
         0,
};

nrf_pwm_sequence_t const seq =
{
    .values.p_common = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};
#endif
/* < 蜂鸣器定时器ID */
APP_TIMER_DEF(beep_tiemr_id);

static void beep_timer_handle(void *p_context)
{
    UNUSED_PARAMETER(p_context);
    beep_stop();
}


void beep_init(void)
{
    uint32_t err_code = NRF_SUCCESS;
    #ifdef PWM_DRIVER
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            EN_BEEP,   // channel 0
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 1
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 2
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 185,
        .load_mode    = NRF_PWM_LOAD_COMMON,
        .step_mode    = NRF_PWM_STEP_AUTO
    };

    err_code = nrf_drv_pwm_init(&m_pwm0, &config0, NULL);
    APP_ERROR_CHECK(err_code);
#endif

#ifdef APP_PWM
    /* 2-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(185L, 7, EN_BEEP);
    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,NULL);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
#endif

    err_code = app_timer_create(&beep_tiemr_id, APP_TIMER_MODE_SINGLE_SHOT, beep_timer_handle);
    APP_ERROR_CHECK(err_code);
}

void beep_start(uint32_t time)
{
    uint32_t err_code = NRF_SUCCESS;

    if(beep_start_is == 1)
    {
        return;
    }
#ifdef PWM_DRIVER
    (void)nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 3, NRF_DRV_PWM_FLAG_LOOP);
#endif

#ifdef APP_PWM
    while (app_pwm_channel_duty_set(&PWM1, 1, 50) == NRF_ERROR_BUSY);
#endif

    beep_start_is = 1;
    if(time != BEEP_LOUD_NEVER_STOP_TIME)
    {
//        err_code = app_timer_start(beep_tiemr_id, APP_TIMER_TICKS(time), NULL);
//        APP_ERROR_CHECK(err_code);
    }
}

void beep_stop(void)
{
    if(beep_start_is == 0)
    {
        return;
    }
#ifdef APP_PWM
    while (app_pwm_channel_duty_set(&PWM1, 0, 0) == NRF_ERROR_BUSY);
#endif

#ifdef PWM_DRIVER
    nrfx_pwm_stop(&m_pwm0, true);
#endif
    beep_start_is = 0;
}


void beep_sleep(void)
{
#ifdef PWM_DRIVER
    nrfx_pwm_uninit(&m_pwm0);
#endif
}


Parents Reply Children
No Data
Related