Timer channels

#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_drv_timer.h"
#include "bsp.h"
#include "app_error.h"
#include "nrf_gpio.h"
#define LED1 13
#define LED2 14
#define LED3 15
#define Button 11
#define Button1 12
static nrfx_timer_t timer=NRFX_TIMER_INSTANCE(0);
static int n=12,m=12;
void time_handler(nrf_timer_event_t event_type, void* p_context)
{
switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
{
if(n<11)
{
nrf_gpio_pin_toggle(LED1);
printf("%d",n);//debug
n++;
if(n==10)
{
nrf_gpio_pin_clear(LED1);
}
}
}
break;

case NRF_TIMER_EVENT_COMPARE1:
{
if(m<11)
{
nrf_gpio_pin_toggle(LED2);
printf("%d",m);//debug
m++;

if(m==10)
{
nrf_gpio_pin_clear(LED2);
}
}
}
break;

default:
break;
}
}
int main(void)
{
uint32_t time_ticks,time_ticks1,count=0;
uint32_t time_ms=1000,time_ms1=5000;

nrfx_timer_config_t configure = NRFX_TIMER_DEFAULT_CONFIG;

nrfx_timer_init(&timer,&configure,time_handler);

time_ticks=nrfx_timer_ms_to_ticks(&timer,time_ms);
time_ticks1=nrfx_timer_ms_to_ticks(&timer,time_ms1);

nrfx_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL0,time_ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,true);
nrfx_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL1,time_ticks1,NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK,true);

nrfx_timer_enable(&timer);

nrf_gpio_cfg_output(LED1);
nrf_gpio_cfg_output(LED2);

nrf_gpio_cfg_input(Button,NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(Button1,NRF_GPIO_PIN_PULLUP);
while(1)
{
if(nrf_gpio_pin_read(Button)==0)
{
n=0;
}
if(nrf_gpio_pin_read(Button1)==0)
{
m=0;
}
}
}

WHEN I TRY WITH ONE CHANNEL THE TIMER WORKS PREFECT BUT WHILE HANDLING WITH TWO OR MORE CHANNELS IN TIMER  I DON'T GET OUTPUT .PLEASE HELP ME TO GET OUTPUT IN 2 CHANNEL .THANK YOU

Parents
  • You are setting a NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK at 1000ms and also a NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK at 5000ms.  This means that the counter will be reset every 1000ms, so the second compare event (at 5000ms) will never happen.

    You could instead let the timer be free running (and wrap around from time to time).  Then every time the interrupt handler is called, reset the next compare event to be +1000ms/+5000ms into the future.  Do an unsigned add and things will wrap as needed.

     

Reply
  • You are setting a NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK at 1000ms and also a NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK at 5000ms.  This means that the counter will be reset every 1000ms, so the second compare event (at 5000ms) will never happen.

    You could instead let the timer be free running (and wrap around from time to time).  Then every time the interrupt handler is called, reset the next compare event to be +1000ms/+5000ms into the future.  Do an unsigned add and things will wrap as needed.

     

Children
  • #include <stdbool.h>
    #include <stdio.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nrf_drv_timer.h"
    #include "bsp.h"
    #include "app_error.h"
    #include "nrf_gpio.h"
    #define LED1 13
    #define LED2 14
    #define LED3 15
    #define Button 11
    #define Button1 12
    static nrfx_timer_t timer=NRFX_TIMER_INSTANCE(0);
    static int n=12,m=12;
    void time_handler(nrf_timer_event_t event_type, void* p_context)
    {
    switch (event_type)
    {
    case NRF_TIMER_EVENT_COMPARE0:
    {
    if(n<11)
    {
    nrf_gpio_pin_toggle(LED1);
    printf("%d",n);//debug
    n++;
    if(n==10)
    {
    nrf_gpio_pin_clear(LED1);
    }
    }
    }
    break;

    case NRF_TIMER_EVENT_COMPARE1:
    {
    if(m<11)
    {
    nrf_gpio_pin_toggle(LED2);
    printf("%d",m);//debug
    m++;

    if(m==10)
    {
    nrf_gpio_pin_clear(LED2);
    }
    }
    }
    break;

    default:
    break;
    }
    }
    int main(void)
    {
    uint32_t time_ticks,time_ticks1,count=0;
    uint32_t time_ms=0;

    nrfx_timer_config_t configure = NRFX_TIMER_DEFAULT_CONFIG;

    nrfx_timer_init(&timer,&configure,time_handler);

    time_ms +=50;
    printf("%d\n",time_ms);
    time_ticks=nrfx_timer_ms_to_ticks(&timer,time_ms);
    printf("%d\n",time_ticks);
    nrfx_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL0,time_ticks,0,true);

    time_ms +=50;
    printf("%d\n",time_ms);
    time_ticks=nrfx_timer_ms_to_ticks(&timer,time_ms);
    printf("%d\n",time_ticks);
    nrfx_timer_extended_compare(&timer,NRF_TIMER_CC_CHANNEL1,time_ticks,NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK,true);

    nrfx_timer_enable(&timer);

    nrf_gpio_cfg_output(LED1);
    nrf_gpio_cfg_output(LED2);

    nrf_gpio_cfg_input(Button,NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(Button1,NRF_GPIO_PIN_PULLUP);
    while(1)
    {
    if(nrf_gpio_pin_read(Button)==0)
    {
    n=0;
    }
    if(nrf_gpio_pin_read(Button1)==0)
    {
    m=0;
    }
    }
    }

    I changed the timing value by adding the value is changes but the output to led does not changes..HELP ME SIR 

  • Hi there,

    Are you using the nRF52840 DK? Most likely, the GPIO button value isn't read correctly resulting in the program never entering the conditional statement since n/m = 12 and again resulting in the leds not being toggled. 

    regards

    Jared

Related