Re: xl4015
MCP4725 2 шт
#define KALL_I_IZ 1.020
#define KALL_U_IZ 0.970
#define KALL_I_DAC 410.00
#define KALL_U_DAC 395.00
#define ADDR1 0x60 // I
#define ADDR2 0x61 // U
#include <EEPROM.h>
#include <Wire.h>
#include <U8glib.h> // https://github.com/olikraus/u8glib/
#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip
#include <Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
Encoder myEnc(6, 5);// DT, CLK
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST);
int n,m,m1;
float i_dig,u_dig,i_dig1;
float u_iz,i_iz,i_iz1;
long i_sum,u_sum,i_sum1;
bool w=1,w1,iu,power;
long times,oldPosition = -999,newPosition;
int un,in;
void setup(){
delay(200);
Wire.begin();
analogReadResolution(12);// АЦП 12 БИТ
MsTimer2::set(1, to_Timer);MsTimer2::start();
un = EEPROM.read(0);in=EEPROM.read(1);
analogReference(DEFAULT);
pinMode(A0,INPUT); // Изм.I
pinMode(A1,INPUT); // Изм.U
pinMode(7,INPUT); // SW ENCODER
u8g.firstPage();
do {
u8g.setFont(u8g_font_profont12r);
u8g.drawStr(30,10,"POWER SUPPLY");
u8g.drawStr(30,25,"0-25V 0-2.5A");
u8g.drawStr(30,55,"rcl-radio.ru");
} while( u8g.nextPage() );
delay(2000);
I2C_write(0, true, ADDR2);
I2C_write(0, true, ADDR1);
}
void loop(){
if(digitalRead(7)==LOW &&iu==0){iu=1;times=millis();w=1;w1=1;delay(200);}
if(digitalRead(7)==LOW &&iu==1){iu=0;times=millis();w=1;w1=1;delay(200);}
if(iu==0){
if(newPosition != oldPosition){oldPosition = newPosition;if(newPosition>1){newPosition=1;}if(newPosition<-1){newPosition=-1;}
un=un+newPosition;myEnc.write(0);newPosition=0;
if(un<0){un=0;}if(un>250){un=250;}times=millis();w=1;w1=1;}
}
if(iu==1){
if(newPosition != oldPosition){oldPosition = newPosition;if(newPosition>1){newPosition=1;}if(newPosition<-1){newPosition=-1;}
in=in+newPosition;myEnc.write(0);newPosition=0;
if(in<0){in=0;}if(in>250){in=250;}times=millis();w=1;w1=1;}
}
i_dig = analogRead(A0);
i_sum = i_sum+i_dig;
n++;if(n>9){n=0;i_iz=i_sum/10;i_sum=0;}
u_dig = analogRead(A1);
u_sum = u_sum+u_dig;
m++;if(m>9){m=0;u_iz=u_sum/10;u_sum=0;w=1;}
if(w==1){
I2C_write(4095.00/KALL_U_DAC*un, false, ADDR2);
I2C_write(4095.00/KALL_I_DAC*in, false, ADDR1);
delay(10);
u8g.firstPage();
do {
u8g.drawLine(0, 16, 128, 16);//u8g.drawLine(0, 53, 128, 53);
u8g.setFont(u8g_font_profont29r);
u8g.drawStr(0,40,"U");u8g.setPrintPos(35, 40);u8g.print(u_iz*KALL_U_IZ/100.0,1);u8g.drawStr(114,40,"V");
u8g.drawStr(0,64,"I");u8g.setPrintPos(35, 64);u8g.print(i_iz*KALL_I_IZ/1000.0,2);u8g.drawStr(114,64,"A");
u8g.setFont(u8g_font_profont15r);
if(iu==0){u8g.drawStr(0,12,"U >");}else{u8g.drawStr(0,12,"U ");}u8g.setPrintPos(25, 12);u8g.print(un/10.0,1);
if(iu==1){u8g.drawStr(70,12,"I >");}else{u8g.drawStr(70,12,"I ");}u8g.setPrintPos(95, 12);u8g.print(in/100.00,2);
} while( u8g.nextPage() );
}
if(millis()-times>5000 && w1==1){EEPROM.update(0,un);EEPROM.update(1,in);w1=0;w=1;iu=0;myEnc.write(0);}
}
void to_Timer(){newPosition = myEnc.read()/4;}
void I2C_write(int data, byte eeprom, byte addr){
uint8_t cmd[3];
if(eeprom==true){cmd[0]=0x60;}else{cmd[0]=0x40;}
cmd[1] = (data / 16);
cmd[2] = (data % 16) << 4;
Wire.beginTransmission(addr);
Wire.write(cmd[0]);
Wire.write(cmd[1]);
Wire.write(cmd[2]);
Wire.endTransmission();
}