Go Down

Topic: Set up timers (Read 96 times) previous topic - next topic

russell_roberts

Hi I have been using the mkr1010 and set up timer 4 to be able to set a pin high & low to run a stepper motor driver

I did this by following some YouTube & forum messages to set top the registers. I also heavily relied on the tome library.

I was hoping for a similar option to set up the timers on the Nano 33 BLE which uses a different chip

Thanks Russell

russell_roberts

I have seen this post which I have not as yet had the opportunity to try.
If you have same issue as me maybe it will help you

https://devzone.nordicsemi.com/f/nordic-q-a/1610/timer-interrupts

if you do look at the post advisable to read more than the first post as it has corrections

russell_roberts

Ok I till have not had any luck
I have written a program where the internal LED is written HIGH in set up and then the ISR should set it low.

I have tried to follow all examples I have found but it does not work

I would appreciate some help

Thanks

Code: [Select]



// 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

// 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.println("looping");
//
//    // 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_16Bit;
  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) &
                            TIMER_SHORTS_COMPARE0_CLEAR_Msk;
  NVIC_EnableIRQ(TIMER2_IRQn);
   
  NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
}







void TIMER2_IRQHandler()
{

if (NRF_TIMER2->EVENTS_COMPARE[0] != 0)
{
NRF_TIMER2->EVENTS_COMPARE[0] = 0;

digitalWrite(ledPin, LOW );


}
}

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





Go Up