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

multiple timers not working on nrf52832 Dev Kit

Hello,

I am using the nrf52832 with SDK v12.3 and PCA10040. Not using any soft devices, nor the RTC. The program is intended such thattimer0 is set to toggle a pin (P0_11) every 166us using 1MHz clock source and timer2 is set to toggle another pin (P0_08) every 1us also using the 1MHz clock source. timer0 is working properly toggling every 166us but timer2 is toggling every 2.6us when it should be toggling every 1us according to my settings.

In sdk_config.h, I have:

// <e> TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver
//==========================================================
#ifndef TIMER_ENABLED
#define TIMER_ENABLED 1
#endif

// <q> TIMER0_ENABLED  - Enable TIMER0 instance


#ifndef TIMER0_ENABLED
#define TIMER0_ENABLED 1
#endif

// <q> TIMER1_ENABLED  - Enable TIMER1 instance


#ifndef TIMER1_ENABLED
#define TIMER1_ENABLED 0
#endif

// <q> TIMER2_ENABLED  - Enable TIMER2 instance


#ifndef TIMER2_ENABLED
#define TIMER2_ENABLED 1
#endif

// <q> TIMER3_ENABLED  - Enable TIMER3 instance


#ifndef TIMER3_ENABLED
#define TIMER3_ENABLED 0
#endif

// <q> TIMER4_ENABLED  - Enable TIMER4 instance


#ifndef TIMER4_ENABLED
#define TIMER4_ENABLED 0
#endif

in main.c, I have the following:

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_drv_timer.h"
#include "nrf_timer.h"
#include "nrf_gpio.h"

//timer0
const nrf_drv_timer_t timer0 = NRF_DRV_TIMER_INSTANCE(0);
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;

//timer2
const nrf_drv_timer_t timer2 = NRF_DRV_TIMER_INSTANCE(2);
nrf_drv_timer_config_t timer_cfg2 = NRF_DRV_TIMER_DEFAULT_CONFIG;

int main(void)
{
    uint32_t  err_code = NRF_SUCCESS;

    //timer0 test pin
    nrf_gpio_cfg_output(P0_11);
    //timer2 test pin
    nrf_gpio_cfg_output(P0_08);

    //change config from defaults in NRF_DRV_TIMER_DEFAULT_CONFIG for timer0
    timer_cfg.frequency = NRF_TIMER_FREQ_1MHz;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_8;

    //change config from defaults in NRF_DRV_TIMER_DEFAULT_CONFIG for timer2
    timer_cfg2.frequency = NRF_TIMER_FREQ_1MHz;
    timer_cfg2.bit_width = NRF_TIMER_BIT_WIDTH_32;

    //initialize timer0 with config changes and event handler
    err_code = nrf_drv_timer_init(&timer0, &timer_cfg, timer0_event_handler);
    APP_ERROR_CHECK(err_code);

    //initialize timer2 with config changes and event handler
    err_code = nrf_drv_timer_init(&timer2, &timer_cfg2, timer2_event_handler);
    APP_ERROR_CHECK(err_code);

    //set ticks for timer0
    uint32_t timer0_ticks = 166;
    //set timer0 operating mode
    nrf_drv_timer_extended_compare(&timer0, NRF_TIMER_CC_CHANNEL0, timer0_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    //set ticks for timer2
    uint32_t timer2_ticks = 1;
    //set timer2 operating mode
    nrf_drv_timer_extended_compare(&timer2, NRF_TIMER_CC_CHANNEL0, timer2_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    //enable timer0
    nrf_drv_timer_enable(&timer0);
    //enable timer2
    nrf_drv_timer_enable(&timer2);

    while(1)
    {
        __WFI();
    }

}
Related