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

I am using nRF52832. How can I create continuously running 2 timers using Arduino IDE?

I was able to create 2 times, Timer 1 and Timer 2. 

They work when they run separately, but one stops when I try to use both of them. 

I need simultaneously running two timers. 

#include <nrf.h>
#include "nrf_timer.h"
#include "Timer.h"

#define OUT_PIN 25
#define OUT_PIN1 24


#define nrf_timer_num (2)
#define cc_channel_num (0)
TimerClass timer(nrf_timer_num, cc_channel_num);

#define nrf_timer_num1 (1)
#define cc_channel_num1 (0)
TimerClass timer1(nrf_timer_num1, cc_channel_num1);

void setup() {

Serial.begin(230400);
Serial.println("Starting...");

pinMode(OUT_PIN, OUTPUT);
digitalWrite(OUT_PIN, 0);


pinMode(OUT_PIN1, OUTPUT);
digitalWrite(OUT_PIN1, 0);

timer.attachInterrupt(&Timer_callback, 600); // microseconds
timer1.attachInterrupt(&Timer_callback1, 1200); // microseconds


}


void Timer_callback() {
digitalWrite(OUT_PIN, 1); // to visualize when it was called
digitalWrite(OUT_PIN, 0);

timer.attachInterrupt(&Timer_callback, 600); // microseconds
}


void Timer_callback1() {
digitalWrite(OUT_PIN1, 1); // to visualize when it was called
digitalWrite(OUT_PIN1, 0);

timer1.attachInterrupt(&Timer_callback1, 1200); // microseconds
}

void loop() {

// Sleep
__WFE(); // Enter System ON sleep mode (OFF mode would stop timers)
__SEV(); // Make sure any pending events are cleared
__WFE(); // More info about this sequence:
// devzone.nordicsemi.com/f/nordic-q-a/490/how-do-you-put-the-nrf51822-chip-to-sleep/2571
}

Related