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