Cannot get the timers to work NRF52840

Hey, I have been trying to use the hardware timers on my nrf52840 without success.
This program should print "Test" every cycle of the loop function and make the blue LED blink.
The code compiles fine but when I run it, it gets stuck after a few seconds (about 5 so I guess the interruption a least gets triggered), the LED stays on. I tried all timers but none of them works.
I am aware that a few libraries already exists but I want to build my own one. Also, the timers work when using other libraries.

By the way, I have two ISR functions because I couldn't find where the correct name was and I found both of the ones I used on the internet.

#include <arduino.h>
#include "Adafruit_TinyUSB.h"
#include <nrf_timer.h>

void setup(){

  pinMode(LED_BLUE, OUTPUT);
  digitalWrite(LED_BLUE, HIGH);

  Serial.begin(115200);

  NRF_TIMER1->BITMODE = NRF_TIMER_BIT_WIDTH_32;
  NRF_TIMER1->MODE = NRF_TIMER_MODE_TIMER;
  NRF_TIMER1->PRESCALER = NRF_TIMER_FREQ_1MHz;
  NRF_TIMER1->CC[0] = 5000000;  // 5 sec

  NRF_TIMER1->INTENSET = nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0);
  NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;

  NVIC_SetPriority(TIMER1_IRQn, 3);
  NVIC_SetVector(TIMER1_IRQn, (uint32_t)TIMER1_IRQHandler);

  NVIC_EnableIRQ(TIMER1_IRQn);
  NRF_TIMER1->TASKS_START = 1;

  __enable_irq();
}

void loop(){
  Serial.println("TEST");
  delay(250);
}

void TIMER1_IRQHandler()
{
  NRF_TIMER1->EVENTS_COMPARE[0] = 0;
  digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
}

void TIMER1_IRQHandler_v()
{
  NRF_TIMER1->EVENTS_COMPARE[0] = 0;
  digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
}

Welcome! Thanks for properly posting your code. From what I can see you are flooding the serial port. What happens is when the buffer fills up it waits until it can place the current message in the buffer. While waiting all of the code is blocked.

It might also help if you told your code what pin LED_BLUE is, you say pinMode(LED_BLUE, OUTPUT); you also need to const LED_BLUE = whatever pin you are using.

Hey, thanks for your help!
I tried removing the Serial (it was just to tell if the code is still running or if it is blocked) but it still doesn't work.
LED_BLUE is already defined in the libraries I use and I know it works because I set it to High right after the pinMode in the setup and I can see the LED light up.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.