8051 ADXL SPI interfacing

8051cspi

I am trying to interface a ADXL345 to 8051 – AT89C51RC2 through SPI interface.

I have used the below code taken from here:

 include "Init.h"

 define SS_ADXL P1_0;

 void SPI_interr_routine(void);

 char serial_data;

 char data_example;

 char data_save;

 char transmit_completed;


void main(void)
{

    // Insert code
    Hardware_init();
    transmit_completed = 0;
    data_example=0x55;
    data_save = 0;
    printf("\n\r1111111111111");
    SPCON |= 0x10; /* Master mode */
    P1_1=1; /* enable master */
    SPCON |= 0x82; /* Fclk Periph/128 */
    SPCON &= ~0x08; /* CPOL=0; transmit mode example */
    SPCON |= 0x04; /* CPHA=1; transmit mode example */
    IEN1 |= 0x04; /* enable spi interrupt */
    SPCON |= 0x40; /* run spi */
    P1_1 = 0;
    EA=1; /* enable interrupts */
    //while(1) /* endless */
    //{
        SS_ADXL = 0;
        SPDAT= (0x32 | 0x80); /* send an example data */ //
        while(!transmit_completed);/* wait end of transmition */
        transmit_completed = 0; /* clear software transfert flag */

        SPDAT=0x00; /* data is send to generate SCK signal */
        while(!transmit_completed);/* wait end of transmition */
        transmit_completed = 0; /* clear software transfert flag */
        data_save = serial_data; /* save receive data */
        SS_ADXL = 1;
    //}

    printf("\n\r%c data_save = " , data_save);
}


void it_SPI(void) __interrupt (9) 
{

   SPI_interr_routine();

   return;

}

void SPI_interr_routine(void)
{

    if((SPSTA & 0x80) == 1)
    {
        serial_data = SPDAT;
        transmit_completed=1;
    }

    return;
}

Hardware Connections :

I have used this logic level converter to connect the 8051 to ADXL345.

P1.1 (SS) pin of 8051 is unconnected and I am enabling it in software by setting P1.1 to 1.

The CS pin of ADXL345 is connected to a GPIO of 8051 and I am selecting the slave (ADXL345) by setting the GPIO pin to low before writing / reading and then setting it high.
I am using the inbuilt SPI module in 8051.

When I run the above code, the SPIF bit is not getting set.

please let me know if anything is wrong with the hardware connection or the code.

Thanks

Best Answer

The ADXL345 Datasheet Rev E page 15 states "the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1". Make absolutely sure none of the timing limitations are violated. This chip in particular must follow the minimum and maximum timings shown on page 17. A digital-storage oscilloscope (preferred) or logic analyzer may be very helpful for troubleshooting.