1

Тема: LC75342

Данный контент доступен только зарегистрированным пользователям.

LC75342.h

// liman324@yandex.ru rcl-radio.ru
// 15.01.2022

#ifndef LC75342_H
#define LC75342_H

#include <Arduino.h>
class LC75342
{
  public:
    LC75342(uint8_t ce, uint8_t di, uint8_t cl);

    void addr();
    void set_input(byte in);
    // input 1...4 = byte 0...3 (byte 4...7 = All switches off)
    void set_gain(byte gain); 
    // gain 0...30 dB step 2 dB = byte 0...15
    void set_volume(byte vol);
    // volume 0...-79 dB = byte 0...79
    void set_treble(int treb);
    // treble 10...-10 dB step 2 dB = int 5...-5 
    void set_bass(int bass);
    // bass 20...-20 dB step 2 dB = int 10...-10 
    void set_ch(byte ch);
    // Channel Selection RCH = byte 2, LCH = byte 1, Left and right together = byte 3
    void test();
    	
  private:
        uint8_t CE;
        uint8_t DI;
        uint8_t CL;
};
	
#endif //LC75342_H

LC75342.cpp

#include <Arduino.h>
#include "LC75342.h"

LC75342::LC75342(uint8_t ce, uint8_t di, uint8_t cl){
	CE = ce;
        DI = di;
        CL = cl;
        pinMode(CE,OUTPUT);
        pinMode(DI,OUTPUT);
        pinMode(CL,OUTPUT);
        digitalWrite(CL,LOW);
        digitalWrite(CE,LOW);
        digitalWrite(DI,LOW);
}

void LC75342::addr(){
       digitalWrite(CL,LOW);
       digitalWrite(CE,LOW);
       byte addr = 0b01000001; // 0x82
    for(int i = 7; i >= 0; i--){
       digitalWrite(CL,LOW);
       digitalWrite(DI, (addr >> i) & 0x01);
       digitalWrite(CL,HIGH);
       }  
       digitalWrite(CE,HIGH); 
}

void LC75342::set_input(byte in){
    for(int i = 0; i <= 3; i++){
       digitalWrite(CL,LOW);
    switch(i){
       case 0: digitalWrite(DI, (in & 0b0001)>>0);break;
       case 1: digitalWrite(DI, (in & 0b0010)>>1);break;
       case 2: digitalWrite(DI, (in & 0b0100)>>2);break;
       case 3: digitalWrite(DI, (in & 0b1000)>>3);break;
} 
       digitalWrite(CL,HIGH);
       }
}

void LC75342::set_gain(byte gain){
    for(int i = 0; i <= 3; i++){
       digitalWrite(CL,LOW);
    switch(i){
       case 0: digitalWrite(DI, (gain & 0b0001)>>0);break;
       case 1: digitalWrite(DI, (gain & 0b0010)>>1);break;
       case 2: digitalWrite(DI, (gain & 0b0100)>>2);break;
       case 3: digitalWrite(DI, (gain & 0b1000)>>3);break;
} 
       digitalWrite(CL,HIGH);
       }
}

void LC75342::set_volume(byte vol){
    for(int i = 0; i <= 7; i++){
       digitalWrite(CL,LOW);
    switch(i){
       case 0: digitalWrite(DI, (vol & 0b00000001)>>0);break;
       case 1: digitalWrite(DI, (vol & 0b00000010)>>1);break;
       case 2: digitalWrite(DI, (vol & 0b00000100)>>2);break;
       case 3: digitalWrite(DI, (vol & 0b00001000)>>3);break;
       case 4: digitalWrite(DI, (vol & 0b00010000)>>4);break;
       case 5: digitalWrite(DI, (vol & 0b00100000)>>5);break;
       case 6: digitalWrite(DI, (vol & 0b01000000)>>6);break;
       case 7: digitalWrite(DI, (vol & 0b10000000)>>7);break;
} 
       digitalWrite(CL,HIGH);
       }
}

void LC75342::set_treble(int treb){
      switch(treb){
        case 5:  treb = 0b1010;break;//10dB
        case 4:  treb = 0b0010;break;//8dB
        case 3:  treb = 0b1100;break;//6dB
        case 2:  treb = 0b0100;break;//4dB
        case 1:  treb = 0b1000;break;//2dB
        case 0:  treb = 0b0000;break;//0dB
        case -1: treb = 0b1001;break;//-2dB
        case -2: treb = 0b0101;break;//-4dB
        case -3: treb = 0b1101;break;//-6dB
        case -4: treb = 0b0011;break;//-8dB
        case -5: treb = 0b1011;break;//10dB
        }
      for(int i = 3; i >= 0; i--){
        digitalWrite(CL,LOW);
        digitalWrite(DI, (treb >> i) & 0x01);
        digitalWrite(CL,HIGH);
        }
}

void LC75342::set_bass(int bass){
      switch(bass){
        case 10:  bass = 0b010100;break;//20dB
        case 9 :  bass = 0b100100;break;//18dB
        case 8 :  bass = 0b000100;break;//16dB
        case 7 :  bass = 0b111000;break;//14dB
        case 6 :  bass = 0b011000;break;//12dB
        case 5 :  bass = 0b101000;break;//10dB   
        case 4 :  bass = 0b001000;break;//8dB  
        case 3 :  bass = 0b110000;break;//6dB 
        case 2 :  bass = 0b010000;break;//4dB  
        case 1 :  bass = 0b101000;break;//2dB
        case 0 :  bass = 0b000000;break;//0dB
        case -1:  bass = 0b100010;break;//-2dB
        case -2:  bass = 0b010010;break;//-4dB
        case -3:  bass = 0b110010;break;//-6dB
        case -4:  bass = 0b001010;break;//-8dB
        case -5:  bass = 0b101010;break;//-10dB
        case -6:  bass = 0b011010;break;//-12dB
        case -7:  bass = 0b111010;break;//-14dB
        case -8:  bass = 0b000110;break;//-16dB
        case -9:  bass = 0b100110;break;//-18dB
        case -10: bass = 0b010110;break;//-20dB
        }
      for(int i = 5; i >= 0; i--){
        digitalWrite(CL,LOW);
        digitalWrite(DI, (bass >> i) & 0x01);
        digitalWrite(CL,HIGH);
        }
}

void LC75342::set_ch(byte ch){
      switch(ch){
        case 1: ch = 0b01;break;
        case 2: ch = 0b10;break;
        case 3: ch = 0b11;break;
      }
      for(int i = 1; i >= 0; i--){
        digitalWrite(CL,LOW);
        digitalWrite(DI, (ch >> i) & 0x01);
        digitalWrite(CL,HIGH);
        }
}

void LC75342::test(){
      byte test = 0;
      for(int i = 3; i >= 0; i--){
        digitalWrite(CL,LOW);
        digitalWrite(DI, (test >> i) & 0x01);
        digitalWrite(CL,HIGH);
        }
      digitalWrite(CL,LOW);
      digitalWrite(CE,LOW); 
}

test.ino

#include <LC75342.h>

LC75342 lc(2,3,4);   // CE,DI,CL

void setup(){
  audio();
  }

void loop(){}

void audio(){
  lc.addr();
  lc.set_input(0);   // input 1...4 = byte 0...3 (byte 4...7 = All switches off)
  lc.set_gain(0);    // gain 0...30 dB step 2 dB = byte 0...15
  lc.set_volume(0);  // volume 0...-79 dB = byte 0...79
  lc.set_treble(0);  // treble 10...-10 dB step 2 dB = int 5...-5 
  lc.set_bass(0);    // bass 20...-20 dB step 2 dB = int 10...-10
  lc.set_ch(3);      // Channel Selection RCH = byte 2, LCH = byte 1, Left and right together = byte 3 
  lc.test();
  }

Скетч использует 1214 байт (3%) памяти устройства. Всего доступно 32256 байт.
Глобальные переменные используют 12 байт (0%) динамической памяти, оставляя 2036 байт для локальных переменных. Максимум: 2048 байт.