Interface to LTC1298 Serial 12 bit ADC

Hi all,

This is a simple program that I created to read an LTC1298 12 bit ADC. I am using it to read an MPX4115 pressure sensor. Here is the code:

/*
/////////////////////////////////////////////////////////////////////////////////////////////

LTC1298 - Arduino interface
Matt Sargent
October 2009

This example is essentially a port of the code that is contained in the 
Peter Anderson article. I believe the code was written by Towanda Malone, Baltimore, MD in Apr, '99.
Here is a link to the article:
http://www.phanderson.com/PIC/PICC/CCS_PCM/1298.html

I updated the code to work with the Arduino. I am using the default board, so I did not make any
adjustments for the speed at which the conversions are being made. I do take 20 samples and delay 1 ms 
between each and then average them. I have added an error amount that I have found is needed to get my
barometer to match the local one at the Naval Air Station.

/////////////////////////////////////////////////////////////////////////////////////////////

Connections:
The LTC1298 is connected thus:

LTC1298  |  Arduino Pin (not ATMEGA328)
---------------------------------------
CLK  (7) |   Pin 2
Din  (5) |   Pin 3  Note same pin as below
Dout (6) |   Pin 3  Note same pin as above
!CS  (5) |   Pin 4

/////////////////////////////////////////////////////////////////////////////////////////////
Data sheets:
http://cds.linear.com/docs/Datasheet/128698fs.pdf

References:
http://www.phanderson.com/PIC/PICC/CCS_PCM/1298.html

/////////////////////////////////////////////////////////////////////////////////////////////
*/

const int CS_PIN=4;
const int Dinout_PIN=3; //Din and Dout are both connected to pin 3...
const int CLK_PIN=2;

/////////////////////////////////////////////////////////////////////////////////////////////
void setup(void)
{
  //Set output pins to output
  pinMode(CS_PIN, OUTPUT);
  pinMode(CLK_PIN, OUTPUT);

  //Setup this pin, though it changes from input to output and vice versa
  pinMode(Dinout_PIN, OUTPUT);    

  // start serial port
  Serial.begin(9600);
}
/////////////////////////////////////////////////////////////////////////////////////////////

float samples;
float analogValue;
float adjustment = 1.0;
float P; //pressure in millibars
float temp;

void loop(void) {

   samples = 0.0;
   for (int i = 0; i < 20; i++)
   {
     temp = ReadFromLTC1298(0x0d); //single ended Ch 0 
     samples = samples + temp; 
     delay(1);
   }
   
   //now average the 20 samples
   analogValue = samples / 20.0;
   
   P = analogValue * 0.272 + 105.5 + adjustment;   //For 12 bit ADC
    
   // print the result:
   Serial.print("P(avg)=");
   Serial.print(P);
   P = P * 0.0295301;  //convert to in Hg...
   Serial.print("; in Hg = ");
   Serial.print(P);
   Serial.print("  Analog Value=");
   Serial.println(analogValue);

   delay(500); //wait a 1/2 second so the screen does not fly by...
}

int ReadFromLTC1298(int command)
{
  int aBit;
  int n;
  int result = 0x0000;

  //Set data pin for output so we can send the command
  pinMode(Dinout_PIN, OUTPUT);    
 
  //make sure the chip is not selected
  digitalWrite(CS_PIN, HIGH);
  
  //Initialize the clock high
  digitalWrite(CLK_PIN, HIGH);

  //Select the chip (LTC1298 will reset)
  digitalWrite(CS_PIN, LOW);

  //write the command to the LTC1298
  for (n=0; n<4; n++)
  {
    aBit = (command >> (3-n)) & 0x01;
    digitalWrite(Dinout_PIN, aBit); //Most sig fig first

    //Pulse in the bit...
    digitalWrite(CLK_PIN, LOW);
    digitalWrite(CLK_PIN, HIGH);
  }

  //Change the Dinout to input for reading
  pinMode(Dinout_PIN, INPUT);
  
  //dummy clock pulse  
  digitalWrite(CLK_PIN, LOW);
  digitalWrite(CLK_PIN, HIGH);
  
  //Read in the 12 bits of data
  for (n=0; n<12; n++)
  {
    //Pulse the clock...
    digitalWrite(CLK_PIN, LOW);
    digitalWrite(CLK_PIN, HIGH);
    
    aBit = digitalRead(Dinout_PIN);
    result = (result << 1) | aBit;
  }
  
  digitalWrite(CS_PIN, HIGH); //De-Selected the chip
  return result;
}