use a higher-precision timer implementation

This commit is contained in:
Peter Cai 2021-06-10 18:36:33 +08:00
parent 7bd6bb1073
commit 8e1b13345a
5 changed files with 13 additions and 3 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "ArduinoMIDI"] [submodule "ArduinoMIDI"]
path = ArduinoMIDI path = ArduinoMIDI
url = https://github.com/FortySevenEffects/arduino_midi_library url = https://github.com/FortySevenEffects/arduino_midi_library
[submodule "TimerCounter"]
path = TimerCounter
url = https://github.com/ElectricRCAircraftGuy/eRCaGuy_TimerCounter

View File

@ -24,6 +24,7 @@ VPATH := $(foreach path, ${VPATH}, ${path} $(shell find ${path} -type d))
# the VPATH list # the VPATH list
VPATH += ${PWD} \ VPATH += ${PWD} \
${PWD}/ArduinoMIDI/src \ ${PWD}/ArduinoMIDI/src \
${PWD}/TimerCounter \
# Build parameters # Build parameters
BUILD_DIR := out BUILD_DIR := out
@ -36,7 +37,7 @@ INCS := \
$(foreach path, ${VPATH}, -I ${path}) \ $(foreach path, ${VPATH}, -I ${path}) \
$(foreach path, ${VARIANTS}, -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 # Generate sections for each function and variable
# so that LD can eliminate unused functions and variables # so that LD can eliminate unused functions and variables
CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -ffunction-sections -fdata-sections

1
TimerCounter Submodule

@ -0,0 +1 @@
Subproject commit f2651c78bce8a1b5f69d707ac3d019ef17ab8055

View File

@ -10,7 +10,8 @@
], ],
"defines": [ "defines": [
"__ATmega328P__", "__ATmega328P__",
"__AVR_ATmega328P__" "__AVR_ATmega328P__",
"ARDUINO=1000",
] ]
} }
] ]

View File

@ -1,5 +1,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <MIDI.h> #include <MIDI.h>
#include <eRCaGuy_Timer2_Counter.h>
#include "motor_control.h" #include "motor_control.h"
#define NUM_MOTORS 4 #define NUM_MOTORS 4
@ -53,6 +54,9 @@ int main() {
// Needed for some functions to work (like micros) // Needed for some functions to work (like micros)
init(); init();
// Timer based on Timer 2 (this breaks PWM output)
timer2.setup();
// Enable the motor drivers // Enable the motor drivers
pinMode(8, OUTPUT); pinMode(8, OUTPUT);
digitalWrite(8, LOW); digitalWrite(8, LOW);
@ -70,7 +74,7 @@ int main() {
MIDI.setHandleNoteOff(midi_note_off); MIDI.setHandleNoteOff(midi_note_off);
while (true) { 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); handle_tick(cur_micros);
MIDI.read(); MIDI.read();
} }