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

voice output using PWM

 Hello,

I'm trying to play voice using PWM in nRF51822.

I've already experienced this using other MCU before.

So. I'm trying.

Voice source data is 8bit / 8K / mono PCM sampling data.

I call below code according to 8KHz frequency.

I write the above PCM sampling data value to below level register.

The val register value is a fixed value of 50.

At the test level, I don't execute any other code, but only this code.

And the below code is used for buzzer playback now.

I don't know what's wrong.

Could anybody let me know what's wrong or some tip?

 Thanks and happy new year.

*****************************************************************************************

// for sound level

err_code = app_pwm_uninit(&PWM_BUZZER);
// NRF_LOG_RAW_INFO("pwm-buzzer uninit error : %d\r\n", err_code);
APP_ERROR_CHECK(err_code);

app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(level, BUZZER_PIN);
pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;

err_code = app_pwm_init(&PWM_BUZZER, &pwm1_cfg, NULL); // callback_pwmBuz);
// NRF_LOG_RAW_INFO("pwm-buzzer init error : %d\r\n", err_code);
APP_ERROR_CHECK(err_code);

app_pwm_enable(&PWM_BUZZER);
// NRF_LOG_RAW_INFO("pwm-buzzer enable\r\n");

// for sound strength
while(app_pwm_channel_duty_set(&PWM_BUZZER, 0, val) == NRF_ERROR_BUSY);

*****************************************************************************************

Parents
  • I modified the library as shown below and confirmed that the voice was output.

    But the quality is too low.

    I have already commercialized it in this way.

    Of course I have to optimize the hardware, but I do not know why.

    ****************************************************************************************************************


    APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1.

    static volatile bool ready_flag; // A flag indicating PWM status.

    uint16_t voiceData = 0;

    const unsigned char hexData[8253] = {

    // 8bit/8K PCM sampling data

    }

    void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
    {
    ready_flag = true;


    int main(void)
    {
      ret_code_t err_code;

      while (true)
      {
        /* 2-channel PWM, 200Hz, output on DK LED pins. */
        // app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_0, BSP_LED_1);
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(125L, 13);

        /* Switch the polarity of the second channel. */
        // pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
        pwm1_cfg.pin_polarity[0] = 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);


        // for speaker DC-DC enable
        nrf_gpio_pin_set(16);
        nrf_gpio_cfg_output(16);
        nrf_delay_ms(100);

     
        for (uint16_t i = 0; i < 8253; i++)
        {
          ready_flag = false;

          // voiceData = (hexData[i] * 100) / 0xff;
          voiceData = hexData[i] / 2;

          /* Set the duty cycle - keep trying until PWM is ready... */
          while (app_pwm_channel_duty_set(&PWM1, 0, voiceData) == 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);
        }

        // for speaker DC-DC disable
        app_pwm_disable(&PWM1);
        nrf_gpio_pin_clear(13);
        nrf_gpio_cfg_output(13);
        nrf_delay_ms(2000);
      }
    }

Reply Children
No Data
Related