Home > other >  Lis3dh acceleration sensor
Lis3dh acceleration sensor

Time:11-27


I use acceleration sensor development board test, tests found that SPI can only send data, receive data, there should be no and lis3dh acceleration sensors connected, which a great god help me have a look at the code what's the problem?
Int main (void)
{
AxesRaw_t data;
U8 lisid;
SystemInit ();
Delay_init (168);//initialization time delay function
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Uart_init (115200);//serial port initialization
IO_Init ();
LED_Init ();
SPI1_FLASH_Init ();//SPI initialization
Lis3dhinit ();//initialization three-axis accelerometer
//LIS3DH_GetWHO_AM_I (& amp; Lisid);//for three-axis accelerometer address
While (1)
{

LIS3DH_GetAccAxesRaw (& amp; The data);//get a three axis data
Printf (" accel: [x] % DMG, DMG % [y], [z] DMG % \ r \ n ",
(int) ((float) data. 4/65536 AXIS_X * * 1000),
(int) ((float) data. 4/65536 AXIS_Y * * 1000),
(int) ((float) data. 4/65536 AXIS_Z * * 1000));
delay_ms(1000);//delay
}

}

Void SPI1_FLASH_Init (void)
{

GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;


RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE);//can make GPIOB clock
RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);//can make SPI1 clock

//GPIOFB3, 4, 5 initialization setting
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;//PB3 ~ 5 multiplexing function output
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF;//multiplexing function
GPIO_InitStructure. GPIO_OType=GPIO_OType_PP;//push-pull output
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_100MHz;//100 MHZ
GPIO_InitStructure. GPIO_PuPd=GPIO_PuPd_UP;//pull
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//initialize

GPIO_PinAFConfig (GPIOB, GPIO_PinSource3 GPIO_AF_SPI1);//PB3 reuse for SPI1
GPIO_PinAFConfig (GPIOB, GPIO_PinSource4 GPIO_AF_SPI1);//PB4 reuse for SPI1
GPIO_PinAFConfig (GPIOB, GPIO_PinSource5 GPIO_AF_SPI1);//PB5 reuse for SPI1

//here only for SPI mouth initialization
RCC_APB2PeriphResetCmd (RCC_APB2Periph_SPI1, ENABLE);//reset SPI1
RCC_APB2PeriphResetCmd (RCC_APB2Periph_SPI1, DISABLE);//stop reset SPI1

SPI_InitStructure. SPI_Direction=SPI_Direction_2Lines_FullDuplex;//set the SPI one-way or two-way data model: the SPI is set to double bidirectional full-duplex
SPI_InitStructure. SPI_Mode=SPI_Mode_Master;//set the SPI work mode: set give priority to SPI
SPI_InitStructure. SPI_DataSize=SPI_DataSize_8b;//set the SPI data size: SPI to send and receive eight frame structure
SPI_InitStructure. SPI_CPOL=SPI_CPOL_High;//the idle state of serial synchronous clock for the high level
SPI_InitStructure. SPI_CPHA=SPI_CPHA_2Edge;//the first jump of serial synchronous clock along (up or down) data by sampling
SPI_InitStructure. SPI_NSS=SPI_NSS_Soft;//NSS signal by the hardware (NSS pin) or software (use SSI) management: internal NSS signal with SSI control
SPI_InitStructure. SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;//define the baud rate of the preassigned frequency value: baud rate preassigned frequency value of 256
SPI_InitStructure. SPI_FirstBit=SPI_FirstBit_MSB;//specified data transmission from the MSB position or LSB: data transmission from the MSB start
SPI_InitStructure. SPI_CRCPolynomial=7;//CRC value calculated polynomial
SPI_Init (SPI1, & amp; SPI_InitStructure);//set according to the specified parameters in SPI_InitStruct initial non-greeks SPIx register

SPI_Cmd (SPI1, ENABLE);//can make SPI peripheral
LIS3DH_CS=1;//SPI FLASH uncheck
SPI1_ReadWriteByte (0 XFF);//start transmission, can don't
//SPI1_SetSpeed (SPI_BaudRatePrescaler_4);
}
//SPI1 speed setting function
//SPI speed=fAPB2/frequency division coefficient
//@ ref SPI_BaudRate_Prescaler: SPI_BaudRatePrescaler_2 ~ SPI_BaudRatePrescaler_256
//fAPB2 clock is 84 MHZ commonly:
Void SPI1_SetSpeed (u8 SPI_BaudRatePrescaler)
{
The assert_param (IS_SPI_BAUDRATE_PRESCALER (SPI_BaudRatePrescaler));//determine validity
SPI1 - & gt; CR1 & amp;=0 xffc7;//a 3-5 reset, which is used to set the baud rate
SPI1 - & gt; CR1 |=SPI_BaudRatePrescaler;//set the SPI1 speed
SPI_Cmd (SPI1, ENABLE);//can make SPI1
}
//a byte
SPI1, speaking, reading and writing//TxData: to write bytes
//the return value: read bytes
U8 SPI1_ReadWriteByte (u8 TxData)
{

While (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_TXE)==RESET) {}//area waiting to send an empty

SPI_I2S_SendData (SPI1, TxData);//sent by a peripheral SPIx a byte data

While (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_RXNE)==RESET) {}//waits to receive a byte

Return SPI_I2S_ReceiveData (SPI1);//return through SPIx recently received data

}

Void IO_Init ()
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE);//can make GPIOB clock
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOG, ENABLE);//can make GPIOG clock

//GPIOB14
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_14;//PB14
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;//output
GPIO_InitStructure. GPIO_OType=GPIO_OType_PP;//push-pull output
GPIO_InitStructure. GPIO_Speed=GPIO_Speed_100MHz;//100 MHZ
GPIO_InitStructure. GPIO_PuPd=GPIO_PuPd_UP;//pull
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//initialize

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_7;//PG7
GPIO_Init (GPIOG, & amp; GPIO_InitStructure);//initialize

GPIO_SetBits (GPIOG, GPIO_Pin_7);//PG7 output 1, to prevent the NRF interfere with SPI FLASH communication




}

Void lis3dhinit (void)
{

U8 response=0;
The response=LIS3DH_SetODR (LIS3DH_ODR_100Hz);//set the output frequency
//printf (" set the ODR: % d \ r \ n ", response);
The response=LIS3DH_SetMode (LIS3DH_NORMAL);//set the work mode
//printf (" set MODE: % d \ r \ n ", response);
The response=LIS3DH_SetFullScale (LIS3DH_FULLSCALE_2);
//printf (" set SCALE: % d \ r \ n ", response);
The response=LIS3DH_SetAxis (LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);//set three axis can make
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related