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

Setup timer on ARDUINO NANO 33 BLE nRF 52840.

Hi I am having trouble setting up a timer and interrupt on the Arduino side & was hoping some one might help

here is my code so far

Thanks so much 


// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
volatile int flag = 0;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  setupSerial();
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
	digitalWrite(ledPin, HIGH );
  start_timer();
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
 unsigned long currentMillis = millis();
//
  if (currentMillis - previousMillis >= interval) {
//    // save the last time you blinked the LED
    previousMillis = currentMillis;
		Serial.print("looping");
    Serial.println(flag);
//    // if the LED is off turn it on and vice-versa:
//    if (ledState == LOW) {
//      ledState = HIGH;
//    Serial.println("Going high");
//    } else {
//      ledState = LOW;
//    }
//
//    // set the LED with the ledState of the variable:
// //   digitalWrite(ledPin, ledState);
  }
}


void start_timer(){
  NRF_TIMER2->TASKS_STOP = 1; // Stop timer
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode
	NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
  NRF_TIMER2->TASKS_CLEAR = 1;               // clear the task first to be usable for later
  NRF_TIMER2->PRESCALER = 8;                             //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
  NRF_TIMER2->CC[0] = 62500;                             //Set value for TIMER2 compare register 0
//  NRF_TIMER2->CC[1] = 5;                                 //Set value for TIMER2 compare register 1
	
	
  // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events
  NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
	NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
  NVIC_EnableIRQ(TIMER2_IRQn);
    
  NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
  Serial.println("Timer configured");
	
}


extern "C"
{
	void TIMER2_IRQHandler(void)
	{
	    if (NRF_TIMER2->EVENTS_COMPARE[0] == 1)
	    {   
	       flag++;
	        NRF_TIMER2->EVENTS_COMPARE[0] = 0;
	    }
	}
	
}
void setupSerial(){
  int time_now = millis();
  Serial.begin(115200);
  while (!Serial && (millis() < (time_now + 3000) ) ) {
    // wait for serial port to connect. Needed for native USB port only
  } 
  if(Serial){
      Serial.println("Serial running setup");
  }
 
  Serial.println("Make sure to turn on the device.");
}

Related