Rev 2774 Rev 2775
1 #include <main.h> 1 #include <main.h>
2   2  
3 /* 3 /*
4 MPL115A1 SDN - +3.3V (always power on) 4 MPL115A1 SDN - +3.3V (always power on)
5 MPL115A1 #CS - PIN_C2 5 MPL115A1 #CS - PIN_C2
6 MPL115A1 DOUT - PIN_C4 6 MPL115A1 DOUT - PIN_C4
7 MPL115A1 DIN - PIN_C5 7 MPL115A1 DIN - PIN_C5
8 MPL115A1 SCLK - PIN_C3 8 MPL115A1 SCLK - PIN_C3
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   14  
15 // SPI mode definitions. 15 // SPI mode definitions.
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 signed int16 a0; 21 float a0;
22 signed int16 b1; 22 float b1;
23 signed int16 b2; 23 float b2;
24 signed int16 c12; 24 float c12;
25   25  
26   26  
27 int MPL_init() 27 void MPL_init()
28 { 28 {
29 unsigned int8 a0_MSB, a0_LSB; 29 unsigned int8 a0_MSB, a0_LSB;
30 unsigned int8 b1_MSB, b1_LSB; 30 unsigned int8 b1_MSB, b1_LSB;
31 unsigned int8 b2_MSB, b2_LSB; 31 unsigned int8 b2_MSB, b2_LSB;
32 unsigned int8 c12_MSB, c12_LSB; 32 unsigned int8 c12_MSB, c12_LSB;
33   33  
34 output_low(CSN_SPI); 34 output_low(CSN_SPI);
35   35  
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 a0
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 a0
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 a0
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 a0
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 a0
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 a0
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 58
59 a0 = ((int16) a0_MSB << 8) + a0_LSB; 59 a0 = (a0_MSB << 5) + (a0_LSB >> 3) + (a0_LSB & 0x07)/8.0;
60 b1 = ((int16) b1_MSB << 8) + b1_LSB; 60 b1 = ((((b1_MSB & 0x1F) * 0x100) + b1_LSB) / 8192.0) - 3;
61 b2 = ((int16) b2_MSB << 8) + b2_LSB; 61 b2 = ((((b2_MSB - 0x80) << 8) + b2_LSB)/ 16384.0) - 2;
62 c12 = ((int16) c12_MSB << 8) + c12_LSB; 62 c12 =(((c12_MSB * 0x100) + c12_LSB)/16777216.0);
63 } 63 }
64   64  
65 float MPL_get_pressure() 65 float MPL_get_pressure()
66 { 66 {
67 unsigned int8 LSB_data, MSB_data; 67 unsigned int8 LSB_data, MSB_data;
68 unsigned int16 ADC_pressure, ADC_temperature; 68 unsigned int16 ADC_pressure, ADC_temperature;
-   69 float Pcomp;
69   70  
70 output_low(CSN_SPI); //Start temperature and pressure conversion 71 output_low(CSN_SPI); //Start temperature and pressure conversion
71 spi_write(0x24); 72 spi_write(0x24);
72 spi_write(0x00); 73 spi_write(0x00);
73 output_high(CSN_SPI); 74 output_high(CSN_SPI);
74   75  
75 delay_ms(10); 76 delay_ms(10);
76 77
77 output_low(CSN_SPI); // get MSB for Pressure 78 output_low(CSN_SPI); // get MSB for Pressure
78 spi_write(0x80); 79 spi_write(0x80);
79 LSB_data = spi_read(0x00); -  
80 spi_write(0x82); // get LSB for Pressure -  
81 MSB_data = spi_read(0x00); 80 MSB_data = spi_read(0x00);
-   81 spi_write(0x82); // get LSB for Pressure
-   82 LSB_data = spi_read(0x00);
82 output_high(CSN_SPI); 83 output_high(CSN_SPI);
83   84  
84 printf("%lX %lX\r\n", MSB_data, LSB_data); 85 printf("%lX %lX\r\n", MSB_data, LSB_data);
85 ADC_pressure = ((int16) MSB_data << 8) + LSB_data; // conversion of 8bit registers to 16bit variable 86 ADC_pressure = (((unsigned int16) MSB_data << 8) + LSB_data ) >> 6; // conversion of 8bit registers to 16bit variable
86   87  
87 output_low(CSN_SPI); 88 output_low(CSN_SPI);
88 spi_write(0x84); 89 spi_write(0x84);
89 LSB_data = spi_read(0x00); -  
90 spi_write(0x86); // get LSB for Temperature -  
91 MSB_data = spi_read(0x00); 90 MSB_data = spi_read(0x00);
-   91 spi_write(0x86); // get LSB for Temperature
-   92 LSB_data = spi_read(0x00);
92 spi_read(0x00); 93 spi_read(0x00);
93 output_high(CSN_SPI); 94 output_high(CSN_SPI);
94   95  
95 printf("%lX %lX\r\n", MSB_data, LSB_data); 96 printf("%lX %lX\r\n", MSB_data, LSB_data);
96 ADC_temperature = ((int16) MSB_data << 8) + LSB_data; // 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
-   98  
-   99 printf("%lu %lu \r\n", ADC_pressure, ADC_temperature);
97   100
98 printf("%lX %lX\r\n", ADC_pressure, ADC_temperature); 101 Pcomp = (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature );
99   102  
100 return (a0 + (b1 + c12 * ADC_temperature) * ADC_pressure + b2 * ADC_temperature ); 103 return (Pcomp * ((115.0 - 50.0)/1023.0) + 50.0);
101 } 104 }
102   105  
103 void main() 106 void main()
104 { 107 {
105 printf("Start \r\n"); 108 printf("Start \r\n");
106 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); 109 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
107 output_high(CSN_SPI); 110 output_high(CSN_SPI);
108 delay_ms(100); 111 delay_ms(100);
109   112  
110 MPL_init(); 113 MPL_init();
111   114  
112 while(true) 115 while(true)
113 { 116 {
114 // MPL_init(); 117 // MPL_init();
115 // printf("%f \r\n", MPL_get_pressure()); 118 printf("%f \r\n", MPL_get_pressure());
116 // printf("%ld %ld %ld %ld \r\n",a0, b1, b2, c12); 119 // printf("%ld %ld %ld %ld \r\n",a0, b1, b2, c12);
117 MPL_get_pressure(); 120 //MPL_get_pressure();
118   121  
119 delay_ms(500); 122 delay_ms(500);
120 } 123 }
121 } 124 }