cancel
Showing results for 
Search instead for 
Did you mean: 

how to interface adxl345 in stm32?

Sm.16
Associate II

Im currently working on stm32f103r8. Now i need to interface the adx345 through SPI in stm32 also i coded for this,issue im getting here is when the simulation is running the SS is not getting low it is always in high state.please notify my mistakes and correct it.

The following is my program,

***********\

#include "stm32f10x.h"

#define BufferSize 7

uint8_t buffer [BufferSize];

unsigned int a=0;

SPI_InitTypeDef SPI_Structure;

int main()

{

// RCC->APB2ENR =0x00001005;  //spi clock enabled and gpioa port and afioa enabled

RCC->APB2ENR |= RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_SPI1EN; //spi clock enabled and gpioa port and afioa enabled

GPIOA->CRL =0x00000000; // all bits of the a ports are cleared

/* spi bus no 1 is used ,so the port A  pins PA4,PA5,PA6,PA7 are used for communincation

 PA4 - used as chip select (CS)     alternate function push pull

 PA5 - used as SCLK           alternate function push pull

 PA6 - used as MISO           INPUT PULLUP

 PA7 - USED as MOSI           alternate function push pull

*/

GPIOA->CRL |= GPIO_CRL_CNF7_1 | GPIO_CRL_CNF6_1 | GPIO_CRL_CNF5_1 |GPIO_CRL_CNF4_1 ; //config bits for 4,5,6,7

GPIOA->CRL |= GPIO_CRL_MODE7_1 | GPIO_CRL_MODE7_0 | GPIO_CRL_MODE5_1 | GPIO_CRL_MODE5_0 | GPIO_CRL_MODE4_1 | GPIO_CRL_MODE4_0; //MODE bits for 4,5,6,7

 SPI_Structure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

 SPI_Structure.SPI_Mode = SPI_Mode_Master;

 SPI_Structure.SPI_DataSize = SPI_DataSize_8b;

 SPI_Structure.SPI_CPOL = SPI_CPOL_High;

 SPI_Structure.SPI_CPHA = SPI_CPHA_2Edge;

 SPI_Structure.SPI_NSS = SPI_NSS_Soft;

 SPI_Structure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

 SPI_Structure.SPI_FirstBit = SPI_FirstBit_MSB;

 SPI_Structure.SPI_CRCPolynomial = 7;

 SPI1->CR2 |=0x0004;

 SPI_Init(SPI1, &SPI_Structure);  //SPI initiated by SPI_Init func

 SPI_CalculateCRC(SPI1, ENABLE);

 SPI_Cmd(SPI1, ENABLE);

 // SPI_I2S_SendData(SPI1,i);

  // buffer[a++] = SPI_I2S_ReceiveData(SPI1);

 while(1)

 {

  SPI_I2S_SendData(SPI1,0x2D);

  for(int i=0;i<=10000;i++);

  // SPI_TransmitCRC(SPI1);

}

 //return 0;

}

2 REPLIES 2
S.Ma
Principal

Did you try to use a cube SPI Nucleo board example to make your code? If not, why?

Sm.16
Associate II

no! i have no idea about that. i will try that and come back.

thank you.