-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS3DSH.c
102 lines (84 loc) · 2.25 KB
/
LIS3DSH.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*!
* \author no1wudi
* \file LIS3DSH.c
*/
#include "LIS3DSH.h"
/*!
* \brief Youc can edit your own init process here.
*/
void LIS3DSH_Init(void) {
LIS3DSH_Init_SPI_Bus();
}
/*!
* \brief Read the content of the register
* \param LIS3DSH_Reg The address of the object register.
*/
unsigned char LIS3DSH_Read(unsigned char LIS3DSH_Reg) {
unsigned char Temp;
CS_L;
LIS3DSH_SPI_Read_Write(LIS3DSH_Reg | LIS3DSH_Read_Mask);
Temp = LIS3DSH_SPI_Read_Write(0xAA);
CS_H;
return Temp;
}
/*!
*\brief Write data to the register.
*\param LIS3DSH_Reg The object register.
*\param Data The data will be written.
*/
void LIS3DSH_Write(unsigned char LIS3DSH_Reg, unsigned char Data) {
CS_L;
LIS3DSH_SPI_Read_Write(LIS3DSH_Reg | LIS3DSH_Write_Mask);
LIS3DSH_SPI_Read_Write(Data);
CS_H;
}
/*!
* \brief Set the output status.
* \param LIS3DSH_Output The setting of the output.
*/
void LIS3DSH_Set_Output(unsigned char LIS3DSH_Output) {
LIS3DSH_Write(LIS3DSH_Reg_Ctrl_4, LIS3DSH_Output);
}
/*!
* \brief Set The output feature.
* \param LIS3DSH_Output_Feature The feature of output.
*/
void LIS3DSH_Set_Output_Feature(unsigned char LIS3DSH_Output_Feature) {
LIS3DSH_Write(LIS3DSH_Reg_Ctrl_5, LIS3DSH_Output_Feature);
}
/*!
* \brief Get the output value of X-axis.
* \param LIS3DSH_Sense Sensitivity of the sensor.
* \return The value of x-axis
*/
float LIS3DSH_Get_X_Out(float LIS3DSH_Sense) {
short Temp;
Temp = LIS3DSH_Read(LIS3DSH_Reg_X_Out_H);
Temp = Temp << 8;
Temp = Temp + LIS3DSH_Read(LIS3DSH_Reg_X_Out_L);
return (float) Temp * LIS3DSH_Sense / 1000;
}
/*!
* \brief Get the output value of y-axis.
* \param LIS3DSH_Sense Sensitivity of the sensor.
* \return The value of y-axis
*/
float LIS3DSH_Get_Y_Out(float LIS3DSH_Sense) {
short Temp;
Temp = LIS3DSH_Read(LIS3DSH_Reg_Y_Out_H);
Temp = Temp << 8;
Temp = Temp + LIS3DSH_Read(LIS3DSH_Reg_Y_Out_L);
return (float) Temp * LIS3DSH_Sense / 1000;
}
/*!
* \brief Get the output value of Z-axis.
* \param LIS3DSH_Sense Sensitivity of the sensor.
* \return The value of Z-axis
*/
float LIS3DSH_Get_Z_Out(float LIS3DSH_Sense) {
short Temp;
Temp = LIS3DSH_Read(LIS3DSH_Reg_Z_Out_H);
Temp = Temp << 8;
Temp = Temp + LIS3DSH_Read(LIS3DSH_Reg_Z_Out_L);
return (float) Temp * LIS3DSH_Sense / 1000;
}