1

Тема: STA339_DIR9001

Основная статья - http://rcl-radio.ru/?p=130096

#define I2C_STA339 0x1C // Address chip | PIN_SA = GND 
#define CLK     9   //  ENCODER 
#define DT      8   //  ENCODER 
#define SW     10   //  ENCODER 
#define BRIG_H 200      // Яркость 0...255 POWER ON
#define BRIG_L 50       // Яркость 0...255 POWER OFF

#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_6 0x2FDEA15 // Кнопка MUTE
#define IR_7 0x2FD00FF // Кнопка STANDBY (POWER)

#include <Wire.h>
#include <EEPROM.h>
#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
#include <LiquidCrystal_I2C.h>           // http://forum.rcl-radio.ru/misc.php?action=pan_download&item=45&download=1 
#include <boarddefs.h>                   // входит в состав библиотеки IRremote
#include <IRremote.h>                    // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip 
 Encoder myEnc(CLK, DT);
 LiquidCrystal_I2C lcd(0x27,16,2);  // Устанавливаем дисплей 
 IRrecv irrecv(12); // указываем вывод модуля IR приемника
 decode_results ir; 

byte v1[8] = {0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07};
byte v2[8] = {0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00};      
byte v3[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v4[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v5[8] = {0x1C,0x1C,0x00,0x00,0x00,0x00,0x1C,0x1C};
byte v6[8] = {0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C};
byte v7[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07};
byte v8[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00};

int menu,vol,a[3],power,bass,bass_print,x,treb,treb_print,ball,chl,chr,bass_q;
unsigned long times,times1,oldPosition  = -999,newPosition,times2;
byte d1,d2,d3,d4,d5,d6,e1,e2,e3,gr1,gr2,www,w,w2,mute;

void setup() {
  Wire.begin();Serial.begin(9600);irrecv.enableIRIn();
  MsTimer2::set(1, to_Timer);MsTimer2::start();
  if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении 
  pinMode(2,INPUT_PULLUP); // POWER
  pinMode(3,INPUT_PULLUP); // MUTE
  pinMode(4,OUTPUT); // RESET PIN  STA339
  pinMode(5,OUTPUT); // PWRDN PIN  STA339
  digitalWrite(4,HIGH);delay(10);  // RESET 
  digitalWrite(5,HIGH);delay(10);  // PWRDN
  pinMode(6,OUTPUT);  // ВЫХОД УПРАВЛЕНИЯ ПОДСВЕТКОЙ
  analogWrite(6, BRIG_H);// больше 200 не делать
  vol = EEPROM.read(0);bass = EEPROM.read(1);treb = EEPROM.read(2);bass_q = EEPROM.read(3);ball = EEPROM.read(4)-100;
  init_sta();
  Serial.println(wireRead(I2C_STA339,0x72D),BIN); 
  lcd.init();lcd.backlight();
  lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);
  lcd.createChar(4, v4);lcd.createChar(5, v5);lcd.createChar(6, v6);
  lcd.createChar(7, v7);lcd.createChar(8, v8);
  lcd.setCursor(0,0);lcd.print("DIR9001  STA339B");
  lcd.setCursor(0,1);lcd.print(" 2.0-CH 40-watt ");
  delay(2000);lcd.clear(); 
  

}

void loop() {
  if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();times=millis();w=1;w2=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
  if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта  
   
  if(power==0){
/// MENU //////////////////////////////
  if(digitalRead(SW)==LOW){menu++;if(menu>4){menu=0;};delay(200);lcd.clear();w=1;w2=1;times=millis();}
  if(ir.value==IR_1){menu++;gr1=0;gr2=0;cl1();lcd.clear();times=millis();w=1;w2=1;if(menu>4){menu=0;};delay(200);}
  if(ir.value==IR_2){menu--;gr1=0;gr2=0;cl1();lcd.clear();times=millis();w=1;w2=1;if(menu<0){menu=4;};delay(200);}
/// MUTE //////////////////////////////
 if((digitalRead(3)==LOW||ir.value==IR_6)&&mute==0){mute=1;gr1=0;gr2=0;menu=100;cl();wireWrite(I2C_STA339,0x06,0b00010110);lcd.setCursor(6,0);lcd.print("MUTE");}
 if((digitalRead(3)==LOW||ir.value==IR_6)&&mute==1){mute=0;gr1=0;gr2=0;menu=0;cl();wireWrite(I2C_STA339,0x06,0b00010000);w2=1;} 
  }
/// POWER ///////////////////////////// 
  if((ir.value==IR_7||digitalRead(2)==LOW)&&power==0){power=1;cl();lcd.setCursor(3,0);lcd.print("POWER  OFF");menu=100;
    wireWrite(I2C_STA339,0x05,0b10011100);delay(3000);analogWrite(6,BRIG_L);}// power off
  if((ir.value==IR_7||digitalRead(2)==LOW)&&power==1){power=0;analogWrite(6, BRIG_H);cl();lcd.setCursor(3,0);lcd.print("POWER   ON ");
    w2=1;w=1;menu=0;myEnc.write(0);wireWrite(I2C_STA339,0x05,0b11011100);delay(3000);cl();}// power on  
 
  
 /// VOLUME ///////////////////////////////////////////////////
 if(menu==0){
   if(ir.value==IR_4){vol++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка >>>>>>
   if(ir.value==IR_3){vol--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка <<<<<<  
 
   if (newPosition != oldPosition){oldPosition = newPosition;
     vol=vol-newPosition;myEnc.write(0);newPosition=0;w=1;times=millis();w2=1;
     vol_conf();
     www=1;
     }
   if(www==1){www=0;
   wireWrite(I2C_STA339,0x07,0);// 0 dB
   wireWrite(I2C_STA339,0x08,vol+ball);
   wireWrite(I2C_STA339,0x09,vol-ball);
   }  
      lcd.setCursor(0,0);lcd.print("VOLUME ");
      if(millis()-times2>1000){times2=millis();
      if(wireRead(I2C_STA339,0x72D)<0b1111111){lcd.setCursor(0,1);lcd.print("ERROR");}
      else{lcd.setCursor(0,1);lcd.print("96 kHz");}
      Serial.println(wireRead(I2C_STA339,0x72D),BIN);}
      
 
  if(w2==1){w2=0;
     a[0]=(246-vol)/100;a[1]=(246-vol)/10%10;a[2]=(246-vol)%10;
       for(int i=0;i<3;i++){
      switch(i){
        case 0: e1=7,e2=8,e3=9;break;
        case 1: e1=10,e2=11,e3=12;break;
        case 2: e1=13,e2=14,e3=15;break;
        }
      switch(a[i]){
        case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
        case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
        case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
        case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
        case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
        case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
        case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
        case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
        case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
        case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
    }
      lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
      lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
 }} 
 }

////////////// TERB ///////////////////////////////////////////////////////////////////
 if(menu==1){
   if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка >>>>>>
   if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка <<<<<<
 
   if (newPosition != oldPosition){oldPosition = newPosition;treb=treb+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;treb_func();www=1;}

   if(www==1){www=0;wireWrite(I2C_STA339,0x11,0|bass|(treb<<4));}

   int treb_bd = treb-7;
   if(treb_bd<0){treb_print = abs(treb_bd)*2;lcd.setCursor(7,0);lcd.write((uint8_t)3);}else{treb_print = treb_bd*2;lcd.setCursor(7,0);lcd.print(" ");}
   a[0]= treb_print/10;a[1]=treb_print%10;
   for(x=0;x<2;x++){switch(x){case 0: e1=8,e2=9,e3=10;break;case 1: e1=11,e2=12,e3=13;break;}digit();}
   lcd.setCursor(0,0);lcd.print("TREBLE");lcd.setCursor(0,1);lcd.print("CONTROL");lcd.setCursor(14,0);lcd.print("dB");
   } 

////////////// BASS ///////////////////////////////////////////////////////////////////
 if(menu==2){
   if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка >>>>>>
   if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка <<<<<<
 
   if (newPosition != oldPosition){oldPosition = newPosition;bass=bass+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;bass_func();www=1;}

   if(www==1){www=0;wireWrite(I2C_STA339,0x11,0|bass|(treb<<4));}

   int bass_bd = bass-7;
   if(bass_bd<0){bass_print = abs(bass_bd)*2;lcd.setCursor(7,0);lcd.write((uint8_t)3);}else{bass_print = bass_bd*2;lcd.setCursor(7,0);lcd.print(" ");}
   a[0]= bass_print/10;a[1]=bass_print%10;
   for(x=0;x<2;x++){switch(x){case 0: e1=8,e2=9,e3=10;break;case 1: e1=11,e2=12,e3=13;break;}digit();}
   lcd.setCursor(0,0);lcd.print("BASS");lcd.setCursor(0,1);lcd.print("CONTROL");lcd.setCursor(14,0);lcd.print("dB");
   }     

//////// BALANCE /////////////////////////////////////////////////////////////// 
 if(menu==4){ 
   if(ir.value==IR_3){ball++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){ball++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка >>>>>>
   if(ir.value==IR_4){ball--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){ball--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка <<<<<<  
 
   if (newPosition != oldPosition){oldPosition = newPosition;
   ball=ball-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ball_fun();www=1;
   }
   if(www==1){www=0;
   wireWrite(I2C_STA339,0x07,0);// 0 dB
   wireWrite(I2C_STA339,0x08,vol+ball);
   wireWrite(I2C_STA339,0x09,vol-ball);
   }
   lcd.setCursor(4,0);lcd.print(F("   <>   "));lcd.setCursor(4,1);lcd.print(F("CHL  CHR"));
   chl=(4+ball)-4;chr=(4-ball)-4;
   if(chl<0){lcd.setCursor(12,0);chl=abs(chl);lcd.write((uint8_t)3);}else{lcd.setCursor(12,0);lcd.print(" ");}
   if(chr<0){lcd.setCursor(0,0);chr=abs(chr);lcd.write((uint8_t)3);}else{lcd.setCursor(0,0);lcd.print(" ");}
   if(w2==1){w2=0;a[0]=chl;a[1]=chr;
   for(int i1=0;i1<2;i1++){
      switch(i1){
        case 0: e1=1,e2=2,e3=3;break;
        case 1: e1=13,e2=14,e3=15;break;
        }
      switch(a[i1]){
        case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
        case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
        case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
        case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
        case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
        case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
        case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
        case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
        case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
        case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
    }
      lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
      lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
 }}}  

if(menu==3){
   if(ir.value==IR_3){bass_q++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){bass_q++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка >>>>>>
   if(ir.value==IR_4){bass_q--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){bass_q--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка <<<<<<  
   
  if (newPosition != oldPosition){oldPosition = newPosition;
   bass_q=bass_q+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;bass_q_func();www=1;
   }
   if(www==1){www=0;wireWrite(I2C_STA339,0x0C,0b00001000|bass_q<<4);
   }
   lcd.setCursor(0,0);lcd.print("BASS FREQUENCY");
   lcd.setCursor(0,1);
   switch(bass_q){
    case 1:lcd.print("80  Hz");break;
    case 2:lcd.print("100 Hz");break;
    case 3:lcd.print("120 Hz");break;
    case 4:lcd.print("140 Hz");break;
    case 5:lcd.print("160 Hz");break;
    case 6:lcd.print("180 Hz");break;
    case 7:lcd.print("200 Hz");break;
    case 8:lcd.print("220 Hz");break;
    case 9:lcd.print("240 Hz");break;
    case 10:lcd.print("260 Hz");break;
    case 11:lcd.print("280 Hz");break;
    case 12:lcd.print("300 Hz");break;
    case 13:lcd.print("320 Hz");break;
    case 14:lcd.print("340 Hz");break;
    case 15:lcd.print("360 Hz");break;
    }
  } 


//// EEPROM ///////////////////////////////////////
 if(millis()-times>10000 && w==1 && mute==0 && power==0){w=0;EEPROM.update(0,vol);
 EEPROM.update(1,bass);EEPROM.update(2,treb);EEPROM.update(3,bass_q);
 EEPROM.update(4,ball+100);menu=0;cl();w2=1;
}
}

void bass_q_func(){if(bass_q>15){bass_q=15;}if(bass_q<1){bass_q=1;}}
void ball_fun(){if(ball>8){ball=8;}if(ball<-8){ball=-8;}}
void treb_func(){if(treb<1){treb=1;}if(treb>13){treb=13;}}
void bass_func(){if(bass<1){bass=1;}if(bass>13){bass=13;}}
void vol_conf(){if(vol>246){vol=246;}if(vol<8){vol=8;}}
void cl(){ir.value=0;delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(200);} 
void digit(){switch(a[x]){
case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;}
lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);}

void init_sta(){ 
  wireWrite(I2C_STA339,0x00,0b01101011);
  //[2:0][MCS2 MCS1 MCS0]=011(64*FS)
  //[4:3][IR1 IR0]=10(192K)
  //[5][TWRB]=1(TEMP_ERR АВТОВКЛЮЧ.ПОСЛЕ ПЕРЕГРЕВА-АКТИВНА)
  //[6][TWAB]=0(ОГРАНИЧ ВЫХ МОЩНОСТИ ПРИ ПЕРЕГРЕВЕ-АКТИВНА)0X37 - УКАЗАТЬ УРОВ. ОГР. МОЩН.
  //[7][FDRB]=0(ЗАЩИТА.ОТКЛ.ПИТАНИЯ ПРИ НЕИСПРАВНОСТИ-АКТИВНА)
  wireWrite(I2C_STA339,0x01,0b10100000); 
  //[3:0][SAI]=0000(I2S 64*fs 16 to 24 bit) 
  //[4][SAIFB]=0(MSB) MSB/LSB-first
  //[5][DSCKE]=1(АКТИВНА ТАКТОВАЯ ЗАДЕРЖКА)
  //[7:6][C1IM C2IM]=10(СОПАСТОНОВЛЕНИЕ КАНАЛОВ)DEF
  wireWrite(I2C_STA339,0x02,0b10011110);
  //[1:0][OM0 OM1]=10( FFX power output mode)DEF
  //[5:2][CSZ]=0111(FFX compensating pulse size register)DEF
  //[6][RESERVED]
  //[7][OCRB]=1(ЗАЩИТА ПО ТОКУ-АКТИВНА)
  wireWrite(I2C_STA339,0x03,0b01000000);
  //[0][HPB]=0(ФИЛЬТР HPB АКТИВЕН)DEF ОПАСНО ВЫКЛЮЧАТЬ!!! 
  //[1][DEMP]=0(De-emphasis)OFF
  //[2][DSPB]=0(DSP bypass)АКТИВНА-РЕГ.ТЕМБРА АКТИВНЫ
  //[3][PSL]=0(Postscale link)DEF
  //[4][BQL]=0(Biquad coefficient link)DEF
  //[5][DCR]=0(Dynamic range compression)DEF
  //[6][ZDE]=1(Zero-detect mute enable)DEF
  //[7][SME]=0(Submix mode enable)DEF  
  wireWrite(I2C_STA339,0x04,0b11000010);//def
  //[0][MPCV]=0(Max power correction variable)DEF
  //[1][MPC]=1(Max power correction)DEF
  //[2][NSBW]=0(Noise-shaper bandwidth selection)DEF
  //[3][AME]=0(AM mode enable)DEF
  //[4][PWMS]=1(PWM speed mode)DEF
  //[5][DCCV]=0(Distortion compensation variable enable)DEF
  //[6][ZCE]=1(Zero-crossing volume enable)DEF
  //[7][SVE]=1(Soft volume update enable)DEF
  wireWrite(I2C_STA339,0x05,0b11011100);
  //[1:0][OCFG]=00(1A/1B > 1A/1B | 2A/2B > 2A/2B)
  //[2][IDE]=1(Invalid input detect mute enable)DEF
  //[3][BCLE]=1(Binary output mode clock loss detection)DEF
  //[4][LDTE]=1(LRCK double trigger protection)DEF
  //[5][ECLE]=0(Auto EAPD on clock loss)DEF
  //[6][PWDN]=1(IC power down)DEF
  //[7][EAPD]=1(ВНЕШНЕЕ ПИТАНИЕ УСИЛИТЕЛЯ)АКТИВНО
  wireWrite(I2C_STA339,0x06,0b00010000);// DEF
  //Mute/line output configuration register
  wireWrite(I2C_STA339,0x07,0);// 0 dB
  //Master volume register BYTE 0...0xFE = 0 dB...-127,5 | 0xFF = MUTE
   wireWrite(I2C_STA339,0x08,vol+ball);// 0 dB
  //Channel 1 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE
  wireWrite(I2C_STA339,0x09,vol-ball);
  //Channel 2 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE 
  wireWrite(I2C_STA339,0x0A,255);
  //Channel 3 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE   
  wireWrite(I2C_STA339,0x0B,0b10000000);//DEF
  //[4:5][AMGC](Audio preset register 1)
  //Audio preset gain compression/limiters selection
  wireWrite(I2C_STA339,0x0C,0b00001000|bass_q<<4);
  //[0][amame]
  //[3:1][AMAM]=000(AM interference frequency switching)DEF
  //[7:4][XO]=0000(Кроссовер для управления басами)byte 0000...0001...1111 = DEF...80Hz...360Hz
  wireWrite(I2C_STA339,0x0E,0b00000000);
  //[0][TCB]=0(Tone control bypass)DEF
  //[1][EQBP]=0(EQ bypass)DEF
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x0F,0b01000000);
  //[0][TCB]=0(Tone control bypass)DEF
  //[1][EQBP]=0(EQ bypass)DEF
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x10,0b10000000);
  //[1:0]RESERVED
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x11,0|bass|treb<<4);// тембр ВЧ НЧ 
  wireWrite(I2C_STA339,0x12,0b01101010);
  //(Limiter 1 attack/release rate)DEF
  wireWrite(I2C_STA339,0x13,0b01101001);
  //(Limiter 1 attack/release threshold )DEF
  wireWrite(I2C_STA339,0x14,0b01101010);
  //(Limiter 2 attack/release rate)DEF
  wireWrite(I2C_STA339,0x15,0b01101001);
  //(Limiter 2 attack/release threshold)DEF
  wireWrite(I2C_STA339,0x33,0b00110000);
  //(Limiter 1 extended release threshold)DEF
  wireWrite(I2C_STA339,0x34,0b00110000);
  //(Limiter 2 extended attack threshold)DEF
  wireWrite(I2C_STA339,0x35,0b00110000);
  //(Limiter 2 extended release threshold)DEF
  wireWrite(I2C_STA339,0x16,255);
  //(Coefficient address register)DEF
  wireWrite(I2C_STA339,0x17,255);
  wireWrite(I2C_STA339,0x18,255);
  wireWrite(I2C_STA339,0x19,255);
  //(Coefficient b1 data register bits)DEF
  wireWrite(I2C_STA339,0x1A,255);
  wireWrite(I2C_STA339,0x1B,255);
  wireWrite(I2C_STA339,0x1C,255);
  //(Coefficient b2 data register bits)DEF
  wireWrite(I2C_STA339,0x1D,255);
  wireWrite(I2C_STA339,0x1E,255);
  wireWrite(I2C_STA339,0x1F,255);
  //(Coefficient a1 data register bits)DEF
  wireWrite(I2C_STA339,0x20,255);
  wireWrite(I2C_STA339,0x21,255);
  wireWrite(I2C_STA339,0x22,255);
  //(Coefficient a2 data register bits)DEF
  wireWrite(I2C_STA339,0x23,255);
  wireWrite(I2C_STA339,0x24,255);
  wireWrite(I2C_STA339,0x25,255);
  //(Coefficient b0 data register bits)DEF 
  wireWrite(I2C_STA339,0x26,255);
  //(Coefficient read/write control register)DEF 
  wireWrite(I2C_STA339,0x27,0b00011010);
  wireWrite(I2C_STA339,0x28,0b11000000);
  //(Variable max power correction registers)DEF 
  wireWrite(I2C_STA339,0x29,0b11110011);
  wireWrite(I2C_STA339,0x2A,0b00110011);
  //(Distortion compensation registers)DEF
  wireWrite(I2C_STA339,0x2B,0b00000000);
  wireWrite(I2C_STA339,0x2C,0b00001100);
  //(Distortion compensation registers)DEF 
  wireWrite(I2C_STA339,0x31,0b00000011);
  //[1:0][SEL]=00( EQ RAM select)DEF
  //[2]RESERVED
  //[4:3][AMGC]=00(Anticlipping and DRC preset)DEF
  //[6:5]RESERVED
  //[7][BOX]=0(This bit can be used to bypass the crossover filters)DEF
  wireWrite(I2C_STA339,0x36,0b00000000);
  //(Extended configuration register)DEF
  wireWrite(I2C_STA339,0x37,0b00000000);
  wireWrite(I2C_STA339,0x38,0b00000000);
  //(Soft volume configuration registers)DEF
  wireWrite(I2C_STA339,0x39,0b00000001);
  wireWrite(I2C_STA339,0x3A,0b11101110);
  wireWrite(I2C_STA339,0x3B,0b11111111);
  wireWrite(I2C_STA339,0x3C,0b01111110);
  wireWrite(I2C_STA339,0x3D,0b11000000);
  wireWrite(I2C_STA339,0x37,0b00100110);
  //(DRC RMS filter coefficients)DEF
}


byte wireRead(byte addr, byte reg){ // I2C
  Wire.beginTransmission(addr);
  Wire.write (reg);
  Wire.endTransmission();
  Wire.requestFrom(addr,1);
  while(Wire.available()<1);
  byte value = Wire.read();
  return value;
  }  
 
void wireWrite(byte addr, byte reg, byte data){
  Wire.beginTransmission(addr);
  Wire.write (reg);
  Wire.write (data);
  Wire.endTransmission();
  } 

void to_Timer(){newPosition = myEnc.read()/4;}  

2

Re: STA339_DIR9001

Коды ошибок

http://forum.rcl-radio.ru/uploads/images/2023/02/366f2e3c367974a58f2976d04f427e00.png

В мониторе порта отображаются коды ошибок

1111111 все ок

3

Re: STA339_DIR9001

Теплоотвод

При использовании двухслойной печатной платы тепловое сопротивление при соединении с окружающей средой с 2
участками медного заземления размером (3 x 3 см) x 2 (сверху и снизу платы площадка заземление-теплоотвод по 9 кв. см)  и 16 сквозными отверстиями составляет 24 ° C/Вт при естественной конвекции воздуха.
Рассеиваемая мощность внутри устройства зависит в первую очередь от напряжения питания, импеданса нагрузки и уровня выходной модуляции.

Пример:

http://forum.rcl-radio.ru/uploads/images/2023/02/4e6d75644aeaf0208121bfb19327132c.png

4

Re: STA339_DIR9001

Добавлены часы DS3231

#define I2C_STA339 0x1C // Address chip | PIN_SA = GND 
#define CLK     9   //  ENCODER 
#define DT      8   //  ENCODER 
#define SW     10   //  ENCODER 
#define BRIG_H 200      // Яркость 0...255 POWER ON
#define BRIG_L 50       // Яркость 0...255 POWER OFF

#define IR_1 0x2FDD02F // Кнопка вверх
#define IR_2 0x2FD32CD // Кнопка вниз
#define IR_3 0x2FD906F // Кнопка >
#define IR_4 0x2FDF20D // Кнопка <
#define IR_6 0x2FDEA15 // Кнопка MUTE
#define IR_7 0x2FD00FF // Кнопка STANDBY (POWER)

#include <Wire.h>
#include <EEPROM.h>
#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
#include <LiquidCrystal_I2C.h>           // http://forum.rcl-radio.ru/misc.php?action=pan_download&item=45&download=1 
#include <boarddefs.h>                   // входит в состав библиотеки IRremote
#include <IRremote.h>                    // http://rcl-radio.ru/wp-content/uploads/2019/06/IRremote.zip 
#include <DS3231.h>                      // http://rcl-radio.ru/wp-content/uploads/2022/07/DS3231.zip 
 Encoder myEnc(CLK, DT);
 LiquidCrystal_I2C lcd(0x27,16,2);  // Устанавливаем дисплей 
 IRrecv irrecv(12); // указываем вывод модуля IR приемника
 decode_results ir; 
 DS3231 clock;RTCDateTime DateTime;

byte v1[8] = {0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07};
byte v2[8] = {0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00};      
byte v3[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v4[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x1F,0x1F};
byte v5[8] = {0x1C,0x1C,0x00,0x00,0x00,0x00,0x1C,0x1C};
byte v6[8] = {0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C};
byte v7[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x07};
byte v8[8] = {0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00};

int menu,vol,a[6],power,bass,bass_print,x,treb,treb_print,ball,chl,chr,bass_q;
unsigned long times,times1,oldPosition  = -999,newPosition,times2;
byte d1,d2,d3,d4,d5,d6,e1,e2,e3,gr1,gr2,www,w,w2,mute;
int hour,minut,secon,time_reg=0;

void setup() {
  Wire.begin();Serial.begin(9600);irrecv.enableIRIn();clock.begin();
  MsTimer2::set(1, to_Timer);MsTimer2::start();
  if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// очистка памяти при первом включении 
  pinMode(2,INPUT_PULLUP); // POWER
  pinMode(3,INPUT_PULLUP); // MUTE
  pinMode(4,OUTPUT); // RESET PIN  STA339
  pinMode(5,OUTPUT); // PWRDN PIN  STA339
  digitalWrite(4,HIGH);delay(10);  // RESET 
  digitalWrite(5,HIGH);delay(10);  // PWRDN
  pinMode(6,OUTPUT);  // ВЫХОД УПРАВЛЕНИЯ ПОДСВЕТКОЙ
  // clock.setDateTime(__DATE__, __TIME__); 
  analogWrite(6, BRIG_H);// больше 200 не делать
  vol = EEPROM.read(0);bass = EEPROM.read(1);treb = EEPROM.read(2);bass_q = EEPROM.read(3);ball = EEPROM.read(4)-100;
  init_sta();
  Serial.println(wireRead(I2C_STA339,0x72D),BIN); 
  lcd.init();lcd.backlight();
  lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);
  lcd.createChar(4, v4);lcd.createChar(5, v5);lcd.createChar(6, v6);
  lcd.createChar(7, v7);lcd.createChar(8, v8);
  lcd.setCursor(0,0);lcd.print("DIR9001  STA339B");
  lcd.setCursor(0,1);lcd.print(" 2.0-CH 40-watt ");
  delay(2000);lcd.clear(); 
  

}

void loop() {
  if ( irrecv.decode( &ir )) {Serial.print("0x");Serial.println( ir.value,HEX);irrecv.resume();times=millis();w=1;w2=1;}// IR приемник - чтение, в мониторе порта отображаются коды кнопок
  if(ir.value==0){gr1=0;gr2=0;}// запрет нажатий не активных кнопок пульта  
   
  if(power==0){time_reg=0;
/// MENU //////////////////////////////
  if(digitalRead(SW)==LOW){menu++;if(menu>4){menu=0;};delay(200);lcd.clear();w=1;w2=1;times=millis();}
  if(ir.value==IR_1){menu++;gr1=0;gr2=0;cl1();lcd.clear();times=millis();w=1;w2=1;if(menu>4){menu=0;};delay(200);}
  if(ir.value==IR_2){menu--;gr1=0;gr2=0;cl1();lcd.clear();times=millis();w=1;w2=1;if(menu<0){menu=4;};delay(200);}
/// MUTE //////////////////////////////
 if((digitalRead(3)==LOW||ir.value==IR_6)&&mute==0){mute=1;gr1=0;gr2=0;menu=100;cl();wireWrite(I2C_STA339,0x06,0b00010110);lcd.setCursor(6,0);lcd.print("MUTE");}
 if((digitalRead(3)==LOW||ir.value==IR_6)&&mute==1){mute=0;gr1=0;gr2=0;menu=0;cl();wireWrite(I2C_STA339,0x06,0b00010000);w2=1;} 
  }
/// POWER ///////////////////////////// 
  if((ir.value==IR_7||digitalRead(2)==LOW && digitalRead(SW)==HIGH)&&power==0){power=1;cl();lcd.setCursor(3,0);lcd.print("POWER  OFF");menu=100;
    wireWrite(I2C_STA339,0x05,0b10011100);delay(3000);analogWrite(6,BRIG_L);}// power off
  if((ir.value==IR_7||digitalRead(2)==LOW && digitalRead(SW)==HIGH)&&power==1){power=0;analogWrite(6, BRIG_H);cl();lcd.setCursor(3,0);lcd.print("POWER   ON ");
    w2=1;w=1;menu=0;myEnc.write(0);wireWrite(I2C_STA339,0x05,0b11011100);delay(3000);cl();}// power on  
 
  
 /// VOLUME ///////////////////////////////////////////////////
 if(menu==0){
   if(ir.value==IR_4){vol++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){vol++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка >>>>>>
   if(ir.value==IR_3){vol--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){vol--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;vol_conf();}// кнопка <<<<<<  
 
   if (newPosition != oldPosition){oldPosition = newPosition;
     vol=vol-newPosition;myEnc.write(0);newPosition=0;w=1;times=millis();w2=1;
     vol_conf();
     www=1;
     }
   if(www==1){www=0;
   wireWrite(I2C_STA339,0x07,0);// 0 dB
   wireWrite(I2C_STA339,0x08,vol+ball);
   wireWrite(I2C_STA339,0x09,vol-ball);
   }  
      lcd.setCursor(0,0);lcd.print("VOLUME ");
      if(millis()-times2>1000){times2=millis();
      if(wireRead(I2C_STA339,0x72D)<0b1111111){lcd.setCursor(0,1);lcd.print("ERROR");}
      else{lcd.setCursor(0,1);lcd.print("96 kHz");}
      Serial.println(wireRead(I2C_STA339,0x72D),BIN);}
      
 
  if(w2==1){w2=0;
     a[0]=(246-vol)/100;a[1]=(246-vol)/10%10;a[2]=(246-vol)%10;
       for(int i=0;i<3;i++){
      switch(i){
        case 0: e1=7,e2=8,e3=9;break;
        case 1: e1=10,e2=11,e3=12;break;
        case 2: e1=13,e2=14,e3=15;break;
        }
      switch(a[i]){
        case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
        case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
        case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
        case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
        case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
        case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
        case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
        case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
        case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
        case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
    }
      lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
      lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
 }} 
 }

////////////// TERB ///////////////////////////////////////////////////////////////////
 if(menu==1){
   if(ir.value==IR_3){treb++;gr1=1;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){treb++;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка >>>>>>
   if(ir.value==IR_4){treb--;gr1=0;gr2=1;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){treb--;gr1=0;cl1();delay(200);times=millis();w=1;w2=1;treb_func();www=1;}// кнопка <<<<<<
 
   if (newPosition != oldPosition){oldPosition = newPosition;treb=treb+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;treb_func();www=1;}

   if(www==1){www=0;wireWrite(I2C_STA339,0x11,0|bass|(treb<<4));}

   int treb_bd = treb-7;
   if(treb_bd<0){treb_print = abs(treb_bd)*2;lcd.setCursor(7,0);lcd.write((uint8_t)3);}else{treb_print = treb_bd*2;lcd.setCursor(7,0);lcd.print(" ");}
   a[0]= treb_print/10;a[1]=treb_print%10;
   for(x=0;x<2;x++){switch(x){case 0: e1=8,e2=9,e3=10;break;case 1: e1=11,e2=12,e3=13;break;}digit();}
   lcd.setCursor(0,0);lcd.print("TREBLE");lcd.setCursor(0,1);lcd.print("CONTROL");lcd.setCursor(14,0);lcd.print("dB");
   } 

////////////// BASS ///////////////////////////////////////////////////////////////////
 if(menu==2){
   if(ir.value==IR_3){bass++;gr1=1;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){bass++;gr2=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка >>>>>>
   if(ir.value==IR_4){bass--;gr1=0;gr2=1;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){bass--;gr1=0;cl1();delay(200);times=millis();w=1;w2=1;bass_func();www=1;}// кнопка <<<<<<
 
   if (newPosition != oldPosition){oldPosition = newPosition;bass=bass+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;bass_func();www=1;}

   if(www==1){www=0;wireWrite(I2C_STA339,0x11,0|bass|(treb<<4));}

   int bass_bd = bass-7;
   if(bass_bd<0){bass_print = abs(bass_bd)*2;lcd.setCursor(7,0);lcd.write((uint8_t)3);}else{bass_print = bass_bd*2;lcd.setCursor(7,0);lcd.print(" ");}
   a[0]= bass_print/10;a[1]=bass_print%10;
   for(x=0;x<2;x++){switch(x){case 0: e1=8,e2=9,e3=10;break;case 1: e1=11,e2=12,e3=13;break;}digit();}
   lcd.setCursor(0,0);lcd.print("BASS");lcd.setCursor(0,1);lcd.print("CONTROL");lcd.setCursor(14,0);lcd.print("dB");
   }     

//////// BALANCE /////////////////////////////////////////////////////////////// 
 if(menu==4){ 
   if(ir.value==IR_3){ball++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){ball++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка >>>>>>
   if(ir.value==IR_4){ball--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){ball--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;ball_fun();}// кнопка <<<<<<  
 
   if (newPosition != oldPosition){oldPosition = newPosition;
   ball=ball-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;ball_fun();www=1;
   }
   if(www==1){www=0;
   wireWrite(I2C_STA339,0x07,0);// 0 dB
   wireWrite(I2C_STA339,0x08,vol+ball);
   wireWrite(I2C_STA339,0x09,vol-ball);
   }
   lcd.setCursor(4,0);lcd.print(F("   <>   "));lcd.setCursor(4,1);lcd.print(F("CHL  CHR"));
   chl=(4+ball)-4;chr=(4-ball)-4;
   if(chl<0){lcd.setCursor(12,0);chl=abs(chl);lcd.write((uint8_t)3);}else{lcd.setCursor(12,0);lcd.print(" ");}
   if(chr<0){lcd.setCursor(0,0);chr=abs(chr);lcd.write((uint8_t)3);}else{lcd.setCursor(0,0);lcd.print(" ");}
   if(w2==1){w2=0;a[0]=chl;a[1]=chr;
   for(int i1=0;i1<2;i1++){
      switch(i1){
        case 0: e1=1,e2=2,e3=3;break;
        case 1: e1=13,e2=14,e3=15;break;
        }
      switch(a[i1]){
        case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
        case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
        case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
        case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
        case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
        case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
        case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
        case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
        case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
        case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
    }
      lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
      lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
 }}}  

if(menu==3){
   if(ir.value==IR_3){bass_q++;gr1=1;gr2=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка > 
   if(ir.value==0xFFFFFFFF and gr1==1){bass_q++;gr2=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка >>>>>>
   if(ir.value==IR_4){bass_q--;gr1=0;gr2=1;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка <
   if(ir.value==0xFFFFFFFF and gr2==1){bass_q--;gr1=0;cl1();times=millis();www=1;w=1;w2=1;bass_q_func();}// кнопка <<<<<<  
   
  if (newPosition != oldPosition){oldPosition = newPosition;
   bass_q=bass_q+newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w2=1;bass_q_func();www=1;
   }
   if(www==1){www=0;wireWrite(I2C_STA339,0x0C,0b00001000|bass_q<<4);
   }
   lcd.setCursor(0,0);lcd.print("BASS FREQUENCY");
   lcd.setCursor(0,1);
   switch(bass_q){
    case 1:lcd.print("80  Hz");break;
    case 2:lcd.print("100 Hz");break;
    case 3:lcd.print("120 Hz");break;
    case 4:lcd.print("140 Hz");break;
    case 5:lcd.print("160 Hz");break;
    case 6:lcd.print("180 Hz");break;
    case 7:lcd.print("200 Hz");break;
    case 8:lcd.print("220 Hz");break;
    case 9:lcd.print("240 Hz");break;
    case 10:lcd.print("260 Hz");break;
    case 11:lcd.print("280 Hz");break;
    case 12:lcd.print("300 Hz");break;
    case 13:lcd.print("320 Hz");break;
    case 14:lcd.print("340 Hz");break;
    case 15:lcd.print("360 Hz");break;
    }
  } 


/////////////////// ЧАСЫ ///////////////////////////////////////////////////////////////////
if(power==1){
  DateTime = clock.getDateTime();hour = DateTime.hour;minut = DateTime.minute;secon = DateTime.second;  
     a[0]=DateTime.hour/10;
     a[1]=DateTime.hour%10;
     a[2]=DateTime.minute/10;
     a[3]=DateTime.minute%10;
     a[4]=DateTime.second/10;
     a[5]=DateTime.second%10;
   for(x=0;x<4;x++){
    switch(x){
        case 0: e1=0,e2=1,e3=2;break;
        case 1: e1=3,e2=4,e3=5;break;
        case 2: e1=7,e2=8,e3=9;break;
        case 3: e1=10,e2=11,e3=12;break;
   }digit();}
 
   lcd.setCursor(6,0);lcd.print(".");lcd.setCursor(6,1);lcd.print(".");
   lcd.setCursor(14,1);lcd.print(a[4]);lcd.setCursor(15,1);lcd.print(a[5]);   
//// set time ////
   if(digitalRead(SW)==LOW&&digitalRead(3)==LOW){time_reg++;delay(200);if(time_reg>3){time_reg=0;}}
   switch(time_reg){
    case 0:lcd.setCursor(14,0);lcd.print("  ");break;
    case 1:lcd.setCursor(14,0);lcd.print("HH");break;
    case 2:lcd.setCursor(14,0);lcd.print("MM");break;
    case 3:lcd.setCursor(14,0);lcd.print("SS");break;
    }
   
   if(digitalRead(SW)==LOW&&digitalRead(2)==LOW&&time_reg==1){hour++;if(hour>23){hour=0;} clock.setDateTime(2023, 1, 14, hour, minut, secon);delay(200);}// HH++
   if(digitalRead(SW)==LOW&&digitalRead(2)==LOW&&time_reg==2){minut++;if(minut>59){minut=0;} clock.setDateTime(2023, 1, 14, hour, minut, secon);delay(200);}// MM++
   if(digitalRead(SW)==LOW&&digitalRead(2)==LOW&&time_reg==3){clock.setDateTime(2023, 1, 14, hour, minut, 0);delay(200);}//  SS clear
}     


//// EEPROM ///////////////////////////////////////
 if(millis()-times>10000 && w==1 && mute==0 && power==0){w=0;EEPROM.update(0,vol);
 EEPROM.update(1,bass);EEPROM.update(2,treb);EEPROM.update(3,bass_q);
 EEPROM.update(4,ball+100);menu=0;cl();w2=1;
}
}

void bass_q_func(){if(bass_q>15){bass_q=15;}if(bass_q<1){bass_q=1;}}
void ball_fun(){if(ball>8){ball=8;}if(ball<-8){ball=-8;}}
void treb_func(){if(treb<1){treb=1;}if(treb>13){treb=13;}}
void bass_func(){if(bass<1){bass=1;}if(bass>13){bass=13;}}
void vol_conf(){if(vol>246){vol=246;}if(vol<8){vol=8;}}
void cl(){ir.value=0;delay(300);lcd.clear();}
void cl1(){ir.value=0;delay(200);} 
void digit(){switch(a[x]){
case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;}
lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);}

void init_sta(){ 
  wireWrite(I2C_STA339,0x00,0b01101011);
  //[2:0][MCS2 MCS1 MCS0]=011(64*FS)
  //[4:3][IR1 IR0]=10(192K)
  //[5][TWRB]=1(TEMP_ERR АВТОВКЛЮЧ.ПОСЛЕ ПЕРЕГРЕВА-АКТИВНА)
  //[6][TWAB]=0(ОГРАНИЧ ВЫХ МОЩНОСТИ ПРИ ПЕРЕГРЕВЕ-АКТИВНА)0X37 - УКАЗАТЬ УРОВ. ОГР. МОЩН.
  //[7][FDRB]=0(ЗАЩИТА.ОТКЛ.ПИТАНИЯ ПРИ НЕИСПРАВНОСТИ-АКТИВНА)
  wireWrite(I2C_STA339,0x01,0b10100000); 
  //[3:0][SAI]=0000(I2S 64*fs 16 to 24 bit) 
  //[4][SAIFB]=0(MSB) MSB/LSB-first
  //[5][DSCKE]=1(АКТИВНА ТАКТОВАЯ ЗАДЕРЖКА)
  //[7:6][C1IM C2IM]=10(СОПАСТОНОВЛЕНИЕ КАНАЛОВ)DEF
  wireWrite(I2C_STA339,0x02,0b10011110);
  //[1:0][OM0 OM1]=10( FFX power output mode)DEF
  //[5:2][CSZ]=0111(FFX compensating pulse size register)DEF
  //[6][RESERVED]
  //[7][OCRB]=1(ЗАЩИТА ПО ТОКУ-АКТИВНА)
  wireWrite(I2C_STA339,0x03,0b01000000);
  //[0][HPB]=0(ФИЛЬТР HPB АКТИВЕН)DEF ОПАСНО ВЫКЛЮЧАТЬ!!! 
  //[1][DEMP]=0(De-emphasis)OFF
  //[2][DSPB]=0(DSP bypass)АКТИВНА-РЕГ.ТЕМБРА АКТИВНЫ
  //[3][PSL]=0(Postscale link)DEF
  //[4][BQL]=0(Biquad coefficient link)DEF
  //[5][DCR]=0(Dynamic range compression)DEF
  //[6][ZDE]=1(Zero-detect mute enable)DEF
  //[7][SME]=0(Submix mode enable)DEF  
  wireWrite(I2C_STA339,0x04,0b11000010);//def
  //[0][MPCV]=0(Max power correction variable)DEF
  //[1][MPC]=1(Max power correction)DEF
  //[2][NSBW]=0(Noise-shaper bandwidth selection)DEF
  //[3][AME]=0(AM mode enable)DEF
  //[4][PWMS]=1(PWM speed mode)DEF
  //[5][DCCV]=0(Distortion compensation variable enable)DEF
  //[6][ZCE]=1(Zero-crossing volume enable)DEF
  //[7][SVE]=1(Soft volume update enable)DEF
  wireWrite(I2C_STA339,0x05,0b11011100);
  //[1:0][OCFG]=00(1A/1B > 1A/1B | 2A/2B > 2A/2B)
  //[2][IDE]=1(Invalid input detect mute enable)DEF
  //[3][BCLE]=1(Binary output mode clock loss detection)DEF
  //[4][LDTE]=1(LRCK double trigger protection)DEF
  //[5][ECLE]=0(Auto EAPD on clock loss)DEF
  //[6][PWDN]=1(IC power down)DEF
  //[7][EAPD]=1(ВНЕШНЕЕ ПИТАНИЕ УСИЛИТЕЛЯ)АКТИВНО
  wireWrite(I2C_STA339,0x06,0b00010000);// DEF
  //Mute/line output configuration register
  wireWrite(I2C_STA339,0x07,0);// 0 dB
  //Master volume register BYTE 0...0xFE = 0 dB...-127,5 | 0xFF = MUTE
   wireWrite(I2C_STA339,0x08,vol+ball);// 0 dB
  //Channel 1 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE
  wireWrite(I2C_STA339,0x09,vol-ball);
  //Channel 2 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE 
  wireWrite(I2C_STA339,0x0A,255);
  //Channel 3 volume BYTE 0...0xEC = +48...-80 dB 0xFF = MUTE   
  wireWrite(I2C_STA339,0x0B,0b10000000);//DEF
  //[4:5][AMGC](Audio preset register 1)
  //Audio preset gain compression/limiters selection
  wireWrite(I2C_STA339,0x0C,0b00001000|bass_q<<4);
  //[0][amame]
  //[3:1][AMAM]=000(AM interference frequency switching)DEF
  //[7:4][XO]=0000(Кроссовер для управления басами)byte 0000...0001...1111 = DEF...80Hz...360Hz
  wireWrite(I2C_STA339,0x0E,0b00000000);
  //[0][TCB]=0(Tone control bypass)DEF
  //[1][EQBP]=0(EQ bypass)DEF
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x0F,0b01000000);
  //[0][TCB]=0(Tone control bypass)DEF
  //[1][EQBP]=0(EQ bypass)DEF
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x10,0b10000000);
  //[1:0]RESERVED
  //[2][VBP]=0(Volume bypass)DEF
  //[3][BO]=0(Binary output enable)DEF
  //[5:4][LS]=00(Channel limiter mapping as a function of CxLS bits)DEF
  //[7:6][OM]=00(Channel output mapping as a function of CxOM bits)DEF
  wireWrite(I2C_STA339,0x11,0|bass|treb<<4);// тембр ВЧ НЧ 
  wireWrite(I2C_STA339,0x12,0b01101010);
  //(Limiter 1 attack/release rate)DEF
  wireWrite(I2C_STA339,0x13,0b01101001);
  //(Limiter 1 attack/release threshold )DEF
  wireWrite(I2C_STA339,0x14,0b01101010);
  //(Limiter 2 attack/release rate)DEF
  wireWrite(I2C_STA339,0x15,0b01101001);
  //(Limiter 2 attack/release threshold)DEF
  wireWrite(I2C_STA339,0x33,0b00110000);
  //(Limiter 1 extended release threshold)DEF
  wireWrite(I2C_STA339,0x34,0b00110000);
  //(Limiter 2 extended attack threshold)DEF
  wireWrite(I2C_STA339,0x35,0b00110000);
  //(Limiter 2 extended release threshold)DEF
  wireWrite(I2C_STA339,0x16,255);
  //(Coefficient address register)DEF
  wireWrite(I2C_STA339,0x17,255);
  wireWrite(I2C_STA339,0x18,255);
  wireWrite(I2C_STA339,0x19,255);
  //(Coefficient b1 data register bits)DEF
  wireWrite(I2C_STA339,0x1A,255);
  wireWrite(I2C_STA339,0x1B,255);
  wireWrite(I2C_STA339,0x1C,255);
  //(Coefficient b2 data register bits)DEF
  wireWrite(I2C_STA339,0x1D,255);
  wireWrite(I2C_STA339,0x1E,255);
  wireWrite(I2C_STA339,0x1F,255);
  //(Coefficient a1 data register bits)DEF
  wireWrite(I2C_STA339,0x20,255);
  wireWrite(I2C_STA339,0x21,255);
  wireWrite(I2C_STA339,0x22,255);
  //(Coefficient a2 data register bits)DEF
  wireWrite(I2C_STA339,0x23,255);
  wireWrite(I2C_STA339,0x24,255);
  wireWrite(I2C_STA339,0x25,255);
  //(Coefficient b0 data register bits)DEF 
  wireWrite(I2C_STA339,0x26,255);
  //(Coefficient read/write control register)DEF 
  wireWrite(I2C_STA339,0x27,0b00011010);
  wireWrite(I2C_STA339,0x28,0b11000000);
  //(Variable max power correction registers)DEF 
  wireWrite(I2C_STA339,0x29,0b11110011);
  wireWrite(I2C_STA339,0x2A,0b00110011);
  //(Distortion compensation registers)DEF
  wireWrite(I2C_STA339,0x2B,0b00000000);
  wireWrite(I2C_STA339,0x2C,0b00001100);
  //(Distortion compensation registers)DEF 
  wireWrite(I2C_STA339,0x31,0b00000011);
  //[1:0][SEL]=00( EQ RAM select)DEF
  //[2]RESERVED
  //[4:3][AMGC]=00(Anticlipping and DRC preset)DEF
  //[6:5]RESERVED
  //[7][BOX]=0(This bit can be used to bypass the crossover filters)DEF
  wireWrite(I2C_STA339,0x36,0b00000000);
  //(Extended configuration register)DEF
  wireWrite(I2C_STA339,0x37,0b00000000);
  wireWrite(I2C_STA339,0x38,0b00000000);
  //(Soft volume configuration registers)DEF
  wireWrite(I2C_STA339,0x39,0b00000001);
  wireWrite(I2C_STA339,0x3A,0b11101110);
  wireWrite(I2C_STA339,0x3B,0b11111111);
  wireWrite(I2C_STA339,0x3C,0b01111110);
  wireWrite(I2C_STA339,0x3D,0b11000000);
  wireWrite(I2C_STA339,0x37,0b00100110);
  //(DRC RMS filter coefficients)DEF
}


byte wireRead(byte addr, byte reg){ // I2C
  Wire.beginTransmission(addr);
  Wire.write (reg);
  Wire.endTransmission();
  Wire.requestFrom(addr,1);
  while(Wire.available()<1);
  byte value = Wire.read();
  return value;
  }  
 
void wireWrite(byte addr, byte reg, byte data){
  Wire.beginTransmission(addr);
  Wire.write (reg);
  Wire.write (data);
  Wire.endTransmission();
  } 

void to_Timer(){newPosition = myEnc.read()/4;}