Hello
I'm trying to print out several sounds with one buzzer. To do that, I need to be able to change the frequency.
PWM was initialized and reset using code.
An attempt was made to change the frequency depending on how the button is entered button.#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "nordic_common.h"
#include "nrf.h"
#include "ble_hci.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "nrf_sdh.h"
#include "nrf_sdh_soc.h"
#include "nrf_sdh_ble.h"
#include "nrf_ble_gatt.h"
#include "nrf_ble_lesc.h"
#include "nrf_ble_qwr.h"
#include "app_timer.h"
#include "ble_nus.h"
#include "app_uart.h"
#include "app_util_platform.h"
#include "bsp_btn_ble.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_twi.h"
#include "nrf_uart.h"
#include "nrf_uarte.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "app_button.h"
#include "nrf_delay.h"
#include "peer_manager.h"
#include "peer_manager_handler.h"
#include "fds.h"
#include "app_error.h"
#include "bsp.h"
#include "app_pwm.h"
#include "nrf_drv_saadc.h" //ADC
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1 (name, NRF_DRV_TIMER_INSTANCE(num)).
static volatile bool ready_flag; // A flag indicating PWM status.
void pwm_play_buzzer_duration(uint32_t duration) //button buzzer
{
uint8_t i;
uint32_t duty = 50;
NRF_LOG_INFO("pwm_play_buzzer_duration =>");
ready_flag = false;
//Set the duty cycle - keep trying until PWM is ready...
while (app_pwm_channel_duty_set(&PWM1, 0, duty) == NRF_ERROR_BUSY);
// ... or wait for callback.
//while (!ready_flag);
//APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, duty));
nrf_delay_ms(duration);
app_pwm_disable(&PWM1);
}
void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
{
NRF_LOG_INFO("pwm_ready_callback =>");
ready_flag = true;
}
/*
init, Once executed
*/
//buzzer PWM test
void pwm_init(void)
{
ret_code_t err_code;
/* 1-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1500, BUZZER_PIN); //original 5000L
/* 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);
}
void pwm_change_frequency(uint16_t period_us) //reset PWM
{
ret_code_t err_code;
app_pwm_disable(&PWM1); //disable PWM for change PWM
/* 1-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(period_us, BUZZER_PIN); //original 5000L
/* 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);
}
static void do_play_buzzer(void)
{
pwm_play_buzzer_duration(300); //delay
app_pwm_enable(&PWM1);
}
static uint32_t pressed_time, released_time, pressed_duration = 0;
static bool pressed_cnt= false;
static bool over5sec_cnt = true; //When at first, trying to connect.
static uint32_t app_button_duration(void)
{
pressed_duration = (released_time - pressed_time)*2; //ms
return pressed_duration;
}
static void app_button_init_time_variable(void)
{
pressed_duration = 0;
//released_time = 0;
//pressed_time = 0;
}
static void app_button_event_generator(void)
{
ret_code_t err_code;
//if ((pressed_duration >= 2000) && (pressed_duration < 5000) ){ //over 2sec
if ((pressed_duration >= 3000) && (pressed_duration < 10000000)) //over 3sec
{
//nrf_gpio_pin_clear(APP_LED_2); //ledt led on, timer test
NRF_LOG_INFO("Button pressed for 3sec...");
printf("Long button\n");
pwm_change_frequency(5000); //change PWM
do_play_buzzer();
//do_play_buzzer_2();
if(m_conn_handle == BLE_CONN_HANDLE_INVALID) //in disconnect pairing
{
printf("Not yet pairing!\n");
}
else //in connect pairing
{
printf("Disconnected pairing!\n");
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); //disconnect pairing
APP_ERROR_CHECK(err_code);
}
}
app_button_init_time_variable();
}
static void app_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
ret_code_t err_code;
switch (pin_no)
{
case APP_BTN_1: //button1
switch (button_action)
{
case APP_BUTTON_PUSH:
{
NRF_LOG_INFO("Button pressed...");
printf("Button pressed...");
nrf_gpio_pin_clear(APP_LED_3); //led on
pwm_change_frequency(1500);
do_play_buzzer();
//nrf_gpio_pin_clear(LED_2); //add(led test)
//LeftLED_timers_start(); //timer test
//get pressed time
pressed_time = app_timer_cnt_get()*1000/32768;//milli-sec
NRF_LOG_INFO("pressed_time: %d", (int)pressed_time);
printf("pressed_time: %d\n", (int)pressed_time);
pressed_cnt = true;
} break;
case APP_BUTTON_RELEASE:
{
NRF_LOG_INFO("Button releaed...");
nrf_gpio_pin_set(APP_LED_3); //led off
//nrf_gpio_pin_set(LED_2);
if (pressed_cnt)
{
released_time = app_timer_cnt_get()*1000/32768; //milli-sec
NRF_LOG_INFO("released_time: %d", (int)released_time);
printf("released_time: %d\n", (int)released_time);
}
pressed_cnt = false;
} break;
}
break;
default:
APP_ERROR_HANDLER(pin_no);
break;
}
if(app_button_duration())
{
app_button_event_generator();
}
app_button_init_time_variable();
}
static void app_buttons_init(void)
{
uint32_t err_code;
static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
{
#if (1)
#ifdef APP_BTN_1 //button1 (custom pca10040)
{APP_BTN_1, false, BUTTON_PULL, app_button_event_handler},
#endif // APP_BTN_1
#else
#ifdef BSP_BUTTON_0 //button1
{BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
#endif // BUTTON_0
#endif
};
err_code = app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER, // 1
APP_TIMER_TICKS(50)); //debounce
APP_ERROR_CHECK(err_code);
}
int main(void)
{
uint32_t err_code;
bool erase_bonds = false;
// Initialize.
gpio_init();
uart_init();
log_init();
timers_init();
app_buttons_init(); //buttons_leds_init(&erase_bonds);
power_management_init();
ble_stack_init();
gap_params_init();
//set_static_passkey(); //Central display
gatt_init();
services_init();
advertising_init();
conn_params_init();
peer_manager_init();
err_code = app_button_enable();
//err_code = app_button_disable();
APP_ERROR_CHECK(err_code);
//adc_configure(); baterry value & LED
pwm_init();
// Start execution.
printf("\r\nUART started.\r\n");
NRF_LOG_INFO("Debug logging for UART over RTT started.");
//CenterLED_timers_start();
advertising_start(erase_bonds); //false
for(;;)
{
idle_state_handle(); //sleep mode
}
}
This will stop all operation when the button is pressed to output the PWM.

