Rev 2776 Rev 2777
Line 16... Line 16...
16 #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)
17 #define SPI_MODE_1 (SPI_L_TO_H) 17 #define SPI_MODE_1 (SPI_L_TO_H)
18 #define SPI_MODE_2 (SPI_H_TO_L) 18 #define SPI_MODE_2 (SPI_H_TO_L)
19 #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)
20   20  
21 float a0; 21 float a0; // correctiaon coefficients
22 float b1; 22 float b1;
23 float b2; 23 float b2;
24 float c12; 24 float c12;
25   25  
26   26  
Line 36... Line 36...
36 spi_write(0x88); // get MSB for a0 36 spi_write(0x88); // get MSB for a0
37 a0_MSB = spi_read(0x00); 37 a0_MSB = spi_read(0x00);
38 spi_write(0x8A); // get LSB for a0 38 spi_write(0x8A); // get LSB for a0
39 a0_LSB = spi_read(0x00); 39 a0_LSB = spi_read(0x00);
40   40  
41 spi_write(0x8C); // get MSB for a0 41 spi_write(0x8C); // get MSB for b1
42 b1_MSB = spi_read(0x00); 42 b1_MSB = spi_read(0x00);
43 spi_write(0x8E); // get LSB for a0 43 spi_write(0x8E); // get LSB for b1
44 b1_LSB = spi_read(0x00); 44 b1_LSB = spi_read(0x00);
45   45  
46 spi_write(0x90); // get MSB for a0 46 spi_write(0x90); // get MSB for b2
47 b2_MSB = spi_read(0x00); 47 b2_MSB = spi_read(0x00);
48 spi_write(0x92); // get LSB for a0 48 spi_write(0x92); // get LSB for b2
49 b2_LSB = spi_read(0x00); 49 b2_LSB = spi_read(0x00);
50   50  
51 spi_write(0x94); // get MSB for a0 51 spi_write(0x94); // get MSB for c12
52 c12_MSB = spi_read(0x00); 52 c12_MSB = spi_read(0x00);
53 spi_write(0x96); // get LSB for a0 53 spi_write(0x96); // get LSB for c12
54 c12_LSB = spi_read(0x00); 54 c12_LSB = spi_read(0x00);
55   55  
56 spi_read(0x00); 56 spi_read(0x00);
57 output_high(CSN_SPI); 57 output_high(CSN_SPI);
-   58
-   59 // translate to floating point number
58 60
59 a0 = ((unsigned int16) a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0; 61 a0 = ((unsigned int16) a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0;
60 b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3; 62 b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3;
61 b2 = ((((unsigned int16) (b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2; 63 b2 = ((((unsigned int16) (b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2;
62 c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0); 64 c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0);
63   -  
64 printf("%4.3f %1.5f %1.5f %1.9f \r\n", a0, b1, b2, c12); -  
65   -  
66 } 65 }
67   66  
68 float MPL_get_pressure() 67 float MPL_get_pressure()
69 { 68 {
70 unsigned int8 LSB_data, MSB_data; 69 unsigned int8 LSB_data, MSB_data;
Line 94... Line 93...
94 LSB_data = spi_read(0x00); 93 LSB_data = spi_read(0x00);
95 spi_read(0x00); 94 spi_read(0x00);
96 output_high(CSN_SPI); 95 output_high(CSN_SPI);
97   96  
98 ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable 97 ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable
99   -  
100 printf("%lu %lu \r\n", ADC_pressure, ADC_temperature); -  
101 98
102 Pcomp = (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); 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);
103   113  
-   114 delay_ms(10);
-   115
-   116 output_low(CSN_SPI);
-   117 spi_write(0x84);
104 printf("%f \r\n", Pcomp); 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);
105   123  
-   124 ADC_temperature = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable
-   125
106 return (Pcomp * ((115.0 - 50.0)/1023.0) + 50.0); 126 return ( ((float) ADC_temperature - 498.0)/-5.35) + 25.0; // return temperature in deg C
107 } 127 }
108   128  
109 void main() 129 void main()
110 { 130 {
111 printf("Start \r\n"); 131 printf("Start \r\n");
112 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); 132 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
113 output_high(CSN_SPI); 133 output_high(CSN_SPI);
114 delay_ms(100); 134 delay_ms(100);
115   135  
116 MPL_init(); 136 MPL_init(); // get correction coeffivcients from the sensor
117   137  
118 while(true) 138 while(true)
119 { 139 {
120 // MPL_init(); -  
121 printf("%f \r\n", MPL_get_pressure()); 140 printf("%f %f \r\n", MPL_get_pressure(), MPL_get_temperature());
122 // printf("%ld %ld %ld %ld \r\n",a0, b1, b2, c12); -  
123 //MPL_get_pressure(); -  
124   -  
125 delay_ms(500); 141 delay_ms(500);
126 } 142 }
127 } 143 }