Pages: [1]   Go Down
Author Topic: Thermocouple amp with MUX on a mega  (Read 1057 times)
0 Members and 1 Guest are viewing this topic.
Melbourne, Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

Just bought this shield to get a few thermocouples connected to my arduino data logger: http://www.oceancontrols.com.au/KTA-259.html

It has a MAX6675 thermocouple amp on board with a ADG608 mux. Not too used to these devices and a beginner with the arduino so I was relying on the example code (which works ona deumi but not on the mega):

Code:
/*
  Ocean Controls k-type board KTA259 TC Mux
  Neil Davey
  06-02-11  
*/

#include <SPI.h>

#define CS_TEMP  9 // MAX6674/6675 /CS Line
#define MUX_EN   7 // ADG608 MUX Enable
#define MUX_A0   4 // ADG608 Addr0
#define MUX_A1   5 // ADG608 Arrd1
#define MUX_A2   6 // ADG608 Addr2

//-------------------------
//-------------------------
void Set_Mux_Channel(unsigned char chan)
{
  switch(chan)
  {
    //----------------------
    // MUX Channel 1
    case 1:
      digitalWrite(MUX_A0,LOW);
      digitalWrite(MUX_A1,LOW);
      digitalWrite(MUX_A2,LOW);
      break;
    //----------------------
    // MUX Channel 2
    case 2:
      digitalWrite(MUX_A0,HIGH);
      digitalWrite(MUX_A1,LOW);
      digitalWrite(MUX_A2,LOW);
      break;
    //----------------------
    // MUX Channel 3
    case 3:
      digitalWrite(MUX_A0,LOW);
      digitalWrite(MUX_A1,HIGH);
      digitalWrite(MUX_A2,LOW);
      break;
    //----------------------
    // MUX Channel 4
    case 4:
      digitalWrite(MUX_A0,HIGH);
      digitalWrite(MUX_A1,HIGH);
      digitalWrite(MUX_A2,LOW);
      break;
    //----------------------
    // MUX Channel 5
    case 5:
      digitalWrite(MUX_A0,LOW);
      digitalWrite(MUX_A1,LOW);
      digitalWrite(MUX_A2,HIGH);
      break;
    //----------------------
    // MUX Channel 6
    case 6:
      digitalWrite(MUX_A0,HIGH);
      digitalWrite(MUX_A1,LOW);
      digitalWrite(MUX_A2,HIGH);
      break;
    //----------------------
    // MUX Channel 7
    case 7:
      digitalWrite(MUX_A0,LOW);
      digitalWrite(MUX_A1,HIGH);
      digitalWrite(MUX_A2,HIGH);
      break;
    //----------------------
    // MUX Channel 8
    case 8:
      digitalWrite(MUX_A0,HIGH);
      digitalWrite(MUX_A1,HIGH);
      digitalWrite(MUX_A2,HIGH);
      break;
    //----------------------
    default:
      break;
  }
}

//-------------------------
//-------------------------
int Read_Temperature(void)
{
unsigned int temp_reading;

  // force conversion now..
  delay(5);
  digitalWrite(CS_TEMP,LOW); // Set MAX7765 /CS Low
  delay(5);
  digitalWrite(CS_TEMP,HIGH); // Set MAX7765 /CS High
  delay(250); // wait for conversion to finish..
  
  // read result
  digitalWrite(CS_TEMP,LOW); // Set MAX7765 /CS Low
  delay(1);
  
  temp_reading = SPI.transfer(0xff) << 8;  
  temp_reading += SPI.transfer(0xff);  

  digitalWrite(CS_TEMP,HIGH); // Set MAX7765 /CS High
  delay(1);
  
  // check result
  if(bitRead(temp_reading,2) == 1) // No Connection
    {
      return(-1); // Failed / NC Error
    }
    else
    {
      return((int)(temp_reading >> 5)); //Convert to Degc
    }
}

//-------------------------
//-------------------------
void setup()
{
  Serial.begin(9600);
  pinMode(10,OUTPUT); // Hardware /SS, need to be output

  pinMode(CS_TEMP,OUTPUT); // MAX6675/6674 /CS Line
  pinMode(MUX_EN,OUTPUT); // Enable pin on ADG608
  pinMode(MUX_A0,OUTPUT); // A0 on ADG608
  pinMode(MUX_A1,OUTPUT); // A1 on ADG608
  pinMode(MUX_A2,OUTPUT); // A2 on ADG608

  digitalWrite(CS_TEMP,HIGH); // Set MAX7765 /CS High
  digitalWrite(MUX_EN,HIGH); // Enable on

  SPI.begin(); // Init SPI
  
  Set_Mux_Channel(1);
  Read_Temperature();
}

//-------------------------
//-------------------------
void loop()
{
int temperature = 0;

while(1)
  {
    delay(1000);
    Set_Mux_Channel(1);
    temperature = Read_Temperature();
    if(temperature == -1)
      {
        Serial.println("N/C");
      }
      else
      {
        Serial.print(temperature,DEC);
        Serial.println(" DegC");
      }
  }
}

Just wondering how to go about getting this working on an arduino mega that has a sparkfun microsd shield attached. The sd shield uses digital pin 8 as CS. I have enabled software SPI to get the sd shield working on the mega (i.e. not using  digital pins 51-54 but rather 10-13 - like the deumi)

From my research on these forums it appears you can have multiple SPI devices provided you use different CS pins?

Also, I have analog inputs 0 to 9 being used already, is this going to be a problem?
« Last Edit: July 21, 2011, 10:37:14 pm by VroomVroom » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 128
Posts: 8524
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I always thought it was not a good idea to run t-couples through a MUX, still if it works it works.

Code:
while(1)
  {
    delay(1000);
    Set_Mux_Channel(1); <<<<<<<<<<<<<< should be a variable, not 1

You never select the other channels. Also you don't need the while(1) but it doesn't do any harm.

Quote
you can have multiple SPI devices provided you use different CS pins?
That's normally true, but I'm not familiar with the SPI library so don't know how it does the CS stuff.

______
Rob
« Last Edit: July 22, 2011, 01:07:34 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: