Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Trouble interfacing LPC2148 and Hitachi H48C accelerometer

Status
Not open for further replies.

aforadi

Newbie level 5
Joined
Aug 15, 2008
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,350
Hi People
The H48C has only one DIO pin. I cant figure out how to perform SPI comm. b/w LPC2148 and H48C since the LPC has MOSI and MISO.

I have tried connecting the MISO and DIO , CS of both and coded up.
But no help with that. I keep getting a mode fault with that. Any idea where I am going wrong?
 

This is what I understand
1. Select the accelerometer through CS.
2. Through a GPIO collect the output from the DIO

Then,
What signal do I give the clock? Is it decided my the sampling rate I desire(obviously less than equal to the acelerometers sampling rate)
Also, How do I extract the data which is coming in?
 

If you check page 15 of the MCP3204 datasheet there is a complete description for the serial communication of the ADC,
you have to study that to find the proper way to read it.
The clock will be pulsed manually from a pin H-L-H-L and for each change you will send/receive your data bit by bit.
I don't have a ready solution since i haven't used this before.

Alex
 

OK..So now what I am doing is shorting the MOSI and MISO wires and connecting them to the DIO of the accelerometer.Here is the code I am using

#define SPIF (1<<7)
#define LED_MSK 0x00FF0000

void Initialize()
{
/* Configure Pin Connect Block */
PINSEL0=0x5500;
/* Set pclk to same as cclk */
VPBDIV=0x1;
/* Set to highest speed for SPI at 12 MHz- > 1.5 MHz */
S0SPCCR=0x8;
/* Device selected as master */
S0SPCR=0xC24; //12 bits at a time
IOSET0 |= 0x80;
}

int main (void) {
int a;
IODIR1 = LED_MSK; /* LED's defined as Outputs */

Initialize();
while (1)
{

IOSET0 |= 0x80;
IOCLR0 |= 0x80;

S0SPDR = 0x060;
while(!(S0SPSR & SPIF))
{
IOPIN1 = (IOPIN1 & ~LED_MSK) | (1 << 16) ;
}
a = S0SPDR;
while(!(S0SPSR & SPIF))
{
IOPIN1 = (IOPIN1 & ~LED_MSK) | (2 << 16) ;
}
IOSET0 |= 0x80;
} /* Loop forever */
}

Could someone please point out my error?

---------- Post added at 17:42 ---------- Previous post was at 17:40 ----------

With that code I am still getting a mode fault and the code gets stuck at the 2nd SPIF check and the 2nd LED stays bright due to that
 

You are still using the SPI, this will not work, you have to define 3 pins and use then as CLOCK,DATA and CS.
then you will manually change the pin states to send each bit and the clock pulses.

I have found the following code in another forum, i think it was intended for arduino, you can change it for ARM, you should be able to understand the code.

for example this is a part of the code below, i have added comments
Code:
  pinMode(DIO_pin, OUTPUT);  	// set DIO_pin as output
  digitalWrite(CS_pin, LOW);        // set CS_pin to 0
  digitalWrite(CLK_pin, LOW);       // set CLK_pin to 0
  delayMicroseconds(1);             // delay
  digitalWrite(DIO_pin, HIGH);      // set DIO_pin to 1
  digitalWrite(CLK_pin, HIGH);      // set CLK_pin to 1
  delayMicroseconds(1);             // delay

This is the complete code
Code:
//// VARS
int CS_pin = 9;
int DIO_pin = 10;
int CLK_pin = 11;

byte tempLSB = 0;
byte tempMSB = 0;

int aX = 0;
int aY = 0;
int aZ = 0;

//// FUNCTIONS
void StartMassurement() {
  pinMode(DIO_pin, OUTPUT);
  digitalWrite(CS_pin, LOW);
  digitalWrite(CLK_pin, LOW);
  delayMicroseconds(1);
  digitalWrite(DIO_pin, HIGH);
  digitalWrite(CLK_pin, HIGH);
  delayMicroseconds(1);

  }

void ShiftOutNibble(byte DataOutNibble) {
  for(int i = 3; i >= 0; i--) { // i = 3 ... 2 ... 1 ... 0
    digitalWrite(CLK_pin, LOW);
    // set DIO first
    if ((DataOutNibble & (1 << i)) == (1 << i)) {  // DataOutNibble AND 1 x 2^i Equals 1 x 2^i ?
	digitalWrite(DIO_pin, HIGH);

	}
    else {
	digitalWrite(DIO_pin, LOW);

	}
    // with CLK rising edge the chip reads the DIO from arduino in
    digitalWrite(CLK_pin, HIGH);
    // data rate is f_clk 2.0 Mhz --> 0,5 micro seeconds
    delayMicroseconds(1); // :-) just nothing
  }

}

void SampleIt() {
  digitalWrite(CLK_pin, LOW);
  delayMicroseconds(1);
  digitalWrite(CLK_pin, HIGH);
  delayMicroseconds(1);

  pinMode(DIO_pin, INPUT);
  digitalWrite(CLK_pin, LOW);
  delayMicroseconds(1);
  digitalWrite(CLK_pin, HIGH);
  if (digitalRead(DIO_pin)== LOW) {
    // Blink LED because ok
    }
}

byte ShiftInNibble() {
  byte resultNibble;
  resultNibble = 0;

    for(int i = 3 ; i >= 0; i--) {
	// The chip Shift out results on falling CLK
	digitalWrite(CLK_pin, LOW);
	delayMicroseconds(1); // :-) just nothing
	if( digitalRead(DIO_pin) == HIGH) {
	  resultNibble += 1 << i;
	}
	else {
	  resultNibble += 0 << i;
	}
	digitalWrite(CLK_pin, HIGH);
    }
return resultNibble;
}

void EndMessurement() {
  digitalWrite(CS_pin, HIGH);
  digitalWrite(CLK_pin, HIGH);
}

int GetValue(byte Axis) { // x = B1000, y = 1001, z = B1010
  int Result = 0;
  StartMassurement();
  ShiftOutNibble(Axis);
  SampleIt();
  Result =  (ShiftInNibble() << 8) + (ShiftInNibble() << 4) + ShiftInNibble();
  EndMessurement();

  return Result;
  }

//// SETUP
void setup() {
  Serial.begin(57600);
  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(2048 - GetValue(B1000));
  Serial.print(" ");
  Serial.print(2048 - GetValue(B1001));
  Serial.print(" ");
  Serial.print(2048 - GetValue(B1010));


  Serial.println("");
}


This is where i found the code
**broken link removed**

Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top