I need an accurate timer to generate pulses about 20ms. With simple timer this is workiong fine, but I need more then one timer simultaneously. So I would like to use app_timer. I do not need a SD. And I do not need external crystal(only the internal RC). I'm working with SDK 11.
When I use the app_timer I will get this:
Its always the same timer. (The low pulse have to be 20 ms). The low pulse duration is always random different. Why? I know from the previous application on nRF51 that the timers are very accurate.
This is how I setup the timer
APP_TIMER_DEF(m_box_timer);
#define APP_TIMER_PRESCALER 2 // Value of the RTC1 PRESCALER register.
#define APP_TIMER_OP_QUEUE_SIZE 3 // Size of timer operation queues.
static void lfclk_request(void)
{
uint32_t err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
}
static void create_timers()
{
uint32_t err_code;
err_code = app_timer_create(&m_box_timer,APP_TIMER_MODE_REPEATED,timeout_handler);
APP_ERROR_CHECK(err_code);
}
Start timer:
app_timer_start(m_box_timer, APP_TIMER_TICKS(20, APP_TIMER_PRESCALER), NULL);
In Main:
lfclk_request();
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
create_timers();
in nrf_drv_config.h file:
#define CLOCK_ENABLED 1
#if (CLOCK_ENABLED == 1)
#define CLOCK_CONFIG_XTAL_FREQ NRF_CLOCK_XTALFREQ_Default
#define CLOCK_CONFIG_LF_SRC NRF_CLOCK_LFCLK_RC
#define CLOCK_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
#endif
#define TIMER1_ENABLED 1
#if (TIMER1_ENABLED == 1)
#define TIMER1_CONFIG_FREQUENCY NRF_TIMER_FREQ_1MHz
#define TIMER1_CONFIG_MODE TIMER_MODE_MODE_Timer
#define TIMER1_CONFIG_BIT_WIDTH TIMER_BITMODE_BITMODE_16Bit
#define TIMER1_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
I'm sure that I can have accurate app_timer without external crystal. But why do I get random timeout times?
Is there an example with app_timer for peripheral projects in SDK11?
Any help?
ADD Here will be the timer started:
if((data_array[index - 1] == '\n') || (index >= (3)))
{
if(data_array[0] == 0x53 && data_array[1] == 0x31)
{
Box1Schloss();
}
}
void Box1Schloss(void)
{
app_simple_timer_start(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, timeout_handler, 65000, NULL);
//app_timer_start(m_box_timer, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
nrf_gpio_pin_set(BOX1_SCHLOSS);
}
void timeout_handler(void * p_context)
{
nrf_gpio_pin_clear(BOX1_SCHLOSS);
}