Тема: TDA7468 - обновленная библиотека (полный функционал)
Данный контент доступен только зарегистрированным пользователям.
TDA7468.h
// rcl-radio.ru liman324@yandex.ru
// Библиотека протестирована
#ifndef TDA7468_H
#define TDA7468_H
#define TDA7468_address 0x44
//Sub addresses
#define TDA7468_INPUT_SELECT 0b00000000
#define TDA7468_INPUT_GAIN 0b00000001
#define TDA7468_SURROUND 0b00000010
#define TDA7468_VOLUME_LEFT 0b00000011
#define TDA7468_VOLUME_RIGHT 0b00000100
#define TDA7468_TREBLE_BASS 0b00000101
#define TDA7468_OUTPUT 0b00000110
#define TDA7468_BASS_ALC 0b00000111
#include <Arduino.h>
class TDA7468
{
public:
TDA7468();
void setInput(int input, int mute_mic, int mic); // input 0...4 === IN1...IN4
// mute_mic 0...1 === OFF...ON
// mic 0...3 === gain 14dB...0dB // mic 4 === OFF
void setGain(int gain); // 0...7 0...14 dB // step 2dB
void setSurround(int sur, int gain1, int mix, int buf); // sur 1 on 0 off // gain 0...3 // mix 0...7 // buf 1 on 0 off
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
void setOutput(int output); // mute 0 on 1 off
void setAlc(int mode, int detector, int circ, int attack, int tresh, int rez);
// mode 1 on 0 off
// detector 1 on 0 off
// circ 1 on 0 off
// attack 12k5 25k 50k 100k 0...3
// tresh 700mVrms 485mVrms 320mVrms 170mVrms 0...3
// rez MODE 1: Fixed Resistor / MODE 2: Adaptive 0...1
private:
void writeWire(char a, char b);
};
#endif //TDA7468_H
TDA7468.cpp
#include <Arduino.h>
#include <Wire.h>
#include "TDA7468.h"
TDA7468::TDA7468(){
Wire.begin();
}
void TDA7468::setInput(int input, int mute_mic, int mic){
switch (input) {
case 0: input = 0b00000000 + (mute_mic << 2) + (mic << 3);break;
case 1: input = 0b00000001 + (mute_mic << 2) + (mic << 3);break;
case 2: input = 0b00000010 + (mute_mic << 2) + (mic << 3);break;
case 3: input = 0b00000011 + (mute_mic << 2) + (mic << 3);break;
}
writeWire(TDA7468_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(TDA7468_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(TDA7468_SURROUND,sum_sur);
}
void TDA7468::setVol_R(int vol_r){
vol_r = 62-vol_r;
writeWire(TDA7468_VOLUME_RIGHT,vol_r);
}
void TDA7468::setVol_L(int vol_l){
vol_l = 62-vol_l;
writeWire(TDA7468_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 7: treb = 0b00001000;break;
case 6: treb = 0b00001001;break;
case 5: treb = 0b00001010;break;
case 4: treb = 0b00001011;break;
case 3: treb = 0b00001100;break;
case 2: treb = 0b00001101;break;
case 1: treb = 0b00001110;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 7: bass = 0b10000000;break;
case 6: bass = 0b10010000;break;
case 5: bass = 0b10100000;break;
case 4: bass = 0b10110000;break;
case 3: bass = 0b11000000;break;
case 2: bass = 0b11010000;break;
case 1: bass = 0b11100000;break;
}
int sum_treb_bass = treb + bass;
writeWire(TDA7468_TREBLE_BASS, sum_treb_bass);
}
void TDA7468::setOutput(int output){
switch (output) {
case 0: output = 0b00000000;break;
case 1: output = 0b00000001;break;
}
writeWire(TDA7468_OUTPUT, 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(TDA7468_BASS_ALC ,sum_alc);
}
void TDA7468::writeWire(char a, char b){
Wire.beginTransmission(TDA7468_address);
Wire.write (a);
Wire.write (b);
Wire.endTransmission();
}
test.ino
#include <Wire.h>
#include <TDA7468.h>
TDA7468 tda;
void setup(){
Serial.begin(9600);Wire.begin();
}
void loop(){
audio();
delay(1000);
}
// http://rcl-radio.ru/wp-content/uploads/2019/05/tda7468.pdf
void audio(){
tda.setInput(0,0,0);
// setInput(input,mute_mic,mic)
// input 0...4 === IN1...IN4
// mute_mic 0...1 === OFF...ON
// mic 0...3 === gain 14dB...0dB // mic 4 === OFF
///////////////////////////////////////////////////////////////
tda.setGain(0);
// setGain(gain);
// gain 0...7 === 0...14 dB // step 2dB
//////////////////////////////////////////////////////////////
tda.setSurround(0,0,0,0);
// setSurround(sur,gain,mix,buf);
// sur 1 === on // 0 === off (SURROUND MODE)
// gain 0...3 === 0...12 dB (GAIN)
// mix 0...7 (MIXING)
// buf 1 === on // 0 === off (BUFFER GAIN)
tda.setVol_R(62); // VOLUME R 0...62
tda.setVol_L(62); // VOLUME L 0...62
tda.setTreb_Bass(0,0); // -14...14 step 2 dB (TREBLE,BASS)
tda.setOutput(1); // MUTE 0 === ON // 1 === OFF
tda.setAlc(0,0,0,0,0,0);
// setAlc(mode,detector,circ,attack,tresh,rez);
// mode 1 on 0 off
// detector 1 on 0 off
// circ 1 on 0 off
// attack 12k5 25k 50k 100k 0...3
// tresh 700mVrms 485mVrms 320mVrms 170mVrms 0...3
// rez MODE 1: Fixed Resistor / MODE 2: Adaptive 0...1
}