PlatformIO/Arduino 中 Teensy 4.x 正交编码器最小示例
此示例使用 PlatformIO 和 Teensy-4.x-Quad-Encoder-Library 在引脚 0 和 1 上实现硬件正交编码器。记住如果你使用 5V 编码器信号,teensy 将被损坏。你只能使用 3.3V 信号!
main.cpp
#include <Arduino.h>
#include <QuadEncoder.h>
QuadEncoder encoder(1, 0, 1, 0);
void setup() {
Serial.begin(115200);
encoder.setInitConfig();
encoder.init();
}
void loop() {
int32_t position = encoder.read();
// 计算以 mm 为单位的位置并打印
constexpr float mm_per_count = 0.0012;
float mm = position * mm_per_count;
Serial.printf("Position: %+3.4f (count %ld)\\r\\n", mm, position);
// 每 50ms 打印一次
delay(50);
}platformio.ini
[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
monitor_speed = 115200
lib_deps =
git+https://github.com/mjs513/Teensy-4.x-Quad-Encoder-LibraryCheck out similar posts by category:
C/C++, Electronics, Embedded, PlatformIO, Teensy
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow