Hitachi H48C

Anyone ever get the Hitachi H48C working with Arduino? I've read all the docs and old posts but can't seem to get things working and Yes I looked at the parallax website as I orginally used it with the BS2, and had it working. :slight_smile:

I am getting values and the numbers do change but not the right scale/they don't make sense.

int CS_pin = 9;
int CLK_pin = 10;
int DIO_pin = 11;

int aX  = 0;
int aY  = 1;
int aZ  = 2; 
int Vref = 3;
int counter = 0;
//byte axis = 0;


word GetValue(int axis) { // x = B11000, y = B11001, z = B11010 Vref = B11011
  word Result = 0;

  pinMode(DIO_pin,OUTPUT);
  pinMode(CLK_pin,OUTPUT);
  digitalWrite(CS_pin, LOW);
 
  shiftOut(DIO_pin,CLK_pin,MSBFIRST,0X18+axis);
 //now get the input
  pinMode(DIO_pin,INPUT);
  //Serial.print("\n");
  Result=shiftIn(DIO_pin,CLK_pin,MSBFIRST);
  Result <<=8;
  Result |= shiftIn(DIO_pin,CLK_pin,MSBFIRST);
  //Serial.print("&&&&&&&&&&&&&&&&&  RESULT=");Serial.print(Result,BIN);Serial.print(" &&&&&&&&&&&&&&\n");
  digitalWrite(CS_pin, HIGH);
  digitalWrite(CLK_pin, HIGH);
  return Result;
  }

//// SETUP
void setup() {
  Serial.begin(4800);
  pinMode(CS_pin, OUTPUT);
  pinMode(CLK_pin, OUTPUT);
  pinMode(DIO_pin, OUTPUT);
  // initialize device & reset
  digitalWrite(CS_pin,LOW);
  digitalWrite(CLK_pin,LOW);
  delayMicroseconds(1);
  digitalWrite(CS_pin, HIGH);
  digitalWrite(CLK_pin,HIGH);
}


//// LOOP
void loop() {
  
  Serial.print("\n\n\n**************************Counter = ");
  Serial.print(counter);
  Serial.print("\n"); 
  Vref = GetValue(B11011);
  aX = GetValue(B11000);
  Serial.print("Xforce=");
  if(aX >= Vref) {
    Serial.print((aX-Vref)*.22);
  } else {
    Serial.print(-(Vref-aX)*.22);
  }
  Serial.print(" aX=");Serial.print(aX);
  Serial.print("  Vref=");Serial.print(Vref);Serial.print("\n");
  Serial.print("\n");
  
  Vref = GetValue(B11011);
  aY = GetValue(B11001);
  Serial.print("Yforce=");
  if(aY >= Vref) {
    Serial.print((aY-Vref)*.22);
  } else {
    Serial.print(-(Vref-aY)*.22);
  }
  Serial.print(" aY=");Serial.print(aY);Serial.print("  Vref=");
  Serial.print(Vref);Serial.print("\n");Serial.print("\n");
  
  Vref = GetValue(B11011);
  aZ = GetValue(B11010);
  Serial.print("Zforce=");
  if(aZ >= Vref) {
    Serial.print((aZ-Vref)*.22);
  } else {
    Serial.print(-(Vref-aZ)*.22);
  }
   Serial.print(" aZ=");Serial.print(aZ);Serial.print("  Vref=");
   Serial.print(Vref);Serial.print("\n");Serial.print("\n");
  Vref = GetValue(B11011);

  counter += 1;
  //Serial.print("\n#####################");
  delay(10);  // loop every 10 times per sec.
}

I think I got it working, I tried using the shiftin/out logic but couldn't seem to get it working. So I wrote my own psuedo shiftin/out which seems to give me the right values(being at sea level the Zaxis should be close to 1). The code could possibly be cleaned/optimized a bit more but it's late.

myclocking.pde (4.57 KB)

MCP3204.pdf (598 KB)