Try this code.
//TM7709 24bit ADC module arduino sketch
//coldtears electronics
//code reworked to read in grams
//default connection arduino UNO
//DRDY = pin 11 >MOSI
//ADIO = pin 12 >MISO
//SCLK = pin 13 >SCK
//default connection arduino MEGA
//DRDY = pin 21 >MOSI
//ADIO = pin 22 >MISO
//SCLK = pin 28 >SCK
#define TM7710_DRDY 3
#define TM7710_ADIO 4
#define TM7710_ADIO_OUT() DDRB|=1<<4
#define TM7710_ADIO_IN() DDRB&=~(1<<4)
#define Set_TM7710_SCLK() PORTB|=1<<5
#define Set_TM7710_ADIO() PORTB|=1<<4
#define Clr_TM7710_SCLK() PORTB&=~(1<<5)
#define Clr_TM7710_ADIO() PORTB&=~(1<<4)
unsigned char x[3];
long Result;
float vref=4.89;
double microvolt = 0;
long microvoltA = 456;
long microvoltB = 180;
int extradata1 = 777;
int extradata2 = 888;
float loadA = 0; //this is the first weight (we use no load at the sensor) our sensor already has as a small plate on it we use a preload.
float loadB = 1590; //this is the second calibrated weight in grams
float valueA = 173; //value of the the serialmonitor reading with the first known weigth at the sensor (we use no load at the sensor).
float valueB = 9360; //value of the the serialmonitor reading with the second known load at the sensor (we 1590 grams at the sensor).
float load;
long finalValue;
void setup()
{
DDRB|=1<<5 ;
delay(1000);
Serial.begin(57600);// set baudrate for PC or serialmonitor the same!
TM7710_Init();
}
void loop()
{
while((PINB&(1<<TM7710_DRDY))==(1<<TM7710_DRDY));
TM7710_start();
TM7710_write(0x7F);
TM7710_ADIO_IN();
for(unsigned char j=0;j<3;j++)
{
x[j]=TM7710_read();
}
TM7710_ADIO_OUT();
TM7710_stop();
Result=x[0];
Result = Result * 256;
Result = Result + x[1];
Result = Result * 256;
Result = Result + x[2];
Result = Result - 6912000;
double volt = Result * vref /16/6912000; //data coming from this line is ..... "0.123456" for example
long microvolt = (volt*1000000); // microvolts are easier to break down in blocks of 3 then floats so we multiply with 1000000.
//split the microvolt value in blocks of three digits so it can be parsed to the serial output by Serial.print as integer value as it is lower as 1024.
//sample value of volt = "123456" so we brake it down in 123 and 456 <> volt.
//In our case it will never go below 0 or above 999999
//float (load.substring (0,2)); // we need the three most right side digits
//float (load.substring (3,5)); //we need the three most left side digits
//at the PC side VB6 should detect the "letter - comma - value" and parse it in a textbox
// how things work with Processing I have no idea yet :-(.
float load = ((loadB - loadA)/(valueB - valueA)) * ( microvolt - valueA) + loadA; //the final value of the real load on the sensor
Serial.print ("A,");
Serial.println (microvoltA);
Serial.print ("B,");
Serial.println (microvoltB);
Serial.print ("C,");
Serial.println (extradata1);
Serial.print ("D,");
Serial.println (extradata2);
Serial.print ("E,");
Serial.println (microvolt); //raw sensor value in microvolts
Serial.print ("F,");
Serial.println (load,0); // value in grams
}
void TM7710_Init()
{
TM7710_ADIO_OUT();
delay(100);
TM7710_stop();
TM7710_start();
TM7710_write(0xBF);
TM7710_write(0x20); //Gain=128
//TM7710_write(0x00); //Gain=16
TM7710_stop();
}
void TM7710_start(void)
{
Clr_TM7710_ADIO();
delayMicroseconds(1);
Clr_TM7710_SCLK();
delayMicroseconds(1);
}
void TM7710_stop(void)
{
Clr_TM7710_ADIO();
delayMicroseconds(1);
Set_TM7710_SCLK();
delayMicroseconds(1);
Set_TM7710_ADIO();
delayMicroseconds(1);
}
void TM7710_write(unsigned char dd)
{
unsigned char i;
for(i=8;i>0;i--)
{
if(dd&0x80)
Set_TM7710_ADIO();
else
Clr_TM7710_ADIO();
delayMicroseconds(1);
Set_TM7710_SCLK();
delayMicroseconds(1);
Clr_TM7710_SCLK();
dd<<=1;
}
}
unsigned char TM7710_read(void)
{
unsigned char data=0,i;
for(i=0;i<8;i++)
{
Set_TM7710_SCLK();
data=data<<1;
if((PINB&(1<<TM7710_ADIO))==(1<<TM7710_ADIO))
{
data=data+1;
}
delayMicroseconds(1);
Clr_TM7710_SCLK();
delayMicroseconds(1);
}
return data;
}
void printFloat(float value, int places) {
// this is used to cast digits
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;
// if value is negative, set tempfloat to the abs value
// calculate rounding term d: 0.5/pow(10,places)
float d = 0.5;
if (value < 0)
d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
d/= 10.0;
// this small addition, combined with truncation will round out
tempfloat += d;
if (value < 0)
tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat)
{
tens *= 10.0;
tenscount += 1;
}
// write out the negative if needed
if (value < 0)
//Serial.print('-');
if (tenscount == 0)
//Serial.print(0, DEC);
for (i=0; i< tenscount; i++)
{
digit = (int) (tempfloat/tens);
//Serial.print(digit, DEC);
tempfloat = tempfloat - ((float)digit * tens);
tens /= 10.0;
}
// if no places after decimal, stop now and return
if (places <= 0)
return;
// otherwise, write the point and continue on
//Serial.print('.');
for (i = 0; i < places; i++)
{
tempfloat *= 10.0;
digit = (int) tempfloat;
//Serial.print(digit,DEC);
// once written, subtract off that digit
tempfloat = tempfloat - (float) digit;
}
}