yincheng.zhong
2024-01-19 7abf818c189ba751b9c21110890b95b9a1881188
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "bmp3.h"
#include "main.h"
 
//ÔÚLIS3DHÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼¶ÁÒ»×Ö½ÚÊý¾Ý
//ReadAddr   :¶Á³öµÄµØÖ· 
//·µ»ØÖµ     :¶Á³öµÄÊý¾Ý
uint8_t BMP390_ReadOneByte(uint8_t ReadAddr)
{
    uint8_t temp=0;                                                                                   
    IIC2_Start();  
    IIC2_Send_Byte(0Xee);         //·¢ËÍдÃüÁî
    IIC2_Wait_Ack();       
    IIC2_Send_Byte(ReadAddr);   //·¢ËͶÁµØÖ·
    IIC2_Wait_Ack();        
    IIC2_Start();              
    IIC2_Send_Byte(0Xef);       //·¢ËͶÁÃüÁ½øÈë½ÓÊÕģʽ           
    IIC2_Wait_Ack();     
    temp=IIC2_Read_Byte(0);           
    IIC2_Stop();                                //²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ         
    return temp;
}
//ÔÚLIS3DHÖ¸¶¨µØÖ·Ð´ÈëÒ»¸öÊý¾Ý
//WriteAddr  :дÈëÊý¾ÝµÄÄ¿µÄµØÖ·    
//DataToWrite:ҪдÈëµÄÊý¾Ý
void BMP390_WriteOneByte(uint8_t WriteAddr,uint8_t DataToWrite)
{                                                                                                  
    IIC2_Start();  
    IIC2_Send_Byte(0Xee);        //·¢ËÍдÃüÁî
    IIC2_Wait_Ack();
    IIC2_Send_Byte(WriteAddr);                    //·¢Ë͵ØÖ·
    IIC2_Wait_Ack();                                                                 
    IIC2_Send_Byte(DataToWrite);     //·¢ËÍ×Ö½Ú                               
    IIC2_Wait_Ack();                     
    IIC2_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ 
    delay_us(10);    
            
}
uint8_t BMP390_WriteLenByte(uint8_t WriteAddr,uint8_t* buffer,uint8_t Len,void *intf_ptr)
{      
    uint8_t t,temp[200];
    memcpy(temp,buffer,Len);
    for(t=0;t<Len;t++)
    {
        BMP390_WriteOneByte(WriteAddr+t,temp[t]);
    }    
return 0;    
}
 
//ÔÚAT24CXXÀïÃæµÄÖ¸¶¨µØÖ·¿ªÊ¼¶Á³ö³¤¶ÈΪLenµÄÊý¾Ý
//¸Ãº¯ÊýÓÃÓÚ¶Á³ö16bit»òÕß32bitµÄÊý¾Ý.
//ReadAddr   :¿ªÊ¼¶Á³öµÄµØÖ· 
//·µ»ØÖµ     :Êý¾Ý
//Len        :Òª¶Á³öÊý¾ÝµÄ³¤¶È2,4
uint8_t BMP390_ReadLenByte(uint8_t ReadAddr,uint8_t* buffer,uint8_t Len,void *intf_ptr)
{      
    uint8_t t;
    uint8_t temp=0;
    for(t=0;t<Len;t++)
    {        
        temp=BMP390_ReadOneByte(ReadAddr+t); 
    memcpy(buffer++,&temp,1);        
    }
    return 0;                                                    
}
void delay_us2(uint32_t nTimer)  
{  
    uint32_t i=0;  
    for(i=0;i<nTimer;i++){  
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();  
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();  
        __NOP();__NOP();__NOP();__NOP(); 
    }  
}
    int8_t rslt;
    uint8_t loop = 0;
    uint16_t settings_sel;
    struct bmp3_dev dev;
    struct bmp3_data data = { 0 };
    struct bmp3_settings settings = { 0 };
    struct bmp3_status status = { { 0 } };
#define ITERATION  UINT8_C(100)
float Altitude2;
uint8_t data_read[10],data_write[10] = {1,2,3,4,5,6,7,8,9,10};
 
//#define FIFO_FRAME_COUNT  UINT8_C(50)
 
///* Maximum FIFO size */
//#define FIFO_MAX_SIZE     UINT16_C(100)
//        struct bmp3_dev dev;
//    int8_t rslt;
 
//uint8_t try = 1;
//static uint8_t index = 0;
//uint16_t settings_sel;
//uint16_t settings_fifo;
//uint16_t fifo_length = 0;
//uint8_t fifo_data[FIFO_MAX_SIZE];
//struct bmp3_data fifo_p_t_data[FIFO_MAX_SIZE];
//struct bmp3_fifo_settings fifo_settings;
//struct bmp3_settings settings;
//struct bmp3_fifo_data fifo;
//struct bmp3_status status;
///* Iteration count to run example code */
//void Bmp390Init2(void)
//{
 
//    
//    dev.chip_id = 0xee;
//    dev.intf = BMP3_I2C_INTF;
//    dev.read = &BMP390_ReadLenByte;
//    dev.write = &BMP390_WriteLenByte;
//    dev.delay_us = delay_us2;
//    rslt = bmp3_init(&dev);
// //   bmp3_check_rslt("bmp3_init", rslt);
 
//    fifo_settings.mode = BMP3_ENABLE;
//    fifo_settings.press_en = BMP3_ENABLE;
//    fifo_settings.temp_en = BMP3_ENABLE;
//    fifo_settings.filter_en = BMP3_ENABLE;
//    fifo_settings.down_sampling = BMP3_FIFO_NO_SUBSAMPLING;
//    fifo_settings.ffull_en = BMP3_ENABLE;
 
//    fifo.buffer = fifo_data;
//    fifo.req_frames = FIFO_FRAME_COUNT;
 
//    settings.press_en = BMP3_ENABLE;
//    settings.temp_en = BMP3_ENABLE;
//    settings.odr_filter.press_os = BMP3_NO_OVERSAMPLING;
//    settings.odr_filter.temp_os = BMP3_NO_OVERSAMPLING;
//    settings.odr_filter.odr = BMP3_ODR_50_HZ;
 
//    settings_sel = BMP3_SEL_PRESS_EN | BMP3_SEL_TEMP_EN | BMP3_SEL_PRESS_OS | BMP3_SEL_TEMP_OS | BMP3_SEL_ODR;
 
//    settings_fifo = BMP3_SEL_FIFO_MODE | BMP3_SEL_FIFO_PRESS_EN | BMP3_SEL_FIFO_TEMP_EN | BMP3_SEL_FIFO_FULL_EN |
//                    BMP3_SEL_FIFO_DOWN_SAMPLING | BMP3_SEL_FIFO_FILTER_EN;
 
//    rslt = bmp3_set_sensor_settings(settings_sel, &settings, &dev);
//   // bmp3_check_rslt("bmp3_set_sensor_settings", rslt);
 
//    settings.op_mode = BMP3_MODE_NORMAL;
//    rslt = bmp3_set_op_mode(&settings, &dev);
//   // bmp3_check_rslt("bmp3_set_op_mode", rslt);
 
//    rslt = bmp3_set_fifo_settings(settings_fifo, &fifo_settings, &dev);
//   // bmp3_check_rslt("bmp3_set_fifo_settings", rslt);
 
//    rslt = bmp3_get_fifo_settings(&fifo_settings, &dev);
//   // bmp3_check_rslt("bmp3_get_fifo_settings", rslt);
 
//  //  printf("Read Fifo full interrupt data\n");
 
//    while (try <= ITERATION)
//    {
//        rslt = bmp3_get_status(&status, &dev);
//   //     bmp3_check_rslt("bmp3_get_status", rslt);
 
//        if ((rslt == BMP3_OK) && (status.intr.fifo_full == BMP3_ENABLE))
//        {
//            rslt = bmp3_get_fifo_length(&fifo_length, &dev);
//      //      bmp3_check_rslt("bmp3_get_fifo_length", rslt);
 
//            rslt = bmp3_get_fifo_data(&fifo, &fifo_settings, &dev);
//     //       bmp3_check_rslt("bmp3_get_fifo_data", rslt);
 
//            /* NOTE : Read status register again to clear FIFO Full interrupt status */
//            rslt = bmp3_get_status(&status, &dev);
//      //      bmp3_check_rslt("bmp3_get_status", rslt);
 
//            if (rslt == BMP3_OK)
//            {
////                printf("\nIteration : %d\n", try);
////                printf("Available fifo length : %d\n", fifo_length);
////                printf("Fifo byte count from fifo structure : %d\n", fifo.byte_count);
 
////                printf("FIFO frames requested : %d\n", fifo.req_frames);
 
//                rslt = bmp3_extract_fifo_data(fifo_p_t_data, &fifo, &dev);
//  //              bmp3_check_rslt("bmp3_extract_fifo_data", rslt);
 
//   //             printf("FIFO frames extracted : %d\n", fifo.parsed_frames);
 
//                for (index = 0; index < fifo.parsed_frames; index++)
//                {
//                        #ifdef BMP3_FLOAT_COMPENSATION
//   //                 printf("Frame[%d]  T: %.2f deg C, P: %.2f Pa\n", index, (fifo_p_t_data[index].temperature),
//   //                        (fifo_p_t_data[index].pressure));
//                        #else
//                    printf("Frame[%d]  T: %ld deg C, P: %lu Pa\n", index,
//                           (long int)(int32_t)(fifo_p_t_data[index].temperature / 100),
//                           (long unsigned int)(uint32_t)(fifo_p_t_data[index].pressure / 100));
//                        #endif
//                }
//            }
 
//            try++;
//        }
//    }
//}
u8 reg_data[5],write_data[2],temp3;
int Bmp390Init(void)
{
 
 
    /* Interface reference is given as a parameter
     *         For I2C : BMP3_I2C_INTF
     *         For SPI : BMP3_SPI_INTF
     */
//    rslt = bmp3_interface_init(&dev, BMP3_I2C_INTF);
//    bmp3_check_rslt("bmp3_interface_init", rslt);
            dev.chip_id = 0xee;
    dev.intf = BMP3_I2C_INTF;
    dev.read = &BMP390_ReadLenByte;
    dev.write = &BMP390_WriteLenByte;
    dev.delay_us = delay_us2;
    rslt = bmp3_init(&dev);
//    bmp3_check_rslt("bmp3_init", rslt);
 
    settings.int_settings.drdy_en = BMP3_ENABLE;
    settings.press_en = BMP3_ENABLE;
    settings.temp_en = BMP3_ENABLE;
 
    settings.odr_filter.press_os = BMP3_OVERSAMPLING_32X;
    settings.odr_filter.temp_os = BMP3_OVERSAMPLING_2X;
        settings.odr_filter.iir_filter = BMP3_IIR_FILTER_COEFF_7;
    settings.odr_filter.odr = BMP3_ODR_6_25_HZ;
 
    settings_sel = BMP3_SEL_PRESS_EN |BMP3_SEL_IIR_FILTER| BMP3_SEL_TEMP_EN | BMP3_SEL_PRESS_OS | BMP3_SEL_TEMP_OS | BMP3_SEL_ODR |
                   BMP3_SEL_DRDY_EN;
 
    rslt = bmp3_set_sensor_settings(settings_sel, &settings, &dev);
//    bmp3_check_rslt("bmp3_set_sensor_settings", rslt);
 
    settings.op_mode = BMP3_MODE_NORMAL;
    rslt = bmp3_set_op_mode(&settings, &dev);
//    bmp3_check_rslt("bmp3_set_op_mode", rslt);
write_data[0]=BMP3_IIR_FILTER_COEFF_7<<1;
temp3 = BMP3_REG_CONFIG;
    bmp3_set_regs(&temp3, write_data, 1, &dev);
    bmp3_get_regs(temp3, reg_data, 1, &dev);
    while (1)
    {
        rslt = bmp3_get_status(&status, &dev);
     //   bmp3_check_rslt("bmp3_get_status", rslt);
 
        /* Read temperature and pressure data iteratively based on data ready interrupt */
        if ((rslt == BMP3_OK) && (status.intr.drdy == BMP3_ENABLE))
        {
            /*
             * First parameter indicates the type of data to be read
             * BMP3_PRESS_TEMP : To read pressure and temperature data
             * BMP3_TEMP       : To read only temperature data
             * BMP3_PRESS      : To read only pressure data
             */
            rslt = bmp3_get_sensor_data(BMP3_PRESS_TEMP, &data, &dev);
      //      bmp3_check_rslt("bmp3_get_sensor_data", rslt);
 
            /* NOTE : Read status register again to clear data ready interrupt status */
            rslt = bmp3_get_status(&status, &dev);
    //        bmp3_check_rslt("bmp3_get_status", rslt);
                            Altitude2 = 44330*(1-pow(data.pressure/101325.0,1.0/5.255));
            #ifdef BMP3_FLOAT_COMPENSATION
     //       printf("Data[%d]  T: %.2f deg C, P: %.2f Pa\n", loop, (data.temperature), (data.pressure));
            #else
            printf("Data[%d]  T: %ld deg C, P: %lu Pa\n", loop, (long int)(int32_t)(data.temperature / 100),
                   (long unsigned int)(uint32_t)(data.pressure / 100));
            #endif
 
            loop = loop + 1;
        }
    }
 
   // bmp3_coines_deinit();
 
    return rslt;
}