Dear Sir.
I am using the Timer of nRF916 .
In a simple project the timer is working fine ( see attached code ).
A buzzer is connected to PWM output pin 12.
I can here the Buzzer beeps as expected .
/* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <nrf9160.h> #include <zephyr.h> #include <device.h> #include <drivers/pwm.h> #include <drivers/gpio.h> //#define LED 12 #define PWM_IO 12 struct device *pwm_dev; /* 1000 msec = 1 sec */ #define SLEEP_TIME 100 struct k_timer bz_timer; void bz_func(struct k_timer *timer_id) { static unsigned char k = 0; static unsigned int time = 0; // Siren // if(time == 0) // pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0); // if(time == 10) // pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0); // if(++time >= 20) // time = 0; // Short Beep if(time == 0) pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0); if(time == 7) pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0); if(++time >= 100) time = 0; // Long Beep // if(time == 0) // pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0); // if(time == 20) // pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0); // if(++time >= 150) // time = 0; // Train // if(k < 100) // { // if(time == 0) // pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0); // if(time == 5) // pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 0,0); // if(++time >= 20) // { // time = 0; // if(++k > 20) // k = 100; // } // } } void main(void) { u32_t cnt = 0; struct device *dev_io; printk("PWM Application has started!\r\n"); pwm_dev = device_get_binding("PWM_0"); if (!pwm_dev) { printk("Cannot find %s!\n", "PWM_0"); return; } k_timer_init(&bz_timer, bz_func, NULL); k_timer_start(&bz_timer, K_SECONDS(1), K_MSEC(50)); while(1) { k_sleep(K_MSEC(800)); } }
In another larger project , I insert the Timer code , and it behaves erratically .
The time between each entry to the timer is seconds.
What can cause this behavior ?.
Please Advise
struct k_timer bz_timer; void bz_func(struct k_timer *timer_id) { unsigned char k; static unsigned int time = 0; // Siren if(time == 0) pwm_pin_set_usec(pwm_dev,PWM_IO, 260, 130,0); if(time == 10) pwm_pin_set_usec(pwm_dev,PWM_IO, 400, 200,0); if(++time >= 20) time = 0; } void main(void) { int err; static unsigned int i = 0; k_work_init(&work_top, work_top_func); k_work_init(&work_spi, spi_work_func); k_work_init(&work_adp5360, adp5360_work_func); initStructures(); // Ephraim 26_5_2020 _AFE4900_AQUIRE_MODE.aquire_mode = afe4900_aquire_sample; k_timer_init(&bz_timer, bz_func, NULL); k_timer_start(&bz_timer, K_SECONDS(1), K_MSEC(50)); io_init(); pwm_init(); while(1) { k_sleep(K_MSEC(2000)); k_sleep(K_MSEC(2000)); };