Arduino~1~ 辅助电源1.0
功能:
1.带液晶显示输出电压,输出电流
2.电压电流按键设定
材料:
ESP32驱动CSD203
ESP32驱动液晶SSD1312
ESP32使用的引脚:
LCD_CLK |
13 |
LCD_SDA |
12 |
LCD_CS |
14 |
LCD_DC |
27 |
LCD_RST |
26 |
IIC_SCL |
22 |
IIC_SDA |
21 |
Arduino代码:
int i;
// define string buffer
char VoltageChar[40];
// 电压采样Buffer
char CurrentChar[40];
// 电流采样Buffer
U8G2_SSD1312_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 12, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 26);
void setup(void) {
//初始化IIC
Wire.begin();
//创建名为CSD的CSD203对象
CSD203 CSD;
CSD_CONFIG CSD_ConfigStruct;
CSD_ConfigStruct.RST=CSD_CONFIG_RST;
CSD_ConfigStruct.Average=CSD_CONFIG_Avg1;
CSD_ConfigStruct.VBUS_Conv_Time=CSD_CONFIG_VBUS_CT1_1mS;
CSD_ConfigStruct.VShunt_Conv_Time=CSD_CONFIG_VShunt_CT1_1mS;
CSD_ConfigStruct.Mode=CSD_CONFIG_ShuntBus_CON;
CSD_ConfigStruct.DeviceADDR=64;
CSD_ConfigStruct.CurrentLSB=CSD_CONFIG_CurrentLsb1mA;
CSD_ConfigStruct.Rshunt=CSD_CONFIG_Rs_2mR;
CSD.begin(&CSD_ConfigStruct);
delay(1000);
u8g2.begin();
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_wqy13_t_gb2312); // choose a suitable font
u8g2.drawStr(0,32," hello analogysemi"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
//CSD.begin();
}
void loop(void) {
//using sprintf conversion string
i++;
sprintf(VoltageChar, "Volt: %d ", i);
sprintf(CurrentChar, " I : %d ", i*2);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_wqy13_t_gb2312); // choose a suitable font
u8g2.drawStr(0,21,VoltageChar); // write something to the internal memory
//u8g2.sendBuffer(); // transfer internal memory to the display
u8g2.drawStr(0,32,CurrentChar); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
}
ESP8266/ESP32 开发板管理器地址:
https://dl.espressif.com/dl/package_esp32_index.json
http://arduino.esp8266.com/stable/package_esp8266com_index.json
原生的Arduino没有ESP32和ESP8266的arduino的包,需要添加一下
U8G2 API参考
https://github.com/olikraus/u8g2/wiki/u8g2reference#begin
CSD203 Arduino驱动
//#include <_types/_uint16_t.h>
void CSD203::begin(CSD_CONFIG *CSD203_CFG)
{
uint16_t Data=0,ADDR=0;
Data|=(CSD203_CFG->RST)<<15;
Data|=(CSD203_CFG->Average)<<9;
Data|=(CSD203_CFG->VBUS_Conv_Time)<<5;
Data|=(CSD203_CFG->VShunt_Conv_Time)<<2;
Data|=CSD203_CFG->Mode;
ADDR=(CSD203_CFG->DeviceADDR);
IIC_DUT_W(ADDR,CONFIGURATION,Data);
Data=CalParam/((CSD203_CFG->CurrentLSB)*(CSD203_CFG->Rshunt));
IIC_DUT_W(ADDR,CALIBRATION,Data);
}
void CSD203::IIC_DUT_W(u_int8_t DUT_ADDR,u_int8_t REG,u_int16_t Data)
{
Wire.beginTransmission(DUT_ADDR);
//写寄存器地址
Wire.write(CONFIGURATION);
Wire.write(Data);
Wire.endTransmission();
}
uint16_t CSD203::IIC_DUT_R(u_int8_t DUT_ADDR,u_int8_t REG)
{
uint16_t Data;
Wire.beginTransmission(DUT_ADDR);
//写寄存器地址
Wire.write(REG);
//结束传输
Wire.endTransmission();
Wire.beginTransmission(DUT_ADDR);
//从这个地址获取两个Byte的数据
Wire.requestFrom(DUT_ADDR,2);
if(Wire.available())
{
Data=Wire.read();
}
Wire.endTransmission();
return Data;
}
/*Read Vbus*/
uint16_t CSD203::CSD203_ReadVbus(CSD_CONFIG *CSD203_CFG)
{
uint16_t Data=0,ADDR=0;
ADDR=(CSD203_CFG->DeviceADDR);
Data=IIC_DUT_R(ADDR,BUS_VOLTAGE);
return Data;
}
/*Read Rshunt*/
uint16_t CSD203::CSD203_ReadRshunt(CSD_CONFIG *CSD203_CFG)
{
uint16_t Data=0,ADDR=0;
ADDR=(CSD203_CFG->DeviceADDR);
Data=IIC_DUT_R(ADDR,SHUNT_VOLTAGE);
return Data;
}
/*Read Power*/
uint16_t CSD203::CSD203_ReadPower(CSD_CONFIG *CSD203_CFG)
{
uint16_t Data=0,ADDR=0;
ADDR=(CSD203_CFG->DeviceADDR);
Data=IIC_DUT_R(ADDR,POWER);
return Data;
}
uint16_t CSD203::CSD203_ReadCurrent(CSD_CONFIG *CSD203_CFG)
{
uint16_t Data=0,ADDR=0;
ADDR=(CSD203_CFG->DeviceADDR);
Data=IIC_DUT_R(ADDR,CURRENT);
return Data;
}
//#include <_types/_uint16_t.h>
/* CSD203 Regsistor map */
/*Read Only*/
/*Read Only*/
/*Calibration Calculation parameter*/
/* This Parameter Gain*10000K*/
/* ↓ add your shunt here ↓ */
/*Calibration Calculation parameter*/
/* CSD203 Regsistor Config*/
/******************/
/*CSD Alert Option*/
/******************/
/*Vshunt Voltage Over Voltage*/
/*Vshunt Voltage Under Voltage*/
/*VBus Voltage Over Voltage*/
/*VBus Voltage Under Voltage*/
/* Power Over Limit */
/* ADC Coversion Ready */
/* ALERT Function Flag and Alert latch Work Together */
/*Power over flow */
/*Alert Pin */
/*CSD Basic Configuration struct*/
typedef struct{
uint8_t DeviceADDR;
uint8_t RST;
uint8_t Average;
uint8_t VBUS_Conv_Time;
uint8_t VShunt_Conv_Time;
uint8_t Mode;
uint16_t CurrentLSB;
uint16_t Rshunt;
}CSD_CONFIG;
/*CSD Basic Configuration struct*/
/*CSD ALERT Configuration struct*/
typedef struct{
uint8_t VShunt_OVA;
uint8_t VShuntUVA;
uint8_t VBUS_OVA;
uint8_t VBUS_UVA;
uint8_t Power_OVER_Limit;
uint8_t CNVR;
uint8_t ALERT_FLAG;
uint8_t CNVR_FLAG;
uint8_t OVF;
uint8_t APOL;
uint8_t ALERT_Latch;
}CSD_ALERT;
/*CSD ALERT Configuration struct*/
/// @brief
class CSD203 {
public:
void begin(CSD_CONFIG *CSD203_CFG); //初始化
uint16_t CSD203_ReadVbus(CSD_CONFIG *CSD203_CFG);// 获取电压值
uint16_t CSD203_ReadPower(CSD_CONFIG *CSD203_CFG); // 获取电流值
uint16_t CSD203_ReadRshunt(CSD_CONFIG *CSD203_CFG); //获取功率值
uint16_t CSD203_ReadCurrent(CSD_CONFIG *CSD203_CFG); //获取功率值
private:
//把Wire作为私有变量
void IIC_DUT_W(u_int8_t DUT_ADDR,u_int8_t REG,u_int16_t Data); //
uint16_t IIC_DUT_R(u_int8_t DUT_ADDR,u_int8_t REG);//
};
实物效果
没接CSD203,大概看下IIC驱动是否正常,是正常的效果如下
参考文档
Arduino Cookbook
GIthub地址:
https://github.com/xutong-analog/fantasy/blob/main/Arduino/CSD203.h
备注
把String buffer替换为电压和电流采集结果即可,后续添加其他功能。
2023-4-15
以下是广告:
类比半导体推出生物电势测量模拟前端AFE96x,助力高端脑电芯片国产替代
喜讯!类比半导体荣获2023中国IC设计成就奖之年度新锐初创IC设计公司
|
github |
https://github.com/xutong-analog/fantasy |
eeworld |
https://home.eeworld.com.cn/space-uid-540784.html |
|
公众号 |
xuyuntong | |
21ic:
|
https://bbs.21ic.com/u/xutang |