ESP32驱动AS5048A和oled屏幕

张开发
2026/5/20 18:35:40 15 分钟阅读
ESP32驱动AS5048A和oled屏幕
使用platformio引脚自己看代码main.cpp#include Arduino.h #include SPI.h #include Wire.h #include Adafruit_GFX.h #include Adafruit_SSD1306.h // --- OLED 配置 --- #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define I2C_SDA 9 #define I2C_SCL 10 #define OLED_ADDR 0x3C // OLED I2C地址 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, Wire, -1); // --- AS5048A 硬件引脚定义 --- #define AS5048_SS 4 // 片选引脚 (CS) // --- 全局对象 --- SPIClass as5048Spi(HSPI); // 变量定义 uint16_t rawData 0; float angle 0.0; unsigned long lastDisplayTime 0; const unsigned long displayInterval 50; // 50ms刷新一次 /** * 读取 AS5048A 原始数据 * return 14位原始角度值 (0-16383) */ uint16_t ReadAS5048A() { uint16_t command 0xFFFF; uint16_t result 0; as5048Spi.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1)); digitalWrite(AS5048_SS, LOW); result as5048Spi.transfer16(command); digitalWrite(AS5048_SS, HIGH); as5048Spi.endTransaction(); // 屏蔽高2位保留低14位数据 result result 0x3FFF; return result; } /** * 初始化OLED显示器 * return 初始化成功返回true失败返回false */ bool initOLED() { Wire.begin(I2C_SDA, I2C_SCL); if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { Serial.println(OLED初始化失败); return false; } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println(AS5048A Ready!); display.display(); delay(2000); return true; } /** * 在OLED上显示数据 * param raw 原始数据值 * param ang 角度值 */ void displayData(uint16_t raw, float ang) { display.clearDisplay(); // 显示标题 display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println(AS5048A Angle Sensor); // 画分隔线 display.drawLine(0, 10, 128, 10, SSD1306_WHITE); // 显示原始数据 (十进制) display.setCursor(0, 16); display.print(Raw: ); display.println(raw); // 显示原始数据 (十六进制) display.setCursor(0, 26); display.print(Hex: 0x); display.println(raw, HEX); // 显示角度 display.setCursor(0, 36); display.print(Angle: ); display.print(ang, 2); display.println( ang); // 显示简单的进度条或指针指示 display.drawRect(0, 48, 128, 12, SSD1306_WHITE); int barWidth map(raw, 0, 16383, 0, 126); display.fillRect(1, 49, barWidth, 10, SSD1306_WHITE); display.display(); } void setup() { // 1. 初始化串口 Serial.begin(115200); Serial.println(AS5048A OLED 显示系统启动); Serial.println(); // 2. 初始化OLED if (!initOLED()) { Serial.println(OLED初始化失败系统停止); while(1) { delay(100); Serial.println(等待OLED连接...); } } Serial.println(OLED初始化成功); // 3. 初始化 AS5048A SPI 引脚 pinMode(AS5048_SS, OUTPUT); digitalWrite(AS5048_SS, HIGH); // 4. 启动 SPI (ESP32: SCK5, MISO18, MOSI23, SS4) as5048Spi.begin(5, 18, 23, AS5048_SS); Serial.println(AS5048A 初始化成功); Serial.println(格式: 原始值 | 角度(°)); Serial.println(); delay(1000); } void loop() { // 读取角度传感器数据 rawData ReadAS5048A(); // 计算角度 (14位: 0-16383 对应 0-360度) angle (float)rawData / 16384.0 * 360.0; // 串口打印数据用于调试 Serial.print(Raw: ); Serial.print(rawData); Serial.print(\tHex: 0x); Serial.print(rawData, HEX); Serial.print(\tAngle: ); Serial.print(angle, 2); Serial.println(°); // 更新OLED显示 displayData(rawData, angle); delay(displayInterval); }platformio.ini; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:esp32dev] platform espressif32 board esp32dev framework arduino lib_deps adafruit/Adafruit SSD1306^2.5.10 monitor_speed 115200

更多文章