18F4685 with accelerometer via I2C - Help!!

Status
Not open for further replies.

Tipp

New Member
This is driving me crazy! I'm trying to talk to a triple-axis accelerometer with I2C, but nothing I do seems to work! I'm using the C18 compiler, and an ST accelerometer (Datasheet here: https://www.st.com/stonline/products/literature/ds/12094.pdf ).

Here's my code. I know something must be wrong, because it always returns a value of 255. Any help or suggestions as to what I'm missing would be appreciated!

Code:
/*

Reads data from an I2C accelerometer. 

Slave Device Address: 0011101

*/  	

	#include	<p18F4685.h>
	#include	<delays.h>
	#include	<usart.h>
	#include	<i2c.h>
	#include	<stdio.h>
	#pragma	config	OSC		=	HSPLL		/*Sets the config bits*/
	#pragma	config	WDT		=	OFF
	#pragma	config	DEBUG	=	OFF
	#pragma	config	MCLRE	=	ON	
	#pragma	config	PBADEN	=	OFF

	void	main(void)
	{

	unsigned	int		lsb;
	unsigned	int	        msb;

	TRISC	=	0x0;
	LATC	        =	0x0;

		OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF	/*Turns the Tx and Rx interupts off*/
				 & USART_ASYNCH_MODE & USART_EIGHT_BIT 	/*Sets up for a-sync and 8-bit data mode*/
				 & USART_BRGH_HIGH,	104);				/*Baud mode "High"*/
		
		OpenI2C(MASTER, SLEW_OFF);
		SSPADD	=	9;

						Delay10KTCYx(225);
						
Start:
		StartI2C();
			IdleI2C();
		WriteI2C(0b00111010);
			IdleI2C();
		WriteI2C(0b0101000);
			IdleI2C();
		RestartI2C();
			IdleI2C();
		WriteI2C(0b00111001);
			IdleI2C();
		lsb	=	ReadI2C();
		NotAckI2C();
			IdleI2C();
		StopI2C();

	printf(" Data:\r\n");
	printf("%d\r\n", lsb);
	printf(" \r\n");

						Delay10KTCYx(225);
						
		goto	Start;


}

I also have another question. The data from the accelerometer for each axis is represented in the form of LSB and MSB. How can I derive a final displayable value from those? I'm still fairly new to C.

Thank you!
 
Your second write to the I2C should be...

WriteI2C(0b00111011);

and then you should do the read. The first 7 bits should remain the same indicating the device and the last bit indicates a read or write.

I think that should do it.

Craig
 
Making progress, I think. It's now returning 0's instead of 255's.

Am I expecting the right kind of data? I have lsb defined as an unsigned int, but should it be an unsigned char, or something else? I'm going to tinker around with that right now. Any further suggestions or pointers are welcome
 
I have lsb defined as an unsigned int, but should it be an unsigned char, or something else?

unsigned char would be better as I think the unsigned int takes up 4 bytes of memory where as the unsigned char is just a single byte which is what is being returned. Still thats just a memory issue - I don't think that would cause the problem with a 0 returning sorry.
 
Whats the speed of your internal OSC? The accelerometer only goes at 100-400 Khz max. Doesn't the HSPLL ramp up the internal OSC to greater than 8Mhz? Check that the SCL speed is less than 400Khz.

Oh and also doesn't the accelerometer measure change in acceleration as well? So you will need to wiggle the thing about to get a reading wouldn't you?
 
Last edited:
The PIC is running with a 4MHz crystal in a PLL, so the running frequency is 16MHz. I've set the I2C freq to 400kHz.

Would trying to run the PIC at a lower freq be worthwhile?

EDIT: Yes, I've been wiggling it around, applying movement to it so that the microcontroller would have different values to display, but I've never observed any changing values.
 
Last edited:
Another important thought... I'm doing this on breadboard. I'm aware breadboards have nasty parasitic capacitance, and I'm also aware that the I2C bus is sensitive to capacitance. Do you think the capacitance in a breadboard would be enough to degrade the signal to an unusable point?
 
Did you notice the Power Down bits?

From the data sheet,
PD1, PD0 bit allows to turn the device out of power-down mode. The device is in powerdown mode when PD1, PD0= “00” (default value after boot). The device is in normal mode when either PD1 or PD0 is set to 1.

HTH

Mike.
 
I must have glanced right over that x.x

Thanks for the tip! Sometimes you just need a kick in the pants to realize something obvious!
 
Yay! I got it! She's up and running, and I'm getting logical values!

Thank you so much guys, I truly appreciate the help from both of you!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…