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!
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!
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!