MCP45HVX1测试

This commit is contained in:
2025-01-15 16:52:01 +08:00
parent 5143b04dec
commit 30ad59c3a7
4 changed files with 362 additions and 4 deletions

View File

@ -15,3 +15,4 @@ framework = arduino
lib_deps = lib_deps =
robtillaart/DS18B20@^0.2.4 robtillaart/DS18B20@^0.2.4
esphome/ESPAsyncWebServer-esphome@^3.3.0 esphome/ESPAsyncWebServer-esphome@^3.3.0
monitor_speed = 115200

193
src/MCP45HVX1.cpp Normal file
View File

@ -0,0 +1,193 @@
/******************************************************
Arduino library for MCP45HVX1 digital potentiometers
Author: Jonathan Dempsey JDWifWaf@gmail.com
Version: 1.0.1
License: Apache 2.0
*******************************************************/
#include "MCP45HVX1.h"
#define DEBUG 0
/* 7 Bit I2C Operation Components ......................................... */
/* TCON configuration.................. */
#define TCON_R0HW (0x08) // Shutdown Resistor Force
#define TCON_R0A (0x04) // Terminal A Connection
#define TCON_R0W (0x02) // Wiper Connection
#define TCON_R0B (0x01) // Terminal B Connection
#define GCALL_TCON (0x60)
#define GCALL_WIPER (0x40)
#define GCALL_WIPERUP (0x42)
#define GCALL_WIPERDWN (0x44)
#define GCALL_COM_WRITE (0x02)
#define GCALL_COM_RWRITE (0x03)
#define GCALL_COM_WIPERINC (0x42)
#define GCALL_COM_WIPERDEC (0x44)
#define MEM_WIPER (0x00)
#define MEM_TCON (0x40)
#define COM_WRITE (0x00)
#define COM_READ (0x0C)
#define COM_WIPERINC (0x04)
#define COM_WIPERDEC (0x08)
/* Setup ............................................................... */
MCP45HVX1::MCP45HVX1(uint8_t address) : _address(address) {};
void MCP45HVX1::begin(int sda, int scl, TwoWire &inWire) {
MCPWire = &inWire; // 使用传递的 I2C 对象实例
if (sda != -1 && scl != -1) {
MCPWire->begin(sda, scl); // 自定义 SDA 和 SCL 引脚
} else {
MCPWire->begin(); // 默认 I2C 引脚初始化
}
MCPWire->setClock(MCPCSPEED); // 设置 I2C 时钟速度
}
/* Wiper Register..........................................................*/
void MCP45HVX1::writeWiper(uint8_t wiperValue)
{
this->MCPWire->beginTransmission(_address);
this->MCPWire->write(MEM_WIPER | COM_WRITE);
this->MCPWire->write(wiperValue);
this->MCPWire->endTransmission();
}
uint8_t MCP45HVX1::readWiper()
{
uint8_t buff = 0;
this->MCPWire->beginTransmission(_address);
this->MCPWire->write(MEM_WIPER | COM_READ);
this->MCPWire->endTransmission(false);
this->MCPWire->requestFrom(_address, (uint8_t)2);
if(this->MCPWire->available())
{
buff = this->MCPWire->read(); // First byte is 0x00
#if DEBUG
Serial.print("\nRead Wiper MSB: ");
Serial.println(buff);
#endif
buff = this->MCPWire->read(); // Second byte contains the wiper value
#if DEBUG
Serial.print("Read Wiper LSB: ");
Serial.println(buff);
#endif
}
return buff;
}
void MCP45HVX1::incrementWiper(uint8_t incriments)
{
this->MCPWire->beginTransmission(_address);
for(uint8_t x = 0; x < incriments; x++)
{
this->MCPWire->write(MEM_WIPER | COM_WIPERINC);
}
this->MCPWire->endTransmission();
}
void MCP45HVX1::decrementWiper(uint8_t decriments)
{
this->MCPWire->beginTransmission(_address);
for(uint8_t x = 0; x < decriments; x++)
{
this->MCPWire->write(MEM_WIPER | COM_WIPERDEC);
}
this->MCPWire->endTransmission();
}
/* TCON Register...........................................................*/
uint8_t MCP45HVX1::readTCON()
{
uint8_t buff = 0;
this->MCPWire->beginTransmission(_address);
this->MCPWire->write(MEM_TCON | COM_READ);
this->MCPWire->endTransmission(false);
this->MCPWire->requestFrom(_address, (uint8_t)2);
if(this->MCPWire->available())
{
buff = this->MCPWire->read(); // First byte is always 0x00
#if DEBUG
Serial.print("\nRead TCON MSB: ");
Serial.println(buff);
#endif
buff = this->MCPWire->read(); // Second byte contains the wiper value (for compatability)
#if DEBUG
Serial.print("Read TCON LSB: ");
Serial.println(buff);
#endif
}
return buff;
}
void MCP45HVX1::defaultTCON()
{
this->TCON_lib_reg.R0HW = true;
this->TCON_lib_reg.R0A = true;
this->TCON_lib_reg.R0B = true;
this->TCON_lib_reg.R0W = true;
write_TCON_Register();
}
void MCP45HVX1::writeTCON(TCON_Register *inReg)
{
memcpy(&this->TCON_lib_reg, inReg, sizeof(this->TCON_lib_reg));
write_TCON_Register();
}
void MCP45HVX1::write_TCON_R0HW(bool isOn)
{
this->TCON_lib_reg.R0HW = isOn;
write_TCON_Register();
}
void MCP45HVX1::write_TCON_R0A(bool isOn)
{
this->TCON_lib_reg.R0A = isOn;
write_TCON_Register();
}
void MCP45HVX1::write_TCON_R0W(bool isOn)
{
this->TCON_lib_reg.R0W = isOn;
write_TCON_Register();
}
void MCP45HVX1::write_TCON_R0B(bool isOn)
{
this->TCON_lib_reg.R0B = isOn;
write_TCON_Register();
}
void MCP45HVX1::write_TCON_Register()
{
uint8_t buff = 0xFF;
this->TCON_lib_reg.R0HW ? (buff |= TCON_R0HW) : (buff ^= TCON_R0HW);
this->TCON_lib_reg.R0A ? (buff |= TCON_R0A) : (buff ^= TCON_R0A);
this->TCON_lib_reg.R0B ? (buff |= TCON_R0B) : (buff ^= TCON_R0B);
this->TCON_lib_reg.R0W ? (buff |= TCON_R0W) : (buff ^= TCON_R0W);
#if DEBUG
Serial.print("Writing TCON: "); Serial.println(buff);
#endif
this->MCPWire->beginTransmission(_address);
this->MCPWire->write(MEM_TCON | COM_WRITE);
this->MCPWire->write(buff);
this->MCPWire->endTransmission();
}

94
src/MCP45HVX1.h Normal file
View File

@ -0,0 +1,94 @@
/******************************************************
Arduino library for MCP45HVX1 digital potentiometers
Author: Jonathan Dempsey JDWifWaf@gmail.com
Version: 1.0.1
License: Apache 2.0
*******************************************************/
#ifndef _MCP45HVX1_H
#define _MCP45HVX1_H
#include <Arduino.h>
#include <Wire.h>
#define MCPCSPEED 100000 // I2C clock speed
class MCP45HVX1
{
public:
/* TCON Register Resistor Configuration ............................... */
typedef struct
{
bool R0HW = true;
bool R0A = true;
bool R0B = true;
bool R0W = true;
}TCON_Register;
/* Setup ............................................................... */
MCP45HVX1(uint8_t address = 0x3C);
void begin(int sda = -1, int scl = -1, TwoWire &inWire = Wire);
/* Wiper Register ...................................................... */
uint8_t readWiper();
void writeWiper(uint8_t wiperValue);
void incrementWiper(uint8_t incriments = 1);
void decrementWiper(uint8_t decriments = 1);
/* TCON Register ....................................................... */
uint8_t readTCON();
void writeTCON(TCON_Register *inReg);
void defaultTCON();
void inline shutdown()
{
write_TCON_R0HW(false);
};
void inline startup()
{
write_TCON_R0HW(true);
};
void inline connectTerminalA()
{
write_TCON_R0A(true);
};
void inline disconnectTerminalA()
{
write_TCON_R0A(false);
};
void inline connectTerminalB()
{
write_TCON_R0B(true);
};
void inline disconnectTerminalB()
{
write_TCON_R0B(false);
};
void inline connectWiper()
{
write_TCON_R0W(true);
};
void inline disconnectWiper()
{
write_TCON_R0W(false);
};
protected:
private:
uint8_t _address;
TwoWire* MCPWire;
TCON_Register TCON_lib_reg;
void write_TCON_Register();
void write_TCON_R0HW(bool state);
void write_TCON_R0A(bool state);
void write_TCON_R0W(bool state);
void write_TCON_R0B(bool state);
};
#endif

View File

@ -7,6 +7,9 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
//#include "TCJ_Show.h" //#include "TCJ_Show.h"
//#include "ESP32_WebServer.h" //#include "ESP32_WebServer.h"
#include "MCP45HVX1.h"
MCP45HVX1 digiPot(0x3C);
AsyncWebServer server(80); AsyncWebServer server(80);
@ -14,7 +17,7 @@ const char* ssid = "SERVIRST-CT";
const char* password = "servirst8888"; const char* password = "servirst8888";
#define ONE_WIRE_BUS 4 #define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS); OneWire oneWire(ONE_WIRE_BUS);
DS18B20 sensor(&oneWire); DS18B20 sensor(&oneWire);
@ -121,6 +124,72 @@ void TJC_Show(void)
TJC.print(str); TJC.print(str);
} }
void MCP45HVX1_Test(void) //MCP45HVX1测试
{
digiPot.begin(5,4);
Serial.println("....... Functionality Test Begin ..........");
/* Wiper ........................... */
digiPot.writeWiper(127); // Baseline Establish
Serial.println("\n----- Wiper Register ----");
Serial.print("readWiper: ");
Serial.println(digiPot.readWiper());
Serial.print("writeWiper: ");
digiPot.writeWiper(200);
Serial.println(digiPot.readWiper());
Serial.print("incrementWiper: ");
digiPot.incrementWiper();
Serial.println(digiPot.readWiper());
Serial.print("incrementWiper by 2: ");
digiPot.incrementWiper(2);
Serial.println(digiPot.readWiper());
Serial.print("decrementWiper: ");
digiPot.decrementWiper();
Serial.println(digiPot.readWiper());
Serial.print("decrementWiper by 2: ");
digiPot.decrementWiper(2);
Serial.println(digiPot.readWiper());
/* TCON .......................... */
Serial.println("\n----- TCON Register ----");
digiPot.disconnectTerminalA();
Serial.print("disconnectTerminalA: ");
Serial.println(digiPot.readTCON());
digiPot.connectTerminalA();
digiPot.disconnectTerminalB();
Serial.print("disconnectTerminalB: ");
Serial.println(digiPot.readTCON());
digiPot.connectTerminalB();
digiPot.disconnectWiper();
Serial.print("disconnectWiper: ");
Serial.println(digiPot.readTCON());
digiPot.connectWiper();
digiPot.shutdown();
Serial.print("shutdown: ");
Serial.println(digiPot.readTCON());
digiPot.startup();
digiPot.startup();
Serial.print("startup: ");
Serial.println(digiPot.readTCON());
digiPot.startup();
digiPot.defaultTCON();
Serial.print("default: ");
Serial.println(digiPot.readTCON());
Serial.println("\n........ Functionality Test End ...........");
}
void setup(void) void setup(void)
{ {
Serial.begin(115200); Serial.begin(115200);
@ -132,9 +201,10 @@ void setup(void)
//sensor.begin(); //sensor.begin();
bh1750_init(bh1750_a, bh1750_b, 19, 18, 21, 20); //1750初始化。sda, scl bh1750_init(bh1750_a, bh1750_b, 19, 18, 21, 20); //1750初始化。sda, scl
Serial.println("BH1750 initialized");
WebServer_Init(ssid, password); //WebServer_Init(ssid, password);
MCP45HVX1_Test();
} }