mehtadhaval
Member level 3
I am trying to read data from ADXL3445 accelerometer using PIC18F4550. The ADXL345 supports both SPI and I2C interfaces. I am using SPI for my project.
The code I am using for reading and writing data to ADXL is as follows :
Code to initialize ADXL345 :
After initialization , I have put following code to read the device ID of ADXL which should give me 0xE5. The code is as follows :
I am getting 0xE5 as response. But if I write the same code to read any of the data registers (DATA[X-Z][0-1]), I am getting 0x00 as the response everytime. Same goes for each address except 0x00 (devId).
I have checked the SDO and SDI with SCLK on oscilloscope and I have attached the screenshot here. The first screenshot is of receiving 0xE5 and the second screenshot is when I am trying to read any other register.
The code I am using for reading and writing data to ADXL is as follows :
Code:
#define SPI_CS LATAbits.LATA5
unsigned char POWER_CTL = 0x2D; //Power Control Register
unsigned char DATA_FORMAT = 0x31;
Code to initialize ADXL345 :
Code:
TRISAbits.TRISA5 = 0;
TRISBbits.TRISB1 = 0;
TRISCbits.TRISC7 = 0;
SPI_CS = 1;
OpenSPI(SPI_FOSC_64, MODE_10, SMPEND);
SPI_CS = 0;
Delay100TCYx(1);
WriteSPI(DATA_FORMAT);
WriteSPI(0x01);
Delay100TCYx(1);
SPI_CS = 1;
Delay100TCYx(1);
SPI_CS = 0;
Delay100TCYx(1);
WriteSPI(POWER_CTL);
WriteSPI(0x08);
Delay100TCYx(1);
SPI_CS = 1;
Delay100TCYx(1);
After initialization , I have put following code to read the device ID of ADXL which should give me 0xE5. The code is as follows :
Code:
SPI_CS = 0;
Delay100TCYx(1);
WriteSPI(0x80 | 0x00); //0x00 is the address to read device ID
readByte = ReadSPI();
Delay100TCYx(1);
SPI_CS = 1;
Delay100TCYx(1);
I am getting 0xE5 as response. But if I write the same code to read any of the data registers (DATA[X-Z][0-1]), I am getting 0x00 as the response everytime. Same goes for each address except 0x00 (devId).
I have checked the SDO and SDI with SCLK on oscilloscope and I have attached the screenshot here. The first screenshot is of receiving 0xE5 and the second screenshot is when I am trying to read any other register.