GithubHelp home page GithubHelp logo

naguissa / utimerlib Goto Github PK

View Code? Open in Web Editor NEW
20.0 6.0 9.0 31.5 MB

Arduino tiny and cross-device compatible timer library

Home Page: https://www.foroelectro.net/electronica-digital-microcontroladores-f8/utimerlib-libreria-arduino-para-eventos-temporizad-t191.html

License: GNU Lesser General Public License v3.0

C++ 100.00%
arduino arduino-library naguissa timer timers interrupts device-timer microseconds

utimerlib's Introduction

Arduino tiny and cross-device compatible timer library - uTimerLib

Master status: Build Status

What is this repository for?

Tiny and cross-device compatible timer-driven timed function calls library.

Supports Arduino AVR, SAM, STM32, ESP8266, ESP32 and SAMD21 microcontrollers.

Current status

Currently suported architectures:

Device timer usage

Depending on wich architecture this library uses one or another device timer. Take in mind this because can caause conflicts with other libraries:

  • Atmel ATtiny: Timer1 (2nd timer).
  • DisgiSpark AVR: Timer0 (1st timer).
  • Atmel AVR 32U4: Timer3 (4rd timer).
  • Atmel AVR other: Timer2 (3rd timer).
  • STM32: Timer3 (3rd timer).
  • SAM (Due): TC3 (Timer1, channel 0).
  • ESP8266: OS Timer, one slot of seven available (Software timer provided by Arduino because ESP8266 has only two hardware timers and one is needed by it normal operation).
  • ESP32: OS Hardware Timer.
  • SAMD21: Timer 4, CC0 (TC3).
  • SAMD51: Timer 2 (TC1), 16 bits mode.

Note: On ESP8266 this library uses "ticker" to manage timer, so it's maximum resolution is miliseconds. On "_us" functions times will be rounded to miliseconds.

Usage

This library defines a global variable when included called "TimerLib".

You have these methods:

  • TimerLib.setInterval_us(callback_function, microseconds); : callback_function will be called each microseconds.
  • TimerLib.setInterval_s(callback_function, seconds); : callback_function will be called each seconds.
  • TimerLib.setTimeout_us(callback_function, microseconds); : callback_function will be called once when microseconds have passed.
  • TimerLib.setTimeout_s(callback_function, seconds); : callback_function will be called once when seconds have passed.
  • TimerLib.clearTimer(); : will clear any timed function if exists.

It only manages one function at a time, if you call any setXXX method it will cancel any running timed function and process new one.

An attached functions broker could be implemented, but then this would not be (micro)TimerLib. Maybe in other project.....

How do I get set up?

You can get it from Arduino libraries directly, searching by uTimerLib.

For manual installation:

Documentation and extras

You can find all documentation and extras in this repository: https://github.com/Naguissa/uTimerLib_doc_and_extras

You can read documentation online here: https://naguissa.github.io/uTimerLib_doc_and_extras/

Examples

Included on example folder, available on Arduino IDE.

Extra

Look in extras folder for datasheets and extra info

Who do I talk to?

Contribute

Any code contribution, report or comment are always welcome. Don't hesitate to use GitHub for that.

Thanks for your support.

Contributors hall of fame: https://www.foroelectro.net/hall-of-fame-f32/contributors-contribuyentes-t271.html

utimerlib's People

Contributors

daniel-mohr avatar naguissa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

utimerlib's Issues

Error: no matching function for call to 'HardwareTimer::attachInterrupt

Hi, I am using STM32 cores for STM32F109 version 1.9.0 with uTimerLib 1.6.0
my code in in essence:
#include "uTimerLib.h"
TimerLib.setInterval_us(timerEvent, 50000);
void timerEvent() {
timeralarm = true;
}

compilation fail with:


In file included from /home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:120:
/home/andre/Arduino/projects/libraries/uTimerLib/src/hardware/uTimerLib.STM32.cpp: In member function 'void uTimerLib::_attachInterrupt_us(long unsigned int)':
/home/andre/Arduino/projects/libraries/uTimerLib/src/hardware/uTimerLib.STM32.cpp:57:52: error: no matching function for call to 'HardwareTimer::attachInterrupt(int, void (&)(HardwareTimer*))'
   57 |     Timer3->attachInterrupt(1, uTimerLib::interrupt);
      |                                                    ^
        
......

In file included from /home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:120:
/home/andre/Arduino/projects/libraries/uTimerLib/src/hardware/uTimerLib.STM32.cpp: In member function 'void uTimerLib::_attachInterrupt_s(long unsigned int)':
/home/andre/Arduino/projects/libraries/uTimerLib/src/hardware/uTimerLib.STM32.cpp:98:52: error: no matching function for call to 'HardwareTimer::attachInterrupt(int, void (&)(HardwareTimer*))'
   98 |     Timer3->attachInterrupt(1, uTimerLib::interrupt);
      |                                                    ^

Any ideas why ?

clearTimer() does not work in 1.6.5 ( it does in 1.6.4 )

hi,

TimerLib.clearTimer();

no longer works in 1.6.5. I have tried it with the below 2 sample programs. The test devices are ESP32.

clearTimer() works on version 1.6.4.

I have tried with both setInterval and setTimeout.

Example 1 (setInterval)

#include "Arduino.h"
#include "uTimerLib.h"

volatile unsigned long int prevMillis = 0;
volatile unsigned long int actMillis = 0;

void timed_function() {
  Serial.println(actMillis - prevMillis);
  prevMillis = actMillis;
  TimerLib.clearTimer();
}

void setup() {
  Serial.begin(115200);
  Serial.println("Timer started!");
  TimerLib.setInterval_s(timed_function, 2);
  prevMillis = millis();
}

void loop() {
  actMillis = millis();
}

Example 2 (setTimeout)

#include "Arduino.h"
#include "uTimerLib.h"

volatile unsigned long int prevMillis = 0;
volatile unsigned long int actMillis = 0;

void timed_function() {
  Serial.println(actMillis - prevMillis);
  TimerLib.clearTimer();
}

void setup() {
  Serial.begin(115200);
  Serial.println("Timer started!");
  TimerLib.setTimeout_s(timed_function, 2);
  prevMillis = millis();
}

void loop() {
  actMillis = millis();
}

Arduino_Core_STM32 compatibility

I've used this library with STM32Duino library, now I needed to switch to this, much better library:
https://github.com/stm32duino/Arduino_Core_STM32

  • but uTimerLib is not compatible with it ?
    (output from example)




/home/andre/Arduino/arduino-1.8.10/arduino-builder -dump-prefs -logger=machine -hardware /home/andre/Arduino/arduino-1.8.10/hardware -hardware /home/andre/.arduino15/packages -hardware /home/andre/Arduino/projects/hardware -tools /home/andre/Arduino/arduino-1.8.10/tools-builder -tools /home/andre/Arduino/arduino-1.8.10/hardware/tools/avr -tools /home/andre/.arduino15/packages -built-in-libraries /home/andre/Arduino/arduino-1.8.10/libraries -libraries /home/andre/Arduino/projects/libraries -fqbn=STM32:stm32:GenF1:pnum=BLUEPILL_F103C6,upload_method=swdMethod,xserial=generic,usb=none,xusb=FS,opt=osstd,rtlib=nano -ide-version=10810 -build-path /tmp/arduino -warnings=default -build-cache /tmp/arduino_cache_586316 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.CMSIS.path=/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1 -prefs=runtime.tools.CMSIS-5.5.1.path=/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1 -prefs=runtime.tools.STM32Tools.path=/home/andre/.arduino15/packages/STM32/tools/STM32Tools/1.3.1 -prefs=runtime.tools.STM32Tools-1.3.1.path=/home/andre/.arduino15/packages/STM32/tools/STM32Tools/1.3.1 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7 -prefs=runtime.tools.arm-none-eabi-gcc-8.2.1-1.7.path=/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7 -verbose /home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial/uTimerLib_setInterval_s_example_serial.ino
/home/andre/Arduino/arduino-1.8.10/arduino-builder -compile -logger=machine -hardware /home/andre/Arduino/arduino-1.8.10/hardware -hardware /home/andre/.arduino15/packages -hardware /home/andre/Arduino/projects/hardware -tools /home/andre/Arduino/arduino-1.8.10/tools-builder -tools /home/andre/Arduino/arduino-1.8.10/hardware/tools/avr -tools /home/andre/.arduino15/packages -built-in-libraries /home/andre/Arduino/arduino-1.8.10/libraries -libraries /home/andre/Arduino/projects/libraries -fqbn=STM32:stm32:GenF1:pnum=BLUEPILL_F103C6,upload_method=swdMethod,xserial=generic,usb=none,xusb=FS,opt=osstd,rtlib=nano -ide-version=10810 -build-path /tmp/arduino -warnings=default -build-cache /tmp/arduino_cache_586316 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.CMSIS.path=/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1 -prefs=runtime.tools.CMSIS-5.5.1.path=/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1 -prefs=runtime.tools.STM32Tools.path=/home/andre/.arduino15/packages/STM32/tools/STM32Tools/1.3.1 -prefs=runtime.tools.STM32Tools-1.3.1.path=/home/andre/.arduino15/packages/STM32/tools/STM32Tools/1.3.1 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7 -prefs=runtime.tools.arm-none-eabi-gcc-8.2.1-1.7.path=/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7 -verbose /home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial/uTimerLib_setInterval_s_example_serial.ino
Using board 'GenF1' from platform in folder: /home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0
Using core 'arduino' from platform in folder: /home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0
bash -c "[ -f /home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial/build_opt.h ] || (mkdir -p /tmp/arduino/sketch && touch /tmp/arduino/sketch/build_opt.h)"
Detecting libraries used...
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX /tmp/arduino/sketch/uTimerLib_setInterval_s_example_serial.ino.cpp -o /dev/null
Alternatives for uTimerLib.h: [[email protected]]
ResolveLibrary(uTimerLib.h)
  -> candidates: [[email protected]]
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX -I/home/andre/Arduino/projects/libraries/uTimerLib/src /tmp/arduino/sketch/uTimerLib_setInterval_s_example_serial.ino.cpp -o /dev/null
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX -I/home/andre/Arduino/projects/libraries/uTimerLib/src /home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp -o /dev/null
Generating function prototypes...
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -w -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -w -x c++ -E -CC -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX -I/home/andre/Arduino/projects/libraries/uTimerLib/src /tmp/arduino/sketch/uTimerLib_setInterval_s_example_serial.ino.cpp -o /tmp/arduino/preproc/ctags_target_for_gcc_minus_e.cpp
/home/andre/Arduino/arduino-1.8.10/tools-builder/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /tmp/arduino/preproc/ctags_target_for_gcc_minus_e.cpp
Compiling sketch...
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX -I/home/andre/Arduino/projects/libraries/uTimerLib/src /tmp/arduino/sketch/uTimerLib_setInterval_s_example_serial.ino.cpp -o /tmp/arduino/sketch/uTimerLib_setInterval_s_example_serial.ino.cpp.o
Compiling libraries...
Compiling library "uTimerLib"
/home/andre/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/8.2.1-1.7/bin/arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb @/tmp/arduino/sketch/build_opt.h -c -Os -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD -I/home/andre/Arduino/projects/libraries/uTimerLib/examples/uTimerLib_setInterval_s_example_serial -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/avr -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32 -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/LL -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/hid -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino/stm32/usb/cdc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Inc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/STM32F1xx_HAL_Driver/Src/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/STM32F1xx/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src -DSTM32F1xx -DARDUINO=10810 -DARDUINO_BLUEPILL_F103C6 -DARDUINO_ARCH_STM32 "-DBOARD_NAME=\"BLUEPILL_F103C6\"" -DSTM32F103x6 -DHAL_UART_MODULE_ENABLED -I/home/andre/.arduino15/packages/STM32/tools/CMSIS/5.5.1/CMSIS/Core/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Include/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/system/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/gcc/ -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/cores/arduino -I/home/andre/.arduino15/packages/STM32/hardware/stm32/1.7.0/variants/PILL_F103XX -I/home/andre/Arduino/projects/libraries/uTimerLib/src /home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp -o /tmp/arduino/libraries/uTimerLib/uTimerLib.cpp.o
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp: In member function 'void uTimerLib::_attachInterrupt_us(long unsigned int)':
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:247:18: error: 'TIMER_CH1' was not declared in this scope
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                  ^~~~~~~~~
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:247:18: note: suggested alternative: 'TIMER_NUM'
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                  ^~~~~~~~~
                  TIMER_NUM
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:247:29: error: 'TIMER_OUTPUTCOMPARE' was not declared in this scope
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                             ^~~~~~~~~~~~~~~~~~~
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:247:29: note: suggested alternative: 'TIMER_OUTPUT_COMPARE'
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                             ^~~~~~~~~~~~~~~~~~~
                             TIMER_OUTPUT_COMPARE
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:249:35: error: 'class HardwareTimer' has no member named 'setPeriod'; did you mean 'setOverflow'?
   uint16_t timerOverflow = Timer3.setPeriod(us);
                                   ^~~~~~~~~
                                   setOverflow
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:250:10: error: 'class HardwareTimer' has no member named 'setCompare'; did you mean 'setCount'?
   Timer3.setCompare(TIMER_CH1, timerOverflow);
          ^~~~~~~~~~
          setCount
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp: In member function 'void uTimerLib::_attachInterrupt_s(long unsigned int)':
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:578:18: error: 'TIMER_CH1' was not declared in this scope
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                  ^~~~~~~~~
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:578:18: note: suggested alternative: 'TIMER_NUM'
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                  ^~~~~~~~~
                  TIMER_NUM
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:578:29: error: 'TIMER_OUTPUTCOMPARE' was not declared in this scope
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                             ^~~~~~~~~~~~~~~~~~~
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:578:29: note: suggested alternative: 'TIMER_OUTPUT_COMPARE'
   Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
                             ^~~~~~~~~~~~~~~~~~~
                             TIMER_OUTPUT_COMPARE
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:579:10: error: 'class HardwareTimer' has no member named 'setPeriod'; did you mean 'setOverflow'?
   Timer3.setPeriod((unsigned long int) 1000000);   // 1s, in microseconds
          ^~~~~~~~~
          setOverflow
/home/andre/Arduino/projects/libraries/uTimerLib/src/uTimerLib.cpp:580:10: error: 'class HardwareTimer' has no member named 'setCompare'; did you mean 'setCount'?
   Timer3.setCompare(TIMER_CH1, 1000000);
          ^~~~~~~~~~
          setCount
Multiple libraries were found for "uTimerLib.h"
 Used: /home/andre/Arduino/projects/libraries/uTimerLib
Using library uTimerLib at version 1.2.2 in folder: /home/andre/Arduino/projects/libraries/uTimerLib 
exit status 1
Error compiling for board Generic STM32F1 series.

ESP32 Timer precision

During compilation I slapped my face into this message

"ESP8266 / ESP32 can only reach a ms resolution so any us interrupt will be rounded to that"

Reference:

#pragma message "ESP8266 / ESP32 can only reach a ms resolution so any us interrupt will be rounded to that"

Apparently, ESP32 does not have this limitation which sounds very weird for any microcontroller.
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/esp_timer.html#obtaining-current-time

Maybe the one used in this library is not the the right timer.
Am I missing something?

Thank you!

unprecisse timing on SAMD21 (Arduino Zero)

On an Arduino Zero (SAMD21) using this simple sketch shows 12 us or 13 us instead of 10 us:

#include "uTimerLib.h"

/* serial interface */
#define SERIAL_BAUD_RATE 115200 // baud = bits per second
#define SERIAL_TIMEOUT 1000 // timeout in 1e-6 seconds

#define LED_PIN 0

unsigned long microstime_old = 0;
unsigned long microstime_delta = 0;

void handler (void) {
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  const unsigned long mymicros = micros();
  microstime_delta = mymicros - microstime_old;
  microstime_old = mymicros;
}

void setup() {
  // set serial communication:
  Serial.begin(SERIAL_BAUD_RATE);
  Serial.setTimeout(SERIAL_TIMEOUT);
  delay(1000);
  pinMode(LED_PIN, OUTPUT);
  TimerLib.setInterval_us(handler, 10);
}

void loop() {
  const unsigned long mtime = millis();
  Serial.print("mtime: ");
  Serial.print(mtime);
  Serial.print(" SystemCoreClock: ");
  Serial.print(SystemCoreClock);
  Serial.print(" microstime_delta: ");
  Serial.println(microstime_delta);
  delay(1000);
}

A more precise measurement with an oscilloscope shows there is an additional time of about 2.5 us (= 2.5e-6 s). So the handler function is called every 12.5 us instead of every 10 us.

The same is true for longer periods. Also for TimerLib.setInterval_us(handler, 100); I get about 102.5 us instead of 100 us.

Unfortunately I have to say that this is not a hardware problem. With my own trigger implementation I get the setting as measurement.

And further it is not clear for my why uTimerLib shows this behavior. Maybe you have an idea?

Timer crash in esp32

I just downgraded a project from PlatformIO to Arduino IDE.

  • on PlatformIO I could install the 1.6.6
  • on Arduino IDE I now can see the 1.6.5 only

This project starts and stop a timer (50ms interval) with two serial port commands, very easy.

  • Using the 1.6.6 the second time I start the timer, the ESP32 crashes.
  • Using the 1.6.5 it works nicely

Note: I could understand the cause was the timer because I decoded the ESP32 backtrace using a tool.

I can't understand from the diff, how this can cause the crash, but apparently it does.
1.6.5...1.6.6#diff-e2a251b850a8484de4afd48e6d37cb2b3600da975fcfca21e8d35935c60c0b17R92

Small timeout trigger callback_function immediately (ATMEGA328)

I have noticed that when using the TimerLib.setTimeout_us(callback_function, microseconds) function with a very small time (I tested up to 8000 us), the callback function is called immediately after executing the command. This occurs both when calling the execute function in setup() and within an ISR. My initial application is a trigger pulse generator for SCRs in controlled rectifiers. I have not tested the maximum time that this occurs and even if other functions have similar behavior.

A workaround is to make the first callback function call a second (which occurs immediately), and the second call the correct function at the expected time, ie a callback function calling another operates normally. Below is a program to check for this failure, modify the commented lines accordingly to test the workaround. I tested it on an Arduino Nano v3 with ATMEGA328.

#include <uTimerLib.h>

const byte interruptPin = 2;
volatile unsigned long prevMicros;

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  
  attachInterrupt(digitalPinToInterrupt(interruptPin), pinInt, RISING);
  prevMicros=micros();
  TimerLib.setTimeout_us(timer, 8000); // change to trigger to workaround
}

void loop() {
}

void pinInt() {
  Serial.print("Interrupt set: ");
  Serial.println(micros()-prevMicros);
  prevMicros=micros();
  TimerLib.setTimeout_us(timer, 8000); // change to trigger to workaround
}

void trigger(void){ // that occurs immediately, but calls the next at correct time
  TimerLib.setTimeout_us(timer, 8000);
}

void timer(void){ // if that is called first, it occurs immediately
  Serial.print("Timer time: ");
  Serial.println(micros()-prevMicros);
}

The workaround isn't the best solution because it causes overhead, but I didn't figure out in the library why this occurs.

'Timer3' was not declared in this scope;

In file included from C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\uTimerLib.cpp:127:
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp: In member function 'void uTimerLib::_attachInterrupt_us(long unsigned int)':
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:52:4: error: 'Timer3' was not declared in this scope; did you mean 'TimerLib'?
52 | Timer3->setMode(1, TIMER_OUTPUT_COMPARE);
| ^~~~~~
| TimerLib
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:56:8: error: '_toInit' was not declared in this scope
56 | if (_toInit) {
| ^~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:58:43: error: 'interrupt' is not a member of 'uTimerLib'
58 | Timer3->attachInterrupt(1, uTimerLib::interrupt);
| ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp: In member function 'void uTimerLib::_attachInterrupt_s(long unsigned int)':
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:92:4: error: 'Timer3' was not declared in this scope; did you mean 'TimerLib'?
92 | Timer3->setMode(1, TIMER_OUTPUT_COMPARE);
| ^~~~~~
| TimerLib
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:97:8: error: '_toInit' was not declared in this scope
97 | if (_toInit) {
| ^~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:99:54: error: 'interrupt' is not a member of 'uTimerLib'
99 | Timer3->attachInterrupt((uint32_t) 1, uTimerLib::interrupt);
| ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp: In member function 'void uTimerLib::clearTimer()':
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:139:4: error: 'Timer3' was not declared in this scope; did you mean 'TimerLib'?
139 | Timer3->pause();
| ^~~~~~
| TimerLib
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp: At global scope:
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:174:23: error: no declaration matches 'callback_function_t uTimerLib::interrupt()'
174 | callback_function_t uTimerLib::interrupt() {
| ^~~~~~~~~
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\hardware/uTimerLib.STM32.cpp:174:23: note: no functions named 'callback_function_t uTimerLib::interrupt()'
In file included from C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\uTimerLib.cpp:36:
C:\Users\Administrator\Documents\Arduino\libraries\uTimerLib\src\uTimerLib.h:78:8: note: 'class uTimerLib' defined here
78 | class uTimerLib {
| ^~~~~~~~~
exit status 1

bluepill stm32f103cbt6 Arduino_Core_STM32 latest version

imprecisse timing on SAMD21 (Arduino Zero)

cf. #18

@Naguissa Sorry, to open again this problem.

But it is not solved and I can provide you a solution.

I had a deeper look in your code and find the reason an a solution.

My example in #18 was not correct and has a few typos.

Simple and dirty testing script would be:

#include "uTimerLib.h"

#define TRIGGERUS 1000000
#define CALCIT 256

#define SERIAL_BAUD_RATE 115200 // baud = bits per second
#define SERIAL_TIMEOUT 1000 // timeout in 1e-6 seconds

unsigned long start_microstime = 0;
unsigned long stop_microstime = 0;
volatile uint8_t it = 0;
volatile unsigned long duration = 0;

void handler (void) {
  if (it++ == 0) {
    stop_microstime = micros();
    duration = stop_microstime - start_microstime;
    start_microstime = stop_microstime;
  }
}

void setup() {
  Serial.begin(SERIAL_BAUD_RATE);
  Serial.setTimeout(SERIAL_TIMEOUT);
  while (!Serial);
  Serial.println("######");
  //TimerLib.setInterval_us(handler, TRIGGERUS);
  TimerLib.setInterval_s(handler, 1);
  delay(3*TRIGGERUS/1000);
}

void loop() {
    delay(max(3, CALCIT*TRIGGERUS/1000));
    static unsigned long mtime;
    mtime = millis();
    Serial.print("mtime: ");
    Serial.print(mtime);
    Serial.print(" microstime_delta: ");
    Serial.print(((double) duration) / CALCIT);
    Serial.println(" us");
}

As a reference we can do the same with another library (my one):

#include <fast_samd21_tc3.h>

#define TRIGGERUS 1000000
#define CALCIT 256

#define SERIAL_BAUD_RATE 115200 // baud = bits per second
#define SERIAL_TIMEOUT 1000 // timeout in 1e-6 seconds

unsigned long start_microstime = 0;
unsigned long stop_microstime = 0;
volatile uint8_t it = 0;
volatile unsigned long duration = 0;

void TC3_Handler(void) {
  if (it++ == 0) {
    stop_microstime = micros();
    duration = stop_microstime - start_microstime;
    start_microstime = stop_microstime;
  }
  TC3->COUNT16.INTFLAG.bit.MC0 = 1; // clears the interrupt
}

void setup() {
  Serial.begin(SERIAL_BAUD_RATE);
  Serial.setTimeout(SERIAL_TIMEOUT);
  while (!Serial);
  Serial.println("######");
  fast_samd21_tc3_configure(TRIGGERUS);
  delay(3*TRIGGERUS/1000);
}

void loop() {
    delay(max(3, CALCIT*TRIGGERUS/1000));
    static unsigned long mtime;
    mtime = millis();
    Serial.print("mtime: ");
    Serial.print(mtime);
    Serial.print(" microstime_delta: ");
    Serial.print(((double) duration) / CALCIT);
    Serial.println(" us");
}

These small scripts allow to get a approximated measurement:

set value uTimerLib v1.7.1 fast_samd21_tc v0.2.3
1000000 us / 1 s 1000021.33 us 1000000.00 us
100000 us 100011.22 us 100000.00 us
10000 us 10002.66 us 10000.00 us
1000 us 1002.69 us 1000.00 us
10 us 12.66 us 10.00 us

The reason for the additional time in uTimerLib is _TC->COUNT.reg = 0;. This is at the moment set at a more or less random time. It depends on all the if clauses. And during running these if clauses some time is lost. Using TC_CTRLA_WAVEGEN_MFRQ instead of TC_CTRLA_WAVEGEN_NFRQ makes setting _TC->COUNT.reg = 0; unnecessary.

During reading your code I also find a few points which I tried to enhance.

ARDUINO_STM32 - setInterval_s not correctly trigged

I have try with Maple compatible board.
When call with any value, it trig on every 1 second.

As example:
void setup() {
Serial.begin(57600);
TimerLib.setInterval_s(timed_function, 2);
prevMillis = millis();
}

Will result in 1000

However, TimerLib.setInterval_us return correctly.
TimerLib.setInterval_us(timed_function, 2000000);
Will result in 2000

changing timer via serial usb?

Hi

Thank you for your lib (and your time on it!).

A long-time developer but new to arduino world, I wish to create a sketch which:

  • writes to gpio on a high frequency (every 20-30 microsec a batch of 3-5 writes on as many pins) and,
  • from time to time, the host computer connected via serial (actually serial USB) will ask the sketch to change the high/low state of these pins.

All this is done on a maple mini board (stm32), connected via usb to a PC.

  • You lib would be perfectly fitted for this purpose, isn't it?
  • Could you advise me on how the sktech should "listen" to the host computer for the change? serialevent?
    You advice would be welcome, as I'm really starting with arduino/stm32.

STM32 compilation fails with error about Timer3

uTimerLib.h line 150 references _VARIANT_ARDUINO_STM32_ and this is for some reason not defined when I use Nucleo32 / NucleoG031K8, so the library compile borks because it doesn't know anything about Timer3.

As a workaround I created a platform.local.txt file with a single line

compiler.extra_flags=-mcpu={build.mcu} {build.flags.fp} -DUSE_FULL_LL_DRIVER -D_VARIANT_ARDUINO_STM32_ -mthumb "@{build.opt.path}"

To clean this up I would suggest to use ARDUINO_ARCH_STM32 instead of _VARIANT_ARDUINO_STM32_.

SAMD21 Timing Incorrect

Thanks for all your work in creating this library. It saved me a lot of time in figuring out how to do a simple timed interrupt on a SAMD21 (MKR 1010 to be specific).

However, in my checks I found that the timing provided by the setInterval_us command was off and actually running very slowly. My application initially required a 1000us interrupt and I found (via micros()) that I could only get 21845 us. In fact, it didn’t matter what value I passed in below 21845, I’d only got 21845! So upon further review of the code (and some trial and error), I’ve come to the conclusion that you need to reset the COUNT register to 0 on every interrupt. (In practice I made the change to only the _loadRemainder() routine and in the case where both _remainder and _overflows are 0 but I think just always reseting would work. And not changing both locations does leave timing errors). With both locations fixed, I now have micros() reporting desired timing to around 3 microseconds from 1000 microseconds to 1000000 micros = 1 sec. I did not review the other prescaler value/setInterval routine but I can only assume that the code is the same so it has the same issue.

(As an aside, I think you can get your code even smaller. Instead of running two different interrupt types, you could run OVF all the time and load COUNT = (_overflows > 0 ? 0 : (65535 - _remainder + 1)) assuming the counter is counting up. You might need a flag to tell the interrupt handler whether the remainder portion or overflow portion was running when the OVF occurred so that you can reload the _overflows and _remainder variables but it seems a bit simpler in general to me. I don't think this would change timing but I also haven't tried this method either.)

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.