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

Why doesn't Timer interrupt operate at the interval which is configured in code?

I want to execute timer interrupt at a certain interval, like 1sec or 5 sec.

I measured an actual interval of voltage change with oscilloscope, but the actual interval is different from the interval configured in code.

Does anyone know this reason?

(configured) -> (actual)
1 sec -> 0.74 5sec
2 sec -> 1.49 sec
3 sec -> 2.24 sec
5 sec -> 4.25 sec
10 sec -> 7.46 sec

#include <nrf9160.h>
#include <zephyr.h>
#include <misc/printk.h>
#include <device.h>
#include <gpio.h>

#define TIEMR_INTERVAL_SEC 1 // 1,2,3,5,10 sec were tested

struct device *gpio_0_dev;
struct k_timer my_timer;
uint8_t toggle = 1;

void my_expiry_function(struct k_timer *timer_id){
        ++toggle;
        gpio_pin_write(gpio_0_dev, 16, toggle % 2);
}

void main(void)
{
        printk("------------\r\nHello, World!\r\n");
        k_timer_init(&my_timer, my_expiry_function, NULL);

	gpio_0_dev = device_get_binding("GPIO_0");
        gpio_pin_configure(gpio_0_dev, 16, GPIO_DIR_OUT);
        gpio_pin_write(gpio_0_dev, 16, toggle);
        k_timer_start(&my_timer, K_SECONDS(TIEMR_INTERVAL_SEC), K_SECONDS(TIEMR_INTERVAL_SEC));
        
        while(1){
                 k_cpu_idle();
        }
}

Parents Reply Children
Related