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));
}