From 8e1b13345a6c9d3c8323001df0633b309a0e5620 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Thu, 10 Jun 2021 18:36:33 +0800 Subject: [PATCH] use a higher-precision timer implementation --- .gitmodules | 3 +++ Makefile | 3 ++- TimerCounter | 1 + c_cpp_properties.json.template | 3 ++- main.cpp | 6 +++++- 5 files changed, 13 insertions(+), 3 deletions(-) create mode 160000 TimerCounter diff --git a/.gitmodules b/.gitmodules index ba18018..46c31ab 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "ArduinoMIDI"] path = ArduinoMIDI url = https://github.com/FortySevenEffects/arduino_midi_library +[submodule "TimerCounter"] + path = TimerCounter + url = https://github.com/ElectricRCAircraftGuy/eRCaGuy_TimerCounter diff --git a/Makefile b/Makefile index a9909a1..d1d8474 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ VPATH := $(foreach path, ${VPATH}, ${path} $(shell find ${path} -type d)) # the VPATH list VPATH += ${PWD} \ ${PWD}/ArduinoMIDI/src \ + ${PWD}/TimerCounter \ # Build parameters BUILD_DIR := out @@ -36,7 +37,7 @@ INCS := \ $(foreach path, ${VPATH}, -I ${path}) \ $(foreach path, ${VARIANTS}, -I ${path}) \ -CFLAGS := -O3 -DF_CPU=16000000UL ${MMCU} ${INCS} +CFLAGS := -O3 -DF_CPU=16000000UL -DARDUINO=1000 ${MMCU} ${INCS} # Generate sections for each function and variable # so that LD can eliminate unused functions and variables CFLAGS += -ffunction-sections -fdata-sections diff --git a/TimerCounter b/TimerCounter new file mode 160000 index 0000000..f2651c7 --- /dev/null +++ b/TimerCounter @@ -0,0 +1 @@ +Subproject commit f2651c78bce8a1b5f69d707ac3d019ef17ab8055 diff --git a/c_cpp_properties.json.template b/c_cpp_properties.json.template index 9474bf5..6a044d1 100644 --- a/c_cpp_properties.json.template +++ b/c_cpp_properties.json.template @@ -10,7 +10,8 @@ ], "defines": [ "__ATmega328P__", - "__AVR_ATmega328P__" + "__AVR_ATmega328P__", + "ARDUINO=1000", ] } ] diff --git a/main.cpp b/main.cpp index 5a8d02c..d896d2f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "motor_control.h" #define NUM_MOTORS 4 @@ -53,6 +54,9 @@ int main() { // Needed for some functions to work (like micros) init(); + // Timer based on Timer 2 (this breaks PWM output) + timer2.setup(); + // Enable the motor drivers pinMode(8, OUTPUT); digitalWrite(8, LOW); @@ -70,7 +74,7 @@ int main() { MIDI.setHandleNoteOff(midi_note_off); while (true) { - unsigned long cur_micros = micros(); + unsigned long cur_micros = timer2.get_count() / 2ul; // The unit of get_count is 0.5us handle_tick(cur_micros); MIDI.read(); }