Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

NRF52_MBED_Timer's TimerHandler delay #4

Closed
klyatskin opened this issue Jul 19, 2021 · 3 comments
Closed

NRF52_MBED_Timer's TimerHandler delay #4

klyatskin opened this issue Jul 19, 2021 · 3 comments

Comments

@klyatskin
Copy link

// Arduino Nano 33 BLE

#define TIMER_INTERRUPT_DEBUG         0
#define _TIMERINTERRUPT_LOGLEVEL_     0
#define HW_TIMER_INTERVAL_MS      1

#include "NRF52_MBED_TimerInterrupt.h"
#include "NRF52_MBED_ISR_Timer.h"

NRF52_MBED_Timer ITimer(NRF_TIMER_3);

// Init NRF52 timer NRF_TIMER3
NRF52_MBED_Timer ITimer(NRF_TIMER_3);
// Init NRF52_MBED_ISRTimer
// Each NRF52_MBED_ISRTimer can service 16 different ISR-based timers
NRF52_MBED_ISRTimer ISR_Timer;

#define TIMER_INTERVAL_1s    1000L

volatile static unsigned int count = 0;
void TimerHandler() {
    count += 1;
    if (count % 100000 == 0) // **** should happen every 1 sec? - does not work
        digitalWrite(LED_PWR, !digitalRead(LED_PWR)); 
    ISR_Timer.run();
}


void onTimer1s() { // once per 1 sec. - it works
     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
   }


void setup() {
    // Interval in microsecs
    if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000/100, TimerHandler)) {  // **** every 10 us?
        // Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
    } else {
       //  Serial.println(F("Can't set ITimer. Select another freq. or timer"));
    }
    ISR_Timer.setInterval(TIMER_INTERVAL_1s, onTimer1s); 
 }


void loop() {
}

For some reason LED_PWR blink are about twice as slow... onTimer1s works as expected.

@khoih-prog
Copy link
Owner

Interrupt-related code requires better skills and experience and it's better to use only and only if i's absolutely necessary.

Don't use too fast 10uS timer interrupt.

You also have to test using some of the examples first, to fully understand what they're doing before modifying to fit your purpose.

Start from ISR_16_Timers_Array example, then moving on gradually.

I don't want to spend anytime on this issue as it's not the library issue.

Good Luck,

@klyatskin
Copy link
Author

klyatskin commented Jul 19, 2021 via email

@khoih-prog
Copy link
Owner

Glad to know that it's somehow helpful to you and you're very welcome.

What I need is 150us frequency for 50% load pwm.

You have to write the code yourself or use any PWM library working with MBED Nano-33-BLE. If there is no ready-made library, just port any PWM one to NRF52 MBED platform.

It all depends on your code (using how many MCU cycles, etc. ) and the core.
Also don't use time-consuming ISR_Timer.setInterval() which is designed for slow-pacing jobs.

Is there any better way to chat with you?

Sorry, I won't do this. You're on your own

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants