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.

[SOLVED] Problem interfacing ADXL345 with PIC18F4550 SPI

Status
Not open for further replies.

mehtadhaval

Member level 3
Joined
Sep 15, 2010
Messages
56
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Activity points
1,616
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:
#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.
 

Attachments

  • Read 0x80.JPG
    Read 0x80.JPG
    153.9 KB · Views: 104
  • Read 0xAC Again At 2V.JPG
    Read 0xAC Again At 2V.JPG
    152.4 KB · Views: 102

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top