Thermistor with external ADC

posted this in one other place, but thought this might be a better place to put it…

I’m a newbie and have had good success following tutorials and individual code examples thus far. I have extensive experience in electronics but not so much in software.

I’m trying to work on a min/max temperature display that integrates the external ADC example here:
http://interface.khm.de/index.php/lab/experiments/connect-a-ltc2400-high-precision-24-bit-analog-to-digital-converter/

with the thermistor2 code (Ellaborate code cleaned up a bit)from here:
http://www.arduino.cc/playground/ComponentLib/Thermistor2

I got the both codes individually to work but the 10 bit internal ADC of the Uno does not support the resolution that I need for the device I’m working on. Can’t seem to figure out how to get the ADC voltage read from the SPI ADC to plug into the thermistor equation on the thermistor 2 code.

Any ideas?

posted this in one other place, but thought this might be a better place to put it

Then please delete the original post.

pulled!

Or ask a moderator to move the topic. Or ask a moderator to merge the two topics.

Will your project include any more SPI devices or just the analog-to-digital converter?

Just this one SPI device.

Do you want the long answer (learn more) or the short answer (get the project done)?

Long answer please! I do want to get the project done, but this is not the only thing I would like to do with arduino, so I want to learn as much as possible along the way.

Thanks in advance for your help!!

One way or the other you will be creating at least one function. Start here... http://www.arduino.cc/en/Reference/FunctionDeclaration

OK, makes sense.

What do you want me to do next?

The next step is to convert loop from this code...

http://interface.khm.de/index.php/lab/experiments/connect-a-ltc2400-high-precision-24-bit-analog-to-digital-converter/

...into a function you can call when you want a fresh value from the analog-to-digital converter. This should get you started...

float GetFreshVoltage( void )
{
  // loop code goes here
}

This will be important... http://www.arduino.cc/en/Reference/Return

Ok, here goes:

float GetAdcVoltage()
{
 cbi(PORTB,LTC_CS);             // LTC2400 CS Low
 delayMicroseconds(1);
 if (!(PINB & (1 << PORTB4))) {    // ADC Converter ready ?
   //    cli();
   ltw=0;
   sig=0;

   b0 = SPI_read();             // read 4 bytes adc raw data with SPI
   if ((b0 & 0x20) ==0) sig=1;  // is input negative ?
   b0 &=0x1F;                   // discard bit 25..31
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;

   delayMicroseconds(1);

   sbi(PORTB,LTC_CS);           // LTC2400 CS Low
   delay(200);

   if (sig) ltw |= 0xf0000000;    // if input negative insert sign bit
   ltw=ltw/16;                    // scale result down , last 4 bits have no information
   volt = ltw * v_ref / 16777216; // max scale
   return volt;
   
   //Serial.print(cnt++);
   //Serial.print(";  ");
   printFloat(volt,6);           // print voltage as floating number
   Serial.println("  ");

 }
 sbi(PORTB,LTC_CS); // LTC2400 CS hi
 delay(20);
}

I modified the name of the function just for ease of recalling what I’m doing. Also made the changes to PORTB4 vice PB4 since I figured out originally that that was a mistake. As far as using the “return”, the voltage indicated by the term “volt” is what I want to acquire, so i I used “return volt;”

Was that correct usage in this case?

   return volt;
   
   //Serial.print(cnt++);
   //Serial.print(";  ");
   printFloat(volt,6);           // print voltage as floating number
   Serial.println("  ");

 }
 sbi(PORTB,LTC_CS); // LTC2400 CS hi
 delay(20);
}

Nothing after the return statement will be executed. If the if test evaluates to false, there is no return statement.

The Arduino IDE suppresses all warnings (foolishly, in my opinion), or you would have been warned of these problems.

Thanks for the heads up Paul. I have moved the return statement to the end of the function. I will be back to working on my project on Tuesday 10/11 and will post my results.

float GetAdcVoltage()
{
 cbi(PORTB,LTC_CS);             // LTC2400 CS Low
 delayMicroseconds(1);
 if (!(PINB & (1 << PORTB4))) {    // ADC Converter ready ?
   //    cli();
   ltw=0;
   sig=0;

   b0 = SPI_read();             // read 4 bytes adc raw data with SPI
   if ((b0 & 0x20) ==0) sig=1;  // is input negative ?
   b0 &=0x1F;                   // discard bit 25..31
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;
   ltw <<= 8;
   b0 = SPI_read();
   ltw |= b0;

   delayMicroseconds(1);

   sbi(PORTB,LTC_CS);           // LTC2400 CS Low
   delay(200);

   if (sig) ltw |= 0xf0000000;    // if input negative insert sign bit
   ltw=ltw/16;                    // scale result down , last 4 bits have no information
   volt = ltw * v_ref / 16777216; // max scale
   //Serial.print(cnt++);
   //Serial.print(";  ");
   printFloat(volt,6);           // print voltage as floating number
   Serial.println("  ");
  }
 sbi(PORTB,LTC_CS); // LTC2400 CS hi
 delay(20);
 return volt;
}

One other question…

Why at the beginnign of the external ADC code does it define the CS, MISO, and SCK as 2, 4 and 5 when the pins used are digital 10, 12, and 13?

#define LTC_CS 2         // LTC2400 Chip Select Pin  on Portb 2
#define LTC_MISO  4      // LTC2400 SDO Select Pin  on Portb 4
#define LTC_SCK  5       // LTC2400 SCK Select Pin  on Portb 5

According to the comments, there’s some direct port manipulation going on, portb pins are 8-13 according to the reference: http://www.arduino.cc/en/Reference/PortManipulation

Thanks to all who assisted me with getting the thermistor read fixed using the external ADC. I tried it today and added a RTC and it worked great. I plan to add an LCD and a datalogger (either SD Card or EEProm). Once I get it all going I'll post the project.

Right now I have the current temp, min/max with timestamps and the current time and date displayed.

Again, thanks to everyone!!