Hi all ,
I have a buzzer that I will order using the library pwm and it works succefully .
But Now I want to turn on the PWM if the temperature value is greater than 40 and turn off pwm otherwise unfortunately the app_pwm_disable doesn't work that is to say the buzzer starts ringing as soon as the temperature value exceeds 40 and does not stop .
is there any solution to do that ?
there is my main code
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "bsp.h"
#include "app_pwm.h"
ret_code_t err_code;
APP_PWM_INSTANCE(PWM1,1); // Create the instance "PWM1" using TIMER1.
#define buzzer NRF_GPIO_PIN_MAP(1, 11)
static volatile bool ready_flag; // A flag indicating PWM status.
void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
{
ready_flag = true;
}
void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
{
// Empty handler function
}
void saadc_init(void)
{
// A variable to hold the error code
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
APP_ERROR_CHECK(err_code);
// Initialize the Channel which will be connected to that specific pin.
err_code = nrfx_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
}
// A function which will initialize the Log module for us
void log_init(void)
{
// check if any error occurred during its initialization
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
// Initialize the log backends module
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
/**
* @brief Function for main application entry.
*/
int main(void)
{
// call the log initialization function
log_init();
// call the saadc initialization function created above
saadc_init();
nrf_saadc_value_t adc_val;
// Print a simple msg that everything started without any error
NRF_LOG_INFO("Application Started!!!");
/* 1-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(25000000L, buzzer);
/* Switch the polarity of the second channel. */
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* Initialize and enable PWM. */
// Inifinite loop
while (1)
{
nrfx_saadc_sample_convert(0, &adc_val);
float voltage = adc_val * (3300 / 1024.0);
float temperature = (voltage - 500) / 10;
NRF_LOG_RAW_INFO(" temperature: " NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(temperature));
NRF_LOG_RAW_INFO(" \xB0"); // shows degree symbol
NRF_LOG_RAW_INFO("C");
NRF_LOG_RAW_INFO(" ");
nrf_delay_ms(1000);
if (temperature> 40)
{
NRF_LOG_RAW_INFO("hiiiiiiiiiiiiii ");
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
app_pwm_enable(&PWM1);
}
else {
err_code = app_pwm_uninit(&PWM1);
app_pwm_disable(&PWM1);
NRF_LOG_RAW_INFO("heeeeeeere ");
}
}
}
/** @} */