This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC1114 SPI1 Help...

I am using LPC1114.

I want to interface LIS302DL Accelerometer with LPC1114. For that purpose I have chosen SPI.

I am not getting proper X,Y,Z values... I am getting default value 255 i.e ff

The following is my code,anyone please help me out....



#include "LPC11xx.h"
#include"system_LPC11xx.c"
#include"stdio.h"
#include"Retarget.c"

#define SystemCoreClock 12000000UL
#define FIFOSIZE                8

/* SSP Status register */
#define SSPSR_TFE               (1 << 0)
#define SSPSR_TNF               (1 << 1)
#define SSPSR_RNE               (1 << 2)
#define SSPSR_RFF               (1 << 3)
#define SSPSR_BSY               (1 << 4)

#define SPI_CS 0 // port 2

typedef unsigned char uc;
typedef unsigned int ui;

void UARTInit   (ui);
int  sendchar   (int ch);
int  getkey             (void);
void delay              (void);

void    SPI_Init        (void);
uc      SPIReceive      (uc);
void    SPISend         (uc,uc);

ui  RECV,regVal,X,Y,Z,i,who;

int main (void)
{
        SystemInit();   // for initializing clocks
        UARTInit(9600); // for serial & Printf communication
        SPI_Init();
        printf("Hi \n");
        printf("Testing SPI ...\n");

        SPISend(0x20,0x47);
        delay();

        while(who!=0x3B)
        {
        SPISend(0x20,0x47);
        who=SPIReceive(0x0f);
        printf("who_am_i=%x \n",who);
        delay();
        }
        while(1)
        {
                delay();
                X=SPIReceive(0x29);
                Y=SPIReceive(0x2B);
                Z=SPIReceive(0x2D);
                printf("X=%d Y=%d Z=%d \n",X,Y,Z);
        }
}


/*====================================SPI==============================================*/
void SPI_Init()
{
                uint8_t i, Dummy=Dummy;

                LPC_SYSCON->PRESETCTRL |= (1<<2);
                LPC_SYSCON->SYSAHBCLKCTRL |= (1<<18);  // clk for spi1
                LPC_SYSCON->SSP1CLKDIV = 0x02;               /* Divided by 2 */

                /*  SSP I/O config */
                LPC_IOCON->PIO2_3           &= ~0x07;
                LPC_IOCON->PIO2_3           |=  0x02;                /* SSP1 MOSI */
                LPC_IOCON->PIO2_2           &= ~0x07;
                LPC_IOCON->PIO2_2           |=  0x02;                /* SSP1 MISO */
                LPC_IOCON->PIO2_1           &= ~0x07;
                LPC_IOCON->PIO2_1           |=  0x02;                /* SSP1 CLK */
                LPC_IOCON->PIO2_0           &= ~0x07;
                LPC_IOCON->PIO2_0           |=  0x02;                /* SSP1 SSEL */

        /* SSPCPSR clock prescale register, master mode, minimum divisor is 0x02 */
                LPC_SSP1->CPSR = 0x2;

        /* Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 15 */
                LPC_SSP1->CR0 = 0x0707;

                for ( i = 0; i < FIFOSIZE; i++ )
                {
                Dummy = LPC_SSP1->DR;                /* clear the RxFIFO */
                }
                LPC_SSP1->CR1 |= 2;  /* Enable SSP */
                return;
}

void SPISend( uc Radd,uc Rval )
{
                uint8_t Dummy=Dummy;
                Radd&=0x7F;

                /* Move on only if NOT busy and TX FIFO not full. */
                while ( (LPC_SSP1->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF );

                LPC_SSP1->DR = Radd;
                while ( (LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );
                Dummy = LPC_SSP1->DR;

                while ( (LPC_SSP1->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF );

                LPC_SSP1->DR = Rval;
                while ( (LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );
                Dummy = LPC_SSP1->DR;
                return;
}

unsigned char SPIReceive(uc Radd)
{
        unsigned char SPIRECV;
        Radd|=0x80;

        //LPC_GPIO2->MASKED_ACCESS[(1<<SPI_CS)] = (0<<SPI_CS);   // cs=0
        LPC_SSP1->DR = Radd;
        while ( LPC_SSP1->SR & SSPSR_BSY );

        LPC_SSP1->DR = 0xFF;
        while ( (LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );

        SPIRECV = LPC_SSP1->DR;
        return SPIRECV;
}

/*======================================UART=============================================*/
void UARTInit(ui baudrate)
{
        LPC_IOCON->PIO1_6 &= ~0x07;                      /*  UART I/O config */
        LPC_IOCON->PIO1_6 |= 0x01;                           /* UART RXD */
        LPC_IOCON->PIO1_7 &= ~0x07;
        LPC_IOCON->PIO1_7 |= 0x01;                           /* UART TXD */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);  // UART CLK enable
        LPC_SYSCON->UARTCLKDIV = 0x01;               /* divided by 1 , like pre-scaler*/
        LPC_UART->LCR = 0x83;                        /* DLB=1, 8 bits, no Parity, 1 Stop bit */
                /*baud rate */
        LPC_UART->DLM = ((SystemCoreClock)/(16*baudrate))/(256);
        LPC_UART->DLL = ((SystemCoreClock)/(16*baudrate))%(256);// Lower byte
        LPC_UART->LCR = 0x03;        /* DLAB = 0 ,Disable access to divisor latches*/
        LPC_UART->FCR = 0x07;        /* Enable and reset TX and RX FIFO. */
        regVal = LPC_UART->LSR;      /* Read to clear the line status. */
        return;
}


int sendchar(int ch)
{
        LPC_UART->THR = ch;
        while ( !(LPC_UART->LSR & 0x40) ); //wait until TEMT=0,i.e. U0THR contains valid data
        return 0;
}

int getkey()
{
        while(!(LPC_UART->LSR & (0x01)));
        RECV=LPC_UART->RBR;
        return RECV;
}

/*=============================================================================================*/

void delay()
{
        int i;
        for(i=0;i<=1005535;i++);
}

Thanks in advance.

  • Where are your information about what debugging you have done? You have done some attempts to figure out what problems you may have? I.e. normal debugging - a significant part of software and hardware development...

    For example - have you verified that the chip gets a slave select signal? Without any slave select, it will not drive the MISO signal, which means you will not receive any data. With a weak pull-up on the signal, you will then sample 0xff.

  • Hi Per Westermark,

    Thanks for your response,As u said Chip Selection is not done properly.Now I got it..

    Thanks
    Yash k