forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

周巍
2024-04-11 91ef77c00ed797b1048c5187f416e351e646a009
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
 
#include "ads1120.h"
 
 
#define uchar unsigned char
#define uint  unsigned int
 
#define    TEMGET    1
#define    PREGET    2
 
 
unsigned char Init_Config[4];
unsigned char i;
//extern unsigned int Result;
//unsigned int Result;
#define    MAXVALUE    32768
#define    TEM_K_FACTOR    (1.5/8) 
#define    PRE_K_FACTOR    (2.048/16)
 
/***************************************************************************
* Function Name:  ADS1120_Init()    
* Purpose:       ADS120 initialize
* Params :        
*
*************************************************************************/
void ADS1120_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    //´ò¿ª¿ØÖƶ˿ÚʱÖÓ
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE, ENABLE);
    //¿ØÖÆÒý½Å£¨Êä³ö£©³õʼ»¯
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                //A6 Miso
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                            //02-019 
    //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                                               //B11 DATA ready
    //GPIO_Init(GPIOE,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                               //B11 DATA ready
    GPIO_Init(GPIOE,&GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                               //B10 ADC cs
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;  
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;  
    //GPIO_Init(GPIOE,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
    GPIO_Init(GPIOE,&GPIO_InitStructure);
 
    TEM_CS_H;
//    PRE_CS_H;
    //SCLK_L;
    ADReset(TEMGET);
    //ADReset(PREGET);
}
 
 
/***************************************************************************
* Function Name:  WriteOneByte(unsigned char command)    
* Purpose:       write one byte to ADS1120
* Params :        
*
*************************************************************************/
void WriteOneByte(unsigned char command)
{
    unsigned char i;
    for(i = 0; i < 8;i++)
    {
        if(command&0x80)
          DIN_H;
        else
          DIN_L;
          command <<= 1;
          SCLK_H;
          SCLK_L;
    }
 
 
    
}
 
 
/***************************************************************************
* Function Name:  ReadOneByte()    
* Purpose:       read one byte from ADS1120
* Params :        result
*
*************************************************************************/
unsigned char ReadOneByte(void)
{
    unsigned char result,i;
    SCLK_L;
    for(i = 0;i<8; i++)
    {
        SCLK_H;
        result <<= 0x01;
        if(DOUT)
          result |= 0X01;
        SCLK_L;
    }
 
    return result;
}
 
 
/***************************************************************************
* Function Name:  ReadData()    
* Purpose:       read data from ADS1120
* Params :        result 0~65535
*
*************************************************************************/
int16_t ReadData(u8 ch_type)
{
    int16_t result;
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    WriteOneByte(RDATA);
    result = ReadOneByte();
    result = (result<<8) | ReadOneByte();
 
    if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
    return result;
}
 
 
/***************************************************************************
* Function Name:  ADReset()    
* Purpose:       reset ADS1120
* Params :        
*
*************************************************************************/
void ADReset(u8 ch_type)
{
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    WriteOneByte(RESET);
     if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
}
 
 
/***************************************************************************
* Function Name:  ADPowerDown()    
* Purpose:       
* Params :        
*
*************************************************************************/
void ADPowerDown(u8 ch_type)
{
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    WriteOneByte(POWERDOWN);
    if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
}
 
 
/***************************************************************************
* Function Name:  ADStartConversion()    
* Purpose:       start conversion
* Params :        
*
*************************************************************************/
void ADStartConversion(u8 ch_type)
{
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    WriteOneByte(START);
    if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
}
 
 
 
 
/***************************************************************************
* Function Name:  ReadRegister()    
* Purpose:       read data from register
* Params :        data 
*
*************************************************************************/
void ReadRegister(u8 ch_type)
{
    unsigned char i;
    unsigned long Data;
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    WriteOneByte(RREG|0x03);
    // get the register content
    for (i=0; i< 4; i++)
    {
         Data += ReadOneByte();
    }
   
    if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
    return;
}
 
 
/***************************************************************************
* Function Name:  WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)    
* Purpose:       write data to register
* steps:          1.write address  2.write data  
* Params          NumRegs-----the numbers of register
*************************************************************************/
void WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData,u8 ch_type)
{
    unsigned char i;
    if(ch_type == TEMGET)
    {
        TEM_CS_L;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_L;
 
    }
    // send the command byte
    WriteOneByte(WREG | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));
    // send the data bytes
    for (i=0; i< NumRegs; i++)
    {
      WriteOneByte(*pData);
      pData++;
    }
    if(ch_type == TEMGET)
    {
        TEM_CS_H;
    }else if(ch_type == PREGET)
    {
        //PRE_CS_H;
 
    }
    return;
}
 
 
/***************************************************************************
* Function Name:  CofigTemAD(unsigned char channel)    
* Purpose:       set the sampling channel
* Params :        
*
*************************************************************************/
void CofigTemAD(void)
{
  /*ÈýÏßÖÆÅäÖüĴæÆ÷*/
    Init_Config[0] = 0x66;// 
    Init_Config[1] = 0X04;//
    Init_Config[2] = 0X55;//
    Init_Config[3] = 0X70;
    WriteRegister(0x00,0x04,Init_Config,TEMGET);
    ReadRegister(TEMGET);
}
 
void CofigPreAD()
{
  /*ËÄÏßÖÆÅäÖüĴæÆ÷*/
    Init_Config[0] = 0x38;// 
    Init_Config[1] = 0X04;//
    Init_Config[2] = 0X18;//
    Init_Config[3] = 0X00;
    WriteRegister(0x00,0x04,Init_Config,PREGET);
    ReadRegister(PREGET);
}
 
 
 
 
/***************************************************************************
* Function Name:  GetAD(unsigned char channel)    
* Purpose:       get the result 
* Params :        Result 0~65535
*
*************************************************************************/
int16_t TemGetAD()
{
    int16_t Result;
    CofigTemAD();
    ADStartConversion(TEMGET);
    while(TEM_DRDY_IN);
    Result = ReadData(TEMGET);
    ADPowerDown(TEMGET);
    return Result;
}
 
 
int16_t PreGetAD()
{
    int16_t Result;
    CofigPreAD();
    ADStartConversion(PREGET);
   // while(PRE_DRDY_IN);
    Result = ReadData(PREGET);
    ADPowerDown(PREGET);
    return Result;
}
 
float    pressure_ad_value(void)
{
    unsigned int ADC_Res;
    float        ad_res;
 
    //CofigPreAD();
    ADC_Res = PreGetAD();
 
    ad_res = (float)ADC_Res / MAXVALUE;
    
    ad_res = ad_res * PRE_K_FACTOR;
    return    ad_res;
}
 
float    temperature_ad_value(void)
{
    unsigned int ADC_Res;
    float        ad_res;
    //CofigTemAD();
    ADC_Res = TemGetAD();
 
    ad_res = (float)ADC_Res/MAXVALUE;
    ad_res = ad_res * TEM_K_FACTOR;
    return    ad_res;
}
 
void ADS1120_Init(void)
{
//    uint16_t i,j;
 
    ADS_for_spi();
    
    //ADPowerDown(PREGET);
//    ADPowerDown(TEMGET);
 
    
    
}
  
void ADS_for_spi(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    //´ò¿ª¿ØÖƶ˿ÚʱÖÓ
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                                //A6 Miso
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                               //B11 DATA ready
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                               //B11 DATA ready
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                               //B11 DATA ready
    GPIO_Init(GPIOE,&GPIO_InitStructure);
 
    TEM_PRE_POWER_Power_ON;
    TEM_CS_H;
    //PRE_CS_H;
 
}