1 |
/* |
1 |
/* |
2 |
MPL115A1 SDN - +3.3V (always power on) |
2 |
MPL115A1 SDN - +3.3V (always power on) |
3 |
MPL115A1 #CS - PIN_C2 |
3 |
MPL115A1 #CS - PIN_C2 |
4 |
MPL115A1 DOUT - PIN_C4 |
4 |
MPL115A1 DOUT - PIN_C4 |
5 |
MPL115A1 DIN - PIN_C5 |
5 |
MPL115A1 DIN - PIN_C5 |
6 |
MPL115A1 SCLK - PIN_C3 |
6 |
MPL115A1 SCLK - PIN_C3 |
7 |
MPL115A1 GND - GND |
7 |
MPL115A1 GND - GND |
8 |
MPL115A1 VDD - +3.3V |
8 |
MPL115A1 VDD - +3.3V |
9 |
*/ |
9 |
*/ |
10 |
|
10 |
|
11 |
|
11 |
|
12 |
// SPI mode definitions. |
12 |
// SPI mode definitions. |
13 |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) |
13 |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) |
14 |
#define SPI_MODE_1 (SPI_L_TO_H) |
14 |
#define SPI_MODE_1 (SPI_L_TO_H) |
15 |
#define SPI_MODE_2 (SPI_H_TO_L) |
15 |
#define SPI_MODE_2 (SPI_H_TO_L) |
16 |
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
16 |
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
17 |
|
17 |
|
18 |
float a0; // correctiaon coefficients |
18 |
float a0; // correctiaon coefficients |
19 |
float b1; |
19 |
float b1; |
20 |
float b2; |
20 |
float b2; |
21 |
float c12; |
21 |
float c12; |
22 |
|
22 |
|
23 |
|
23 |
|
24 |
void MPL_init() |
24 |
int1 MPL_init() |
25 |
{ |
25 |
{ |
26 |
unsigned int8 a0_MSB, a0_LSB; |
26 |
unsigned int8 a0_MSB, a0_LSB; |
27 |
unsigned int8 b1_MSB, b1_LSB; |
27 |
unsigned int8 b1_MSB, b1_LSB; |
28 |
unsigned int8 b2_MSB, b2_LSB; |
28 |
unsigned int8 b2_MSB, b2_LSB; |
29 |
unsigned int8 c12_MSB, c12_LSB; |
29 |
unsigned int8 c12_MSB, c12_LSB; |
30 |
|
30 |
|
31 |
output_low(CSN_SPI); |
31 |
output_low(CSN_SPI); |
32 |
|
32 |
|
33 |
spi_write(0x88); // get MSB for a0 |
33 |
spi_write(0x88); // get MSB for a0 |
34 |
a0_MSB = spi_read(0x00); |
34 |
a0_MSB = spi_read(0x00); |
35 |
spi_write(0x8A); // get LSB for a0 |
35 |
spi_write(0x8A); // get LSB for a0 |
36 |
a0_LSB = spi_read(0x00); |
36 |
a0_LSB = spi_read(0x00); |
37 |
|
37 |
|
38 |
spi_write(0x8C); // get MSB for b1 |
38 |
spi_write(0x8C); // get MSB for b1 |
39 |
b1_MSB = spi_read(0x00); |
39 |
b1_MSB = spi_read(0x00); |
40 |
spi_write(0x8E); // get LSB for b1 |
40 |
spi_write(0x8E); // get LSB for b1 |
41 |
b1_LSB = spi_read(0x00); |
41 |
b1_LSB = spi_read(0x00); |
42 |
|
42 |
|
43 |
spi_write(0x90); // get MSB for b2 |
43 |
spi_write(0x90); // get MSB for b2 |
44 |
b2_MSB = spi_read(0x00); |
44 |
b2_MSB = spi_read(0x00); |
45 |
spi_write(0x92); // get LSB for b2 |
45 |
spi_write(0x92); // get LSB for b2 |
46 |
b2_LSB = spi_read(0x00); |
46 |
b2_LSB = spi_read(0x00); |
47 |
|
47 |
|
48 |
spi_write(0x94); // get MSB for c12 |
48 |
spi_write(0x94); // get MSB for c12 |
49 |
c12_MSB = spi_read(0x00); |
49 |
c12_MSB = spi_read(0x00); |
50 |
spi_write(0x96); // get LSB for c12 |
50 |
spi_write(0x96); // get LSB for c12 |
51 |
c12_LSB = spi_read(0x00); |
51 |
c12_LSB = spi_read(0x00); |
52 |
|
52 |
|
53 |
spi_read(0x00); |
53 |
spi_read(0x00); |
54 |
output_high(CSN_SPI); |
54 |
output_high(CSN_SPI); |
55 |
|
55 |
|
56 |
// translate to floating point number |
56 |
// translate to floating point number |
57 |
|
57 |
|
58 |
a0 = ((unsigned int16) a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0; |
58 |
a0 = ((unsigned int16) a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0; |
59 |
b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3; |
59 |
b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3; |
60 |
b2 = ((((unsigned int16) (b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2; |
60 |
b2 = ((((unsigned int16) (b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2; |
61 |
c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0); |
61 |
c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0); |
- |
|
62 |
|
- |
|
63 |
if((a0_MSB | a0_LSB | b1_MSB | b1_LSB | b2_MSB | b2_LSB |c12_MSB | c12_LSB) == 0xFF) |
- |
|
64 |
return (TRUE); |
- |
|
65 |
else return (FALSE); |
62 |
} |
66 |
} |
63 |
|
67 |
|
64 |
float MPL_get_pressure() |
68 |
float MPL_get_pressure() |
65 |
{ |
69 |
{ |
66 |
unsigned int8 LSB_data, MSB_data; |
70 |
unsigned int8 LSB_data, MSB_data; |
67 |
unsigned int16 ADC_pressure, ADC_temperature; |
71 |
unsigned int16 ADC_pressure, ADC_temperature; |
68 |
float Pcomp; |
72 |
float Pcomp; |
69 |
|
73 |
|
70 |
output_low(CSN_SPI); //Start temperature and pressure conversion |
74 |
output_low(CSN_SPI); //Start temperature and pressure conversion |
71 |
spi_write(0x24); |
75 |
spi_write(0x24); |
72 |
spi_write(0x00); |
76 |
spi_write(0x00); |
73 |
output_high(CSN_SPI); |
77 |
output_high(CSN_SPI); |
74 |
|
78 |
|
75 |
delay_ms(10); |
79 |
delay_ms(10); |
76 |
|
80 |
|
77 |
output_low(CSN_SPI); // get MSB for Pressure |
81 |
output_low(CSN_SPI); // get MSB for Pressure |
78 |
spi_write(0x80); |
82 |
spi_write(0x80); |
79 |
MSB_data = spi_read(0x00); |
83 |
MSB_data = spi_read(0x00); |
80 |
spi_write(0x82); // get LSB for Pressure |
84 |
spi_write(0x82); // get LSB for Pressure |
81 |
LSB_data = spi_read(0x00); |
85 |
LSB_data = spi_read(0x00); |
82 |
output_high(CSN_SPI); |
86 |
output_high(CSN_SPI); |
83 |
|
87 |
|
84 |
ADC_pressure = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
88 |
ADC_pressure = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
85 |
|
89 |
|
86 |
output_low(CSN_SPI); |
90 |
output_low(CSN_SPI); |
87 |
spi_write(0x84); |
91 |
spi_write(0x84); |
88 |
MSB_data = spi_read(0x00); |
92 |
MSB_data = spi_read(0x00); |
89 |
spi_write(0x86); // get LSB for Temperature |
93 |
spi_write(0x86); // get LSB for Temperature |
90 |
LSB_data = spi_read(0x00); |
94 |
LSB_data = spi_read(0x00); |
91 |
spi_read(0x00); |
95 |
spi_read(0x00); |
92 |
output_high(CSN_SPI); |
96 |
output_high(CSN_SPI); |
93 |
|
97 |
|
94 |
ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
98 |
ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
95 |
|
99 |
|
96 |
Pcomp = (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); // compute relative compensated pressure |
100 |
Pcomp = (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); // compute relative compensated pressure |
97 |
|
101 |
|
98 |
return (Pcomp * ((115.0 - 50.0)/1023.0) + 50.0); // return absolute pressure |
102 |
return (Pcomp * ((115.0 - 50.0)/1023.0) + 50.0); // return absolute pressure |
99 |
} |
103 |
} |
100 |
|
104 |
|
101 |
float MPL_get_temperature() |
105 |
float MPL_get_temperature() |
102 |
{ |
106 |
{ |
103 |
unsigned int8 LSB_data, MSB_data; |
107 |
unsigned int8 LSB_data, MSB_data; |
104 |
unsigned int16 ADC_temperature; |
108 |
unsigned int16 ADC_temperature; |
105 |
|
109 |
|
106 |
output_low(CSN_SPI); //Start temperature and pressure conversion |
110 |
output_low(CSN_SPI); //Start temperature and pressure conversion |
107 |
spi_write(0x22); |
111 |
spi_write(0x22); |
108 |
spi_write(0x00); |
112 |
spi_write(0x00); |
109 |
output_high(CSN_SPI); |
113 |
output_high(CSN_SPI); |
110 |
|
114 |
|
111 |
delay_ms(10); |
115 |
delay_ms(10); |
112 |
|
116 |
|
113 |
output_low(CSN_SPI); |
117 |
output_low(CSN_SPI); |
114 |
spi_write(0x84); |
118 |
spi_write(0x84); |
115 |
MSB_data = spi_read(0x00); |
119 |
MSB_data = spi_read(0x00); |
116 |
spi_write(0x86); // get LSB for Temperature |
120 |
spi_write(0x86); // get LSB for Temperature |
117 |
LSB_data = spi_read(0x00); |
121 |
LSB_data = spi_read(0x00); |
118 |
spi_read(0x00); |
122 |
spi_read(0x00); |
119 |
output_high(CSN_SPI); |
123 |
output_high(CSN_SPI); |
120 |
|
124 |
|
121 |
ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
125 |
ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable |
122 |
|
126 |
|
123 |
return ( ((float) ADC_temperature - 498.0)/-5.35) + 27.0; // return temperature in deg C |
127 |
return ( ((float) ADC_temperature - 498.0)/-5.35) + 27.0; // return temperature in deg C |
124 |
} |
128 |
} |
125 |
|
129 |
|