This is a code sample for arduino to compute 0-120dB value into 4x 6 bit dividers truth table
// HMC472 Digital pins Truth table up to 31.5 dB
// ATT 16 – ATT8 – ATT4 – ATT2 – ATT1 – ATT0.5 – Value
// High High High High High High 0 dB
// High High High High High Low 0.5 dB
// Since this chip has a reversed Truth table, in order to write correct bits into dividers we compute bits in old fashion way then we negate values at the end
int debug = 1 ; // set 1 to enable serial console debugging. Disable it when done as it slows down computing time
float att = 0.5 ; // value in dB
int a = 0 ;
int b = 0 ;
int c = 0 ;
float d = 0 ;
int num;
int bit_0 ;
int bit_1 ;
int bit_2 ;
int bit_3 ;
int bit_4 ;
int bit_5 ;
String byte_A ;
String byte_B ;
String byte_C ;
String byte_D ;
void setup()
{
Serial.begin(9600);
compute() ;
if (debug == 1){debuging();}
}
void compute()
{
int times = att / 30 ;
if (times == 4) {
a = 30 , b=30, c = 30, d = att – (a + b +c) ;
}
if (times == 3) {
a = 30 , b = 30, c = 30, d = att – (a + b +c) ;
}
if (times == 2) {
a = 0 , b = 30, c = 30, d = att – (a + b +c) ;
}
if (times == 1) {
a = 0 , b = 0, c = 30, d = att – (a + b +c) ;
}
if (times == 0) {
a = 0 , b = 0, c = 0, d = att – (a + b +c) ;
}
num = a *2 ;
bit_0 = bitRead(num, 5);
bit_1 = bitRead(num, 4);
bit_2 = bitRead(num, 3);
bit_3 = bitRead(num, 2);
bit_4 = bitRead(num, 1);
bit_5 = bitRead(num, 0);
byte_A = String(!bit_0) + “,” + String(!bit_1) + “,” + String(!bit_2) + “,” + String(!bit_3) + “,” + String(!bit_4) + “,” + String(!bit_5);
num = b *2 ;
bit_0 = bitRead(num, 5);
bit_1 = bitRead(num, 4);
bit_2 = bitRead(num, 3);
bit_3 = bitRead(num, 2);
bit_4 = bitRead(num, 1);
bit_5 = bitRead(num, 0);
byte_B = String(!bit_0) + “,” + String(!bit_1) + “,” + String(!bit_2) + “,” + String(!bit_3) + “,” + String(!bit_4) + “,” + String(!bit_5);
num = c *2 ;
bit_0 = bitRead(num, 5);
bit_1 = bitRead(num, 4);
bit_2 = bitRead(num, 3);
bit_3 = bitRead(num, 2);
bit_4 = bitRead(num, 1);
bit_5 = bitRead(num, 0);
byte_C = String(!bit_0) + “,” + String(!bit_1) + “,” + String(!bit_2) + “,” + String(!bit_3) + “,” + String(!bit_4) + “,” + String(!bit_5);
num = d *2 ;
bit_0 = bitRead(num, 5);
bit_1 = bitRead(num, 4);
bit_2 = bitRead(num, 3);
bit_3 = bitRead(num, 2);
bit_4 = bitRead(num, 1);
bit_5 = bitRead(num, 0);
byte_D = String(!bit_0) + “,” + String(!bit_1) + “,” + String(!bit_2) + “,” + String(!bit_3) + “,” + String(!bit_4) + “,” + String(!bit_5);
// Here we should call 4 different instances of MCP23008 to write values into dividers
}
void debuging()
{
Serial.println(“Computing values for ” + String(att) + “dB”);
Serial.println(“Divider A attenuation = ” + String(a) + “dB”);
Serial.println(“Divider B attenuation = ” + String(b) + “dB”);
Serial.println(“Divider C attenuation = ” + String(c) + “dB”);
Serial.println(“Divider D attenuation = ” + String(d) + “dB”);
//////////////////////////////////////////////////////////////////////////
Serial.println(“writting divider A with ” + byte_A );
Serial.println(“writting divider B with ” + byte_B );
Serial.println(“writting divider C with ” + byte_C );
Serial.println(“writting divider D with ” + byte_D );
Serial.println(“Compute time = ” + String(millis()) + ” miliseconds” );
}
void loop()
{
delay(1000000000) ; // do nothing here
}