1

Тема: TDA7468 - библиотека

Версия 1

TDA7468.h

#ifndef TDA7468_H
#define TDA7468_H
#define TDA7468_address 0x44

//Sub addresses
#define INPUT_SELECT 0b00000000
#define INPUT_GAIN   0b00000001
#define SURROUND     0b00000010
#define VOLUME_LEFT  0b00000011
#define VOLUME_RIGHT 0b00000100
#define TREBLE_BASS  0b00000101
#define OUTPUT_MUTE  0b00000110
#define BASS_ALC     0b00000111

#include <Arduino.h>
class TDA7468{
public:
TDA7468();
void setInput(int input);              // (IN1, IN2, IN3, IN4) 0...3
void setGain(int gain);                // (0...14 dB step 2dB) 0...7
void setSurround(int buf);             // BUFFER GAIN 0=0dB 1=6dB
void setVol_R(int vol_r);              // 0...62 
void setVol_L(int vol_l);              // 0...62
void setTreb_Bass(int treb, int bass); // -14...14 step 2 dB -7...7
void setOutput(int output);            // MUTE 0=on 1=off
void setAlc(); 
private:
	void writeWire(int8_t a, int8_t b);
};
#endif //TDA7468_H

TDA7468.cpp

#include <Arduino.h>
#include <Wire.h>
#include "TDA7468.h"

TDA7468::TDA7468(){
	Wire.begin();
}

void TDA7468::setInput(int input){
  switch (input){
    case 0: input = 0b00101000;break;
	  case 1: input = 0b00101001;break;
	  case 2: input = 0b00101010;break;
	  case 3: input = 0b00101011;break;}
  writeWire(INPUT_SELECT,input);	
}

void TDA7468::setGain(int gain){
  switch (gain){
    case 0: gain = 0b00000000;break;
	  case 1: gain = 0b00000001;break;
	  case 2: gain = 0b00000010;break;
	  case 3: gain = 0b00000011;break;
    case 4: gain = 0b00000100;break;
	  case 5: gain = 0b00000101;break;
	  case 6: gain = 0b00000110;break;
	  case 7: gain = 0b00000111;break;}
  writeWire(INPUT_GAIN,gain);	
}

void TDA7468::setSurround(int buf){
  switch (buf) {
    case 0: buf = 0b01011000;break; // 0dB
	  case 1: buf = 0b00011000;break; // +6dB
}
  writeWire(SURROUND, buf);	
}

void TDA7468::setVol_R(int vol_r){
  vol_r = 0b00111110 - vol_r; // MIN -62dB
  writeWire(VOLUME_RIGHT, vol_r);	
}

void TDA7468::setVol_L(int vol_l){
  vol_l = 0b00111110 - vol_l; // MIN -62dB
  writeWire(VOLUME_LEFT, vol_l);	
}

void TDA7468::setTreb_Bass(int treb, int bass){
  switch (treb){
    case -7: treb = 0b00000000;break;
	  case -6: treb = 0b00000001;break;
	  case -5: treb = 0b00000010;break;
	  case -4: treb = 0b00000011;break;
    case -3: treb = 0b00000100;break;
	  case -2: treb = 0b00000101;break;
	  case -1: treb = 0b00000110;break;
	  case  0: treb = 0b00000111;break;
    case  1: treb = 0b00001110;break;
    case  2: treb = 0b00001101;break;
    case  3: treb = 0b00001100;break;
    case  4: treb = 0b00001011;break;
    case  5: treb = 0b00001010;break;
    case  6: treb = 0b00001001;break;
    case  7: treb = 0b00001000;break;
}
  switch (bass){
    case -7: bass = 0b00000000;break;
	  case -6: bass = 0b00010000;break;
	  case -5: bass = 0b00100000;break;
	  case -4: bass = 0b00110000;break;
    case -3: bass = 0b01000000;break;
	  case -2: bass = 0b01010000;break;
	  case -1: bass = 0b01100000;break;
	  case  0: bass = 0b01110000;break;
    case  1: bass = 0b11100000;break;
    case  2: bass = 0b11010000;break;
    case  3: bass = 0b11000000;break;
    case  4: bass = 0b10110000;break;
    case  5: bass = 0b10100000;break;
    case  6: bass = 0b10010000;break;
    case  7: bass = 0b10000000;break;
}
  int sum_treb_bass = treb + bass;
  writeWire(TREBLE_BASS, sum_treb_bass);	
}

void TDA7468::setOutput(int output){
  switch (output){
    case 0: output = 0b00000000;break;
	  case 1: output = 0b00000001;break;
}
  writeWire(OUTPUT_MUTE, output);	
}

void TDA7468::setAlc(){
  writeWire(BASS_ALC, 0b00000000);	
}
 
void TDA7468::writeWire(int8_t a, int8_t b){
  Wire.beginTransmission(TDA7468_address);
  Wire.write (a);
  Wire.write (b);
  Wire.endTransmission();
}

Версия 2

TDA7468D.h

#ifndef TDA7468_H
#define TDA7468_H
#define TDA7468_address 0x44

//Sub addresses
#define INPUT_SELECT 0b00000000
#define INPUT_GAIN   0b00000001
#define SURROUND     0b00000010
#define VOLUME_LEFT  0b00000011
#define VOLUME_RIGHT 0b00000100
#define TREBLE_BASS  0b00000101
#define OUTPUT_MUTE  0b00000110
#define BASS_ALC     0b00000111

#include <Arduino.h>
class TDA7468{
public:
  TDA7468();
void setInput(int input, int mutein, int mic); // RECOMMENDATION: (0,1,1)
                 // INPUT SELECT = input (IN1, IN2, IN3, IN4) 0...3
                 // MUTE (IN5) = mutein (IN5 0=ON 1=OFF) 0...1
                 // MIC = mic (0=ON, 1=OFF, Gain: 0dB, 6dB, 10dB, 14dB) 0...5
void setGain(int gain);                // (0...14 dB step 2dB) 0...7
void setSurround(int sur, int gain1, int mix, int buf); // RECOMMENDATION: (1,0,3,1)
                 // SURROUND MODE = sur 0=on 1=off 
                 // GAIN = gain (0dB 6dB 9dB 12dB) 0...3 
                 // MIXING = mix (INVERTING:100%,50%,25%,0%, NON-INVERTING:100%,50%,25%,mute) 0...7 
                 // BUFFER GAIN = buf 0=0dB 1=6dB
void setVol_R(int vol_r);              // 0...62 
void setVol_L(int vol_l);              // 0...62
void setTreb_Bass(int treb, int bass); // -14...14 step 2 dB -7...7
void setOutput(int output);            // mute 0=on 1=off
void setAlc(int mode, int detector, int circ, int attack, int tresh, int rez); // RECOMMENDATION: (0,0,0,0,0,0)
                 // ALC MODE = mode 1 on 0 off 
                 // DETECTOR = detector 1 on 0 off 
                 // RELEASE CURRENT CIRCUIT = circ 1 on 0 off 
                 // ATTACK TIME RESISTOR = attack (12.5k 25k 50k 100k) 0...3
                 // THRESHOLD = tresh (700mVrms 485mVrms 320mVrms 170mVrms) 0...3
                 // ATTACK MODE = rez (MODE 1: Fixed Resistor, MODE 2: Adaptive) 0...1
private:
  void writeWire(int8_t a, int8_t b);
};

#endif //TDA7468_H

TDA7468D.cpp

#include <Arduino.h>
#include <Wire.h>
#include "TDA7468D.h"

TDA7468::TDA7468(){
  Wire.begin();
}
void TDA7468::setInput(int input, int mutein, int mic){
  switch (input){
    case 0: input = 0b00000000;break;
    case 1: input = 0b00000001;break;
    case 2: input = 0b00000010;break;
    case 3: input = 0b00000011;break;
}
  switch (mutein){
    case 0: mutein = 0b00000100;break; // mute ON
    case 1: mutein = 0b00000000;break; // mute OFF
}
  switch (mic){
    case 0: mic = 0b00000000;break; // mic ON
    case 1: mic = 0b00100000;break; // mic OFF
    case 2: mic = 0b00011000;break; // GAIN 0 dB
    case 3: mic = 0b00010000;break;
    case 4: mic = 0b00001000;break;
    case 5: mic = 0b00000000;break;
}
  int sum_in = input + mutein + mic;
  writeWire(INPUT_SELECT, sum_in);  
}

void TDA7468::setGain(int gain){
  switch (gain){
    case 0: gain = 0b00000000;break;
    case 1: gain = 0b00000001;break;
    case 2: gain = 0b00000010;break;
    case 3: gain = 0b00000011;break;
    case 4: gain = 0b00000100;break;
    case 5: gain = 0b00000101;break;
    case 6: gain = 0b00000110;break;
    case 7: gain = 0b00000111;break;}
  writeWire(INPUT_GAIN, gain); 
}

void TDA7468::setSurround(int sur, int gain1, int mix, int buf){
  switch (sur){
    case 0: sur = 0b00000001;break;
    case 1: sur = 0b00000000;break;
}
  switch (gain1){
    case 0: gain1 = 0b00000000;break;
    case 1: gain1 = 0b00000010;break;
    case 2: gain1 = 0b00000100;break;
    case 3: gain1 = 0b00000110;break;
}
  switch (mix){
    case 0: mix = 0b00000000;break;
    case 1: mix = 0b00001000;break;
    case 2: mix = 0b00010000;break;
    case 3: mix = 0b00011000;break;
    case 4: mix = 0b00100000;break;
    case 5: mix = 0b00101000;break;
    case 6: mix = 0b00110000;break;
    case 7: mix = 0b00111000;break;
}
  switch (buf) {
    case 0: buf = 0b01000000;break;
    case 1: buf = 0b00000000;break;
}
  int sum_sur = sur + gain1 + mix + buf;
  writeWire(SURROUND, sum_sur); 
}
 
void TDA7468::setVol_R(int vol_r){
  vol_r = 0b00111110 - vol_r; // MIN -62dB
  writeWire(VOLUME_RIGHT, vol_r); 
}

void TDA7468::setVol_L(int vol_l){
  vol_l = 0b00111110 - vol_l; // MIN -62dB
  writeWire(VOLUME_LEFT, vol_l);  
}

void TDA7468::setTreb_Bass(int treb, int bass){
  switch (treb){
    case -7: treb = 0b00000000;break;
    case -6: treb = 0b00000001;break;
    case -5: treb = 0b00000010;break;
    case -4: treb = 0b00000011;break;
    case -3: treb = 0b00000100;break;
    case -2: treb = 0b00000101;break;
    case -1: treb = 0b00000110;break;
    case  0: treb = 0b00000111;break;
    case  1: treb = 0b00001110;break;
    case  2: treb = 0b00001101;break;
    case  3: treb = 0b00001100;break;
    case  4: treb = 0b00001011;break;
    case  5: treb = 0b00001010;break;
    case  6: treb = 0b00001001;break;
    case  7: treb = 0b00001000;break;
}
  switch (bass){
    case -7: bass = 0b00000000;break;
    case -6: bass = 0b00010000;break;
    case -5: bass = 0b00100000;break;
    case -4: bass = 0b00110000;break;
    case -3: bass = 0b01000000;break;
    case -2: bass = 0b01010000;break;
    case -1: bass = 0b01100000;break;
    case  0: bass = 0b01110000;break;
    case  1: bass = 0b11100000;break;
    case  2: bass = 0b11010000;break;
    case  3: bass = 0b11000000;break;
    case  4: bass = 0b10110000;break;
    case  5: bass = 0b10100000;break;
    case  6: bass = 0b10010000;break;
    case  7: bass = 0b10000000;break;
}
  int sum_treb_bass = treb + bass;
  writeWire(TREBLE_BASS, sum_treb_bass);  
}
 
void TDA7468::setOutput(int output){
  switch (output){
    case 0: output = 0b00000000;break;
    case 1: output = 0b00000001;break;
}
  writeWire(OUTPUT_MUTE, output);  
}
 
void TDA7468::setAlc(int mode, int detector, int circ, int attack, int tresh, int rez){
  switch (mode){
    case 0: mode = 0b00000001;break;
    case 1: mode = 0b00000000;break;
}
  switch (detector){
    case 0: detector = 0b00000010;break;
    case 1: detector = 0b00000000;break;
}
  switch (circ){
    case 0: circ = 0b00000100;break;
    case 1: circ = 0b00000000;break;
}
  switch (attack){
    case 0: attack = 0b00000000;break;
    case 1: attack = 0b00001000;break;
    case 2: attack = 0b00010000;break;
    case 3: attack = 0b00011000;break;
}
  switch (tresh){
    case 0: tresh = 0b00000000;break;
    case 1: tresh = 0b00100000;break;
    case 2: tresh = 0b01000000;break;
    case 3: tresh = 0b01100000;break;
}
  switch (rez){
    case 0: rez = 0b00000000;break;
    case 1: rez = 0b10000000;break;
}

  int sum_alc = mode + detector + circ + attack + tresh + rez;
  writeWire(BASS_ALC ,sum_alc); 
} 
void TDA7468::writeWire(int8_t a, int8_t b){
  Wire.beginTransmission(TDA7468_address);
  Wire.write (a);
  Wire.write (b);
  Wire.endTransmission();
}

test.ino

#include <Wire.h>
#include <TDA7468D.h>
TDA7468 tda;

void setup(){ 
  Serial.begin(9600);Wire.begin();
  audio();
}
void loop(){
//delay(1000);
}
void audio(){
  tda.setInput(0,1,1); // INPUT 1, MUTE(IN5) OFF, MIC OFF
  tda.setGain(0); // GAIN 0 dB
  tda.setSurround(1,0,3,1);
  tda.setVol_R(62); // MAX
  tda.setVol_L(62); // MAX
  tda.setTreb_Bass(0,0);
  tda.setOutput(1); // MUTE OFF
  tda.setAlc(0,0,0,0,0,0); // ALC OFF
}