cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with SPI and LIS3LV02DQ

leone1386
Associate II
Posted on April 26, 2013 at 23:57

Hello,

I'm astudent of computer engineering, I'm trying to interface a STM32F0_Discoverycard with an LIS3LV02DQ accelerometer using the SPI protocol, but I'm havingtrouble with the initialization of the pins of the STM32F0_Discovery related tothe SPI protocol. Below I attach the code I used

void

SPI2_Init(

void

)

{

     

//SPI & GPIO struct

     

SPI_InitTypeDef

SPI_InitStructure;

     

GPIO_InitTypeDef

GPIO_InitStructure;

      RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2,

ENABLE

);

      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB| RCC_AHBPeriph_GPIOD,

ENABLE

);

     

//MISO, MOSI,CLOCK

      GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_13|GPIO_Pin_14 |GPIO_Pin_15;

      GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_AF

;

 

GPIO_InitStructure.

GPIO_OType

=

GPIO_OType_PP

;

 

GPIO_InitStructure.

GPIO_PuPd

=

GPIO_PuPd_DOWN

;

 

GPIO_InitStructure.

GPIO_Speed

=GPIO_Speed_50MHz;   

      GPIO_Init(GPIOB,&GPIO_InitStructure);

      GPIO_PinAFConfig(GPIOB,GPIO_PinSource13, GPIO_AF_0);

      GPIO_PinAFConfig(GPIOB,GPIO_PinSource14, GPIO_AF_0);

      GPIO_PinAFConfig(GPIOB,GPIO_PinSource15, GPIO_AF_0);

     

//CS

      GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_12;

      GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_OUT

;

      GPIO_InitStructure.

GPIO_OType

=

GPIO_OType_PP

;

 

GPIO_InitStructure.

GPIO_PuPd

=

GPIO_PuPd_UP

;

 

GPIO_InitStructure.

GPIO_Speed

=GPIO_Speed_50MHz;

      GPIO_Init(GPIOB,&GPIO_InitStructure);

     

//SPI2configuration

      SPI_I2S_DeInit(SPI2);

      SPI_StructInit(&SPI_InitStructure);

      SPI_InitStructure.

SPI_BaudRatePrescaler

=SPI_BaudRatePrescaler_2;

      SPI_InitStructure.

SPI_CPOL

= SPI_CPOL_Low;

      SPI_InitStructure.

SPI_CPHA

=SPI_CPHA_2Edge;

      SPI_InitStructure.

SPI_CRCPolynomial

= 7;

      SPI_InitStructure.

SPI_DataSize

=SPI_DataSize_8b;

      

SPI_InitStructure.

SPI_Direction

=SPI_Direction_2Lines_FullDuplex;

      SPI_InitStructure.

SPI_FirstBit

=SPI_FirstBit_MSB;

 

SPI_InitStructure.

SPI_Mode

= SPI_Mode_Master;

 

SPI_InitStructure.

SPI_NSS

= SPI_NSS_Soft;                 

     SPI_Init(SPI2,&SPI_InitStructure);

     SPI_Cmd(SPI2,

ENABLE

);

}

Debuggingstep by step I noted that the fields of the struct SPI_InitStructure andGPIO_InitStructure are properly modified according to the values

​​

that I specified. The values

​​

contained in the registers GPIOB are changed

when the GPIO_Init function is performed , butthe registers of SPI2 are not affected when the SPI_Init function is invoked .I would ask if I am making mistakes in the settings or anything.

I also havea doubt on the SPI read Protocol of the accelerometer. In fact I would like toread the WHO_AM_I register, and in order to do this as first thing, I send abyte specifying the read command and the address of the register from which Iwant to read using the SPI_SendData8 function and then run the reading usingthe SPI_ReceiveData8 function. I would ask you if the procedure is correct.Here's the code I used.

uint8_t

who=NULL;

uint8_t

reg_send;

reg_send = 0x0F;  //WHO_AM_Iregister address

reg_send |= 128;

while

(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==

RESET

);

SPI_SendData8(SPI2,reg_send);

while

(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==

RESET

);

who=SPI_ReceiveData8(SPI2);

I want to informyou that I use the Atollic  IDE.

Thanks inadvance for the help that you will give me.

Bestregards.

0 REPLIES 0