Line 9... |
Line 9... |
9 |
MPL115A1 GND - GND |
9 |
MPL115A1 GND - GND |
10 |
MPL115A1 VDD - +3.3V |
10 |
MPL115A1 VDD - +3.3V |
11 |
*/ |
11 |
*/ |
12 |
|
12 |
|
13 |
#define CSN_SPI PIN_C2 |
13 |
#define CSN_SPI PIN_C2 |
14 |
int8 address, uiTadc, data ; |
- |
|
15 |
|
- |
|
16 |
|
14 |
|
17 |
// SPI mode definitions. |
15 |
// SPI mode definitions. |
18 |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) |
16 |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) |
19 |
#define SPI_MODE_1 (SPI_L_TO_H) |
17 |
#define SPI_MODE_1 (SPI_L_TO_H) |
20 |
#define SPI_MODE_2 (SPI_H_TO_L) |
18 |
#define SPI_MODE_2 (SPI_H_TO_L) |
21 |
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
19 |
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
22 |
|
20 |
|
- |
|
21 |
unsigned int16 a0; |
- |
|
22 |
unsigned int16 b1; |
- |
|
23 |
unsigned int16 b2; |
- |
|
24 |
unsigned int16 c12; |
23 |
|
25 |
|
24 |
void main() |
- |
|
25 |
{ |
- |
|
26 |
printf("Start \r\n"); |
- |
|
27 |
|
- |
|
28 |
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); |
- |
|
29 |
|
26 |
|
30 |
output_high(CSN_SPI); |
27 |
int MPL_init() |
- |
|
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; |
31 |
|
33 |
|
32 |
delay_ms(100); |
34 |
output_low(CSN_SPI); |
33 |
|
35 |
|
- |
|
36 |
spi_write(0x88); // get MSB for a0 |
- |
|
37 |
a0_MSB = spi_read(0x00); |
- |
|
38 |
spi_write(0x8A); // get LSB for a0 |
34 |
while(true) |
39 |
a0_LSB = spi_read(0x00); |
35 |
{ |
40 |
|
- |
|
41 |
spi_write(0x8C); // get MSB for a0 |
- |
|
42 |
b1_MSB = spi_read(0x00); |
- |
|
43 |
spi_write(0x8E); // get LSB for a0 |
- |
|
44 |
b1_LSB = spi_read(0x00); |
- |
|
45 |
|
36 |
data = 5; //just to be sure that the value is updated |
46 |
spi_write(0x90); // get MSB for a0 |
- |
|
47 |
b2_MSB = spi_read(0x00); |
- |
|
48 |
spi_write(0x92); // get LSB for a0 |
- |
|
49 |
b2_LSB = spi_read(0x00); |
- |
|
50 |
|
- |
|
51 |
spi_write(0x94); // get MSB for a0 |
- |
|
52 |
c12_MSB = spi_read(0x00); |
- |
|
53 |
spi_write(0x96); // get LSB for a0 |
- |
|
54 |
c12_LSB = spi_read(0x00); |
37 |
|
55 |
|
38 |
//Start temperature and pressure conversion |
- |
|
39 |
address = 0x24; |
- |
|
40 |
address &= 0x7F; |
- |
|
41 |
|
- |
|
42 |
output_low(CSN_SPI); |
- |
|
43 |
delay_ms(1); |
- |
|
44 |
spi_write(address); |
- |
|
45 |
delay_ms(1); |
- |
|
46 |
spi_write(0x00); |
56 |
spi_read(0x00); |
47 |
delay_ms(1); |
- |
|
48 |
output_high(CSN_SPI); |
57 |
output_high(CSN_SPI); |
49 |
delay_ms(2); |
- |
|
50 |
|
- |
|
51 |
|
- |
|
52 |
// get MSB for Pressure |
- |
|
53 |
address = 0x00; |
- |
|
54 |
address |= 0x80; |
- |
|
55 |
|
58 |
|
- |
|
59 |
a0 = ((int16) a0_MSB << 8) + a0_LSB; |
56 |
output_low(CSN_SPI); |
60 |
b1 = ((int16) b1_MSB << 8) + b1_LSB; |
57 |
delay_ms(1); |
61 |
b2 = ((int16) b2_MSB << 8) + b2_LSB; |
- |
|
62 |
c12 = ((int16) c12_MSB << 8) + c12_LSB; |
- |
|
63 |
} |
- |
|
64 |
|
58 |
spi_write(address); |
65 |
float MPL_get_pressure() |
- |
|
66 |
{ |
59 |
delay_ms(1); |
67 |
int8 LSB_data, MSB_data; |
- |
|
68 |
int16 ADC_pressure, ADC_temperature; |
- |
|
69 |
|
- |
|
70 |
output_low(CSN_SPI); //Start temperature and pressure conversion |
60 |
data = spi_read(0x00); |
71 |
spi_write(0x24); |
61 |
delay_ms(1); |
72 |
spi_write(0x00); |
62 |
output_high(CSN_SPI); |
73 |
output_high(CSN_SPI); |
63 |
|
74 |
|
- |
|
75 |
delay_ms(3); |
- |
|
76 |
|
- |
|
77 |
output_low(CSN_SPI); // get MSB for Pressure |
- |
|
78 |
spi_write(0x80); |
- |
|
79 |
LSB_data = spi_read(0x00); |
- |
|
80 |
spi_write(0x82); // get LSB for Pressure |
64 |
printf("%u \r\n", data); |
81 |
MSB_data = spi_read(0x00); |
- |
|
82 |
|
- |
|
83 |
ADC_pressure = ((int16) MSB_data << 8) + LSB_data; // conversion of 8bit registers to 16bit variable |
- |
|
84 |
|
- |
|
85 |
spi_write(0x84); |
- |
|
86 |
LSB_data = spi_read(0x00); |
- |
|
87 |
spi_write(0x86); // get LSB for Temperature |
- |
|
88 |
MSB_data = spi_read(0x00); |
- |
|
89 |
spi_read(0x00); |
- |
|
90 |
output_high(CSN_SPI); |
65 |
|
91 |
|
66 |
//uiTadc = (unsigned int) data << 8; |
92 |
ADC_temperature = ((int16) MSB_data << 8) + LSB_data; // conversion of 8bit registers to 16bit variable |
- |
|
93 |
|
67 |
//printf("%u\n\r", uiTadc); |
94 |
// printf("%lX %lX\r\n", ADC_pressure, ADC_temperature); |
- |
|
95 |
|
- |
|
96 |
return (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); |
- |
|
97 |
} |
- |
|
98 |
|
68 |
|
99 |
void main() |
69 |
|
100 |
{ |
- |
|
101 |
printf("Start \r\n"); |
- |
|
102 |
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); |
- |
|
103 |
output_high(CSN_SPI); |
70 |
delay_ms(100); |
104 |
delay_ms(100); |
71 |
} |
105 |
|
- |
|
106 |
MPL_init(); |
72 |
|
107 |
|
- |
|
108 |
while(true) |
- |
|
109 |
{ |
- |
|
110 |
printf("%f \r\n", MPL_get_pressure()); |
- |
|
111 |
delay_ms(100); |
- |
|
112 |
} |
73 |
} |
113 |
} |