xuyuntangs

文章数:240 被阅读:410258

账号入驻

Arduino~1~ 辅助电源1.0

最新更新时间:2023-04-15
    阅读数:

功能:

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代码:




#include #include #include "CSD203.h"#ifdef U8X8_HAVE_HW_SPI#include #endif#ifdef U8X8_HAVE_HW_I2C#include #endif
int i;// define string  bufferchar VoltageChar[40];// 电压采样Bufferchar 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>  #include "CSD203.h"  #include "Arduino.h"  #include "Wire.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>#ifndef CSD203_h#define CSD203_h#include "Arduino.h"#include #include "Wire.h"/* CSD203 Regsistor map */#define CONFIGURATION   0X00/*Read Only*/#define SHUNT_VOLTAGE   0X01#define BUS_VOLTAGE     0X02#define POWER           0X03#define CURRENT         0x04/*Read Only*/#define CALIBRATION     0x05#define MASKENABLE      0x06#define ALERTLIMIT      0x07

/*Calibration Calculation parameter*//* This Parameter Gain*10000K*/#define CalParam        51200/* ↓ add your shunt here ↓   */#define CSD_CONFIG_Rs_1mR 1#define CSD_CONFIG_Rs_2mR 2#define CSD_CONFIG_Rs_5mR 5#define CSD_CONFIG_Rs_10mR 10#define CSD_CONFIG_Rs_20mR 20#define CSD_CONFIG_Rs_50mR 50#define CSD_CONFIG_CurrentLsb1mA   10#define CSD_CONFIG_CurrentLsb2mA   20#define CSD_CONFIG_CurrentLsb5mA   50#define CSD_CONFIG_CurrentLsb10mA  100#define CSD_CONFIG_CurrentLsb20mA  200/*Calibration Calculation parameter*/



/* CSD203 Regsistor Config*/#define CSD_CONFIG_RST 1#define CSD_CONFIG_UnRST 0
#define CSD_CONFIG_Avg1 0#define CSD_CONFIG_Avg4 1#define CSD_CONFIG_Avg16 2#define CSD_CONFIG_Avg64 3#define CSD_CONFIG_Avg128 4#define CSD_CONFIG_Avg256 5#define CSD_CONFIG_Avg512 6#define CSD_CONFIG_Avg1024 7
#define CSD_CONFIG_VBUS_CT1_1mS 4#define CSD_CONFIG_VShunt_CT1_1mS 4
#define CSD_CONFIG_ShuntBus_CON 7
#define CSD_CONFIG_ADDR_A1_GND_A0_GND 64#define CSD_CONFIG_ADDR_A1_GND_VS_GND 65#define CSD_CONFIG_ADDR_A1_GND_SDA_GND 66#define CSD_CONFIG_ADDR_A1_GND_SCL_GND 67#define CSD_CONFIG_ADDR_A1_VS_A0_GND 68#define CSD_CONFIG_ADDR_A1_VS_A0_VS 69#define CSD_CONFIG_ADDR_A1_VS_A0_SDA 70#define CSD_CONFIG_ADDR_A1_VS_A0_SCL 71#define CSD_CONFIG_ADDR_A1_SDA_A0_GND 72#define CSD_CONFIG_ADDR_A1_SDA_A0_VS 73#define CSD_CONFIG_ADDR_A1_SDA_A0_SDA 74#define CSD_CONFIG_ADDR_A1_SDA_A0_SCL 75#define CSD_CONFIG_ADDR_A1_SCL_A0_GND 76#define CSD_CONFIG_ADDR_A1_SCL_A0_VS 77#define CSD_CONFIG_ADDR_A1_SCL_A0_SDA 78#define CSD_CONFIG_ADDR_A1_SCL_A0_SCL 79/******************//*CSD Alert Option*//******************//*Vshunt Voltage Over Voltage*/#define CSD_ALERT_VShunt_OVA_ON 1#define CSD_ALERT_VShunt_OVA_OFF 0/*Vshunt Voltage Under Voltage*/#define CSD_ALERT_VShunt_UVA_ON 1#define CSD_ALERT_VShunt_UVA_OFF 0/*VBus Voltage Over Voltage*/#define CSD_ALERT_VBUS_OVA_ON 1#define CSD_ALERT_VBUS_OVA_OFF 0/*VBus Voltage Under Voltage*/#define CSD_ALERT_VBUS_UVA_ON 1#define CSD_ALERT_VBUS_UVA_OFF 0/* Power Over Limit */#define CSD_ALERT_Power_Over_ON 1#define CSD_ALERT_Power_Over_OFF 0/* ADC Coversion Ready */#define CSD_ALERT_CoversionReady_ON 1#define CSD_ALERT_CoversionReady_OFF 0#define CSD_ALERT_CNVR_FLAG_ON 1#define CSD_ALERT_CNVR_FLAG_OFF 0/*  ALERT Function Flag and Alert latch Work Together */#define CSD_ALERT_ALERT_FLAG_ON 1#define CSD_ALERT_ALERT_FLAG_OFF 0#define CSD_ALERT_ALERT_Latch_ON 1#define CSD_ALERT_ALERT_Latch_OFF 0/*Power over flow */#define CSD_ALERT_OVF_ON 1#define CSD_ALERT_OVF_OFF 0/*Alert Pin */#define CSD_ALERT_APOL_ActiveLow 1#define CSD_ALERT_APOL_ActiveHigh 0
/*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);// };#endif

实物效果

没接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



 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: TI培训

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved