Rev 2777 Rev 2778
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   -  
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   -  
21 float a0; // correctiaon coefficients -  
22 float b1; -  
23 float b2; -  
24 float c12; -  
25   -  
26   -  
27 void MPL_init() 14 #include "MPL115A1.c"
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; -  
33   -  
34 output_low(CSN_SPI); -  
35   -  
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); -  
40   -  
41 spi_write(0x8C); // get MSB for b1 -  
42 b1_MSB = spi_read(0x00); -  
43 spi_write(0x8E); // get LSB for b1 -  
44 b1_LSB = spi_read(0x00); -  
45   -  
46 spi_write(0x90); // get MSB for b2 -  
47 b2_MSB = spi_read(0x00); -  
48 spi_write(0x92); // get LSB for b2 -  
49 b2_LSB = spi_read(0x00); -  
50   -  
51 spi_write(0x94); // get MSB for c12 -  
52 c12_MSB = spi_read(0x00); -  
53 spi_write(0x96); // get LSB for c12 -  
54 c12_LSB = spi_read(0x00); -  
55   -  
56 spi_read(0x00); -  
57 output_high(CSN_SPI); -  
58 -  
59 // translate to floating point number -  
60 -  
61 a0 = ((unsigned int16) a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0; -  
62 b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3; -  
63 b2 = ((((unsigned int16) (b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2; -  
64 c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0); -  
65 } -  
66   -  
67 float MPL_get_pressure() -  
68 { -  
69 unsigned int8 LSB_data, MSB_data; -  
70 unsigned int16 ADC_pressure, ADC_temperature; -  
71 float Pcomp; -  
72   -  
73 output_low(CSN_SPI); //Start temperature and pressure conversion -  
74 spi_write(0x24); -  
75 spi_write(0x00); -  
76 output_high(CSN_SPI); -  
77   -  
78 delay_ms(10); -  
79 -  
80 output_low(CSN_SPI); // get MSB for Pressure -  
81 spi_write(0x80); -  
82 MSB_data = spi_read(0x00); -  
83 spi_write(0x82); // get LSB for Pressure -  
84 LSB_data = spi_read(0x00); -  
85 output_high(CSN_SPI); -  
86   -  
87 ADC_pressure = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable -  
88   -  
89 output_low(CSN_SPI); -  
90 spi_write(0x84); -  
91 MSB_data = spi_read(0x00); -  
92 spi_write(0x86); // get LSB for Temperature -  
93 LSB_data = spi_read(0x00); -  
94 spi_read(0x00); -  
95 output_high(CSN_SPI); -  
96   -  
97 ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable -  
98 -  
99 Pcomp = (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); // compute relative compensated pressure -  
100   -  
101 return (Pcomp * ((115.0 - 50.0)/1023.0) + 50.0); // return absolute pressure -  
102 } -  
103   -  
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 -  
127 } -  
128   15  
129 void main() 16 void main()
130 { 17 {
131 printf("Start \r\n"); 18 printf("Start \r\n");
132 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); 19 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
133 output_high(CSN_SPI); 20 output_high(CSN_SPI);
134 delay_ms(100); 21 delay_ms(100);
135   22  
136 MPL_init(); // get correction coeffivcients from the sensor 23 MPL_init(); // get correction coefficients from the sensor
137   24  
138 while(true) 25 while(true)
139 { 26 {
140 printf("%f %f \r\n", MPL_get_pressure(), MPL_get_temperature()); 27 printf("%f %f \r\n", MPL_get_pressure(), MPL_get_temperature());
141 delay_ms(500); 28 delay_ms(500);