Hello!
I'm trying to build digital preamp using 3 TDA7314s.
For now I'm developing it with Arduino.
It also has encoder and LCD.
As Arduino has only 1 hardware i2c I decided to go with multiplexer CD4051.
My problem is that if I connect SDA line to one of the TDA7314s - it works perfectly. For now I just want to adjust volume.
But now I want to adjust volume on all chips, so I used multiplexer. It kinda works but it skips commands. For example - if I adjust volume from 20 to lets say 50 it stays the same for a while, and then suddenly jumps to the level of encoder, i.e. it skips or disturbs i2c transmissions.
I did a simple test - on pressing the button it should mute all channels. And it works approximately 2 times in 10.
Here is my code:
#include <Wire.h>
#include <LiquidCrystal.h>
int lcdRS = 3;
int lcdEn = 4;
int lcdD4 = 9;
int lcdD5 = 10;
int lcdD6 = 11;
int lcdD7 = 12;
int encoder0PinA = 6;
int encoder0PinB = 7;
int encoder0PinButton = 5;
int S0Pin = 16; //analog 0
int S1Pin = 15; //analog 1
int S2Pin = 14; //analog 2
int val;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int pressButtonLast = HIGH;
LiquidCrystal lcd(lcdRS, lcdEn, lcdD4, lcdD5, lcdD6, lcdD7);
int what = 0; // what are we doing 0 = volume
int bin [] = {000, 1, 10}; //for outputs 1, 2 and 3
void send_i2c(int);
void set_volume(void);
void select_output(int);
void setup() {
lcd.begin(16, 4);
lcd.print("hello, world!");
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode(S0Pin, OUTPUT); // s0
pinMode(S1Pin, OUTPUT); // s1
pinMode(S2Pin, OUTPUT); // s2
Serial.begin (9600); //for debug
Wire.begin();
//read saved volume settings and send through wire
for (int i = 0; i <= 2; i++){
select_output(i); //we are doing it for each SDA
Wire.beginTransmission(B1000000); //TDA7314s address
// Wire.send(B00000000); // set volume to max
Wire.send(B10100000); //set attenuation on right
Wire.send(B10000000); //set attenuation on left
Wire.send(B01011100); //select input
Wire.endTransmission(); // stop transmitting
}
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
switch(what){
case 0:
for (int i = 0; i <= 2; i++){
select_output(i);
set_volume();
}
break;
default:
for (int i = 0; i <= 2; i++){
select_output(i);
set_volume();
}
break;
}
Serial.print (encoder0Pos);
Serial.print ("/");
lcd.setCursor(0, 1);
lcd.print(encoder0Pos);
}
if(digitalRead(encoder0PinButton) == LOW && pressButtonLast == HIGH){
Serial.print ("press/");
pressButtonLast = LOW;
}else{
if(digitalRead(encoder0PinButton) == HIGH)
pressButtonLast = HIGH;
}
encoder0PinALast = n;
}
void set_volume(void){
if(encoder0Pos > 63)
encoder0Pos = 63;
if(encoder0Pos < 0)
encoder0Pos = 0;
send_i2c(63 - encoder0Pos);
}
void send_i2c(int data){
Wire.beginTransmission(B1000000); //44?
Wire.send(data);
Wire.endTransmission(); // stop transmitting
}
void select_output(int output){
int row = bin[output];
int r0 = row & 0x01;
int r1 = (row>>1) & 0x01;
int r2 = (row>>2) & 0x01;
digitalWrite(S0Pin, r0);
digitalWrite(S1Pin, r1);
digitalWrite(S2Pin, r2);
}
I will be grateful for any input.
Leonti