This repository was archived by the owner on Jan 29, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 1
This repository was archived by the owner on Jan 29, 2023. It is now read-only.
Poor accuracy on timer interrupt frequency or interval. #4
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
Timer interrupts at a lower frequency than expected.
Arduino IDE 1.8.19
Arduino MBED nano 3.3.0
MBED_RPI_PICO_TimerInterrupt 1.1.2
Hardware: Arduino nano rp2040
Steps to Reproduce
I set a frequency of 20 kHz, and then count 20k interrupts. I check mills() before and after. The delta comes out to 1020 ms where it should be 1000. So it's off by 2%.
Expected behavior
I expected it to take 1000 ms to count 20000 interrupts at 20000 Hz
Actual behavior
It took 1020 ms, so it's off by 2%. I see the same error if I program it by interval or by frequency.
Test code:
#include <MBED_RPi_Pico_TimerInterrupt.h> #include <MBED_RPi_Pico_TimerInterrupt.hpp> #include <MBED_RPi_Pico_ISR_Timer.h> #include <MBED_RPi_Pico_ISR_Timer.hpp> #define TIMER_FREQ_HZ 20000 MBED_RPI_PICO_Timer ITimer1(1); int delta = 0; void TimerHandler(uint alarm_num) // Frequency version { static int zz = 0; static int prev_millis = 0; int new_millis; TIMER_ISR_START(alarm_num); zz++; if(zz >= TIMER_FREQ_HZ) { zz = 0; new_millis = millis(); delta = new_millis - prev_millis; prev_millis = new_millis; } TIMER_ISR_END(alarm_num); } void setup() { Serial.begin(9600); delay(2500); if (ITimer1.attachInterrupt(TIMER_FREQ_HZ, TimerHandler)) Serial.println("Starting ITimer OK, millis() = " + String(millis())); else Serial.println("Can't set ITimer. Select another freq. or timer"); } void loop() { delay(1000); Serial.println(delta); } Sample printout:
Starting ITimer OK, millis() = 2500 0 3520 1020 1020 1020 1020 1020 1020 1020 1020 1020 1020 1020 Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working