cancel
Showing results for 
Search instead for 
Did you mean: 

LIS3DH library

SARTHAK KELAPURE
Associate II
Posted on February 12, 2018 at 11:18

Hello everyone,

Can someone help me with LIS3DH accelerometer library.

Thank you.

3 REPLIES 3
Posted on February 12, 2018 at 11:28

I use the LIS33DE but this one seems to work the same way:

-set right bits here: CTRL_REG1 (20h) - mode, enable x, y, z

-then you can read the STATUS_REG (27h) register and see if the ZYXDA bit is set

-then read the particular OUTxxxx register

I assumed that you can handle the I2C/SPI bus correctly. I use SPI.

Details:

http://www.st.com/content/ccc/resource/technical/document/datasheet/3c/ae/50/85/d6/b1/46/fe/CD00274221.pdf/files/CD00274221.pdf/jcr:content/translations/en.CD00274221.pdf

 
ab val
Associate III
Posted on February 12, 2018 at 16:03

Here is the library I use:

https://github.com/s54mtb/LEDS/blob/master/Src/lis3dh_driver.c

 
Posted on February 12, 2018 at 17:18

 ,

 ,

I use simplified code:

♯ define LIS35_WRITE 0

 ,

♯ define LIS35_READ  , ,  ,0x80

 ,

♯ define LIS35_ADDR_NO_INC 0

 ,

♯ define LIS35_ADDR_INC 0x40

♯ define LIS35_REG_OUTX  , ,  ,0x29

 ,

♯ define LIS35_REG_OUTY , ,  ,0x2B

 ,

♯ define LIS35_REG_OUTZ , ,  ,0x2D

♯ define LIS35_REG_CR1 0x20

 ,

♯ define LIS35_REG_CR1_XEN 0x1

 ,

♯ define LIS35_REG_CR1_YEN 0x2

 ,

♯ define LIS35_REG_CR1_ZEN 0x4

 ,

♯ define LIS35_REG_CR1_DR_400HZ 0x80

 ,

♯ define LIS35_REG_CR1_ACTIVE 0x40

 ,

♯ define LIS35_REG_CR1_FULL_SCALE 0x20

♯ define , ,  ,OUTX , ,  ,0X29

 ,

♯ define , ,  ,OUTY , ,  ,0X2B

 ,

♯ define , ,  ,OUTZ , ,  ,0X2D

 ,

 ,

 ,

♯ define , , , , ChipSelectLOW() , , , , HAL_GPIO_WritePin(LIS35DE_CS_GPIO_Port, LIS35DE_CS_Pin, GPIO_PIN_RESET),

 ,

♯ define , , , , ChipSelectHigh() , , , HAL_GPIO_WritePin(LIS35DE_CS_GPIO_Port, LIS35DE_CS_Pin, GPIO_PIN_SET),

uint8_t memsc, memsr, memss, memsX, memsY, memsZ,

uint8_t LIS35DE_ReadRegister(uint8_t addr)

 ,

{

 ,

 , ,  ,uint8_t address = LIS35_READ | LIS35_ADDR_NO_INC | addr,

 ,

 , ,  ,uint8_t data,

 ,

 , ,  ,

 ,

 , ,  ,ChipSelectLOW(),

 ,

 , ,  ,HAL_SPI_Transmit(&,hspi1, &,address, 1, 1000),

 ,

 , ,  ,HAL_SPI_Receive(&,hspi1, &,data, 1, 1000),

 ,

 , ,  ,ChipSelectHigh(),

 ,

 , ,  ,return data,

 ,

}

 ,

 ,

 ,

void LIS35DE_WriteRegister(uint8_t addr, uint8_t data)

 ,

{

 ,

 , ,  ,ChipSelectLOW(),

 ,

 , ,  ,uint8_t address = LIS35_WRITE | LIS35_ADDR_NO_INC | addr,

 ,

 , ,  ,HAL_SPI_Transmit(&,hspi1, &,address, 1, 1000),

 ,

 , ,  ,HAL_SPI_Transmit(&,hspi1, &,data, 1, 1000),

 ,

 , ,  ,ChipSelectHigh(),

 ,

}

void main(void)

 ,

{ , ,  ,

 ,

 , ,  ,// MEMs init , ,  , , ,  ,

 ,

 , ,  ,memsc=LIS35_REG_CR1_XEN | LIS35_REG_CR1_YEN | LIS35_REG_CR1_ZEN | LIS35_REG_CR1_ACTIVE,

 ,

 , ,  ,LIS35DE_WriteRegister(LIS35_REG_CR1, memsc), , ,  ,// Active mode, Enable X, Y, Z

 ,

 , ,  ,memsr = LIS35DE_ReadRegister(CTRL_REG1), , ,  ,// to verify if the MEMs was configured , ,  , , ,  ,

 ,

 , ,  ,

 ,

 , ,  ,MEMS reading

 ,

 , ,  ,// memss = LIS35DE_ReadRegister(STATUS_REG), , ,  ,// you can check if the data is ready

 ,

 , ,  ,

 ,

 , ,  ,memsX = LIS35DE_ReadRegister(LIS35_REG_OUTX),

 ,

 , ,  ,memsY = LIS35DE_ReadRegister(LIS35_REG_OUTY),

 ,

 , ,  ,memsZ = LIS35DE_ReadRegister(LIS35_REG_OUTZ),

 ,

}