QuakeGod
2024-11-25 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9
提交 | 用户 | age
9aed5d 1 /**
Q 2   ******************************************************************************
3   * @file           : ADC.c
4   * @brief          : ADC functions program body
5   ******************************************************************************
6     */
7 #include "ADC.h"
8 #include "globaldef.h"
9 #include "functions.h"
10 #include "string.h"
11 #include "modbusRTU.h"
12
13 #include "stm32f0xx_hal.h"
14
15 #define ADCrefAddr 0x1FFFF7BA
16
17
18 #define AVGCount 8
19     
20 int nCount6 = 0;
21 unsigned short ADC_TEMPs[16] ={0};
22
23 int ADCProcess2()
24 {
25     // ADC channels
26     //  0 --      --> 0
27     //  1 --      --> 1
28     //    2 -- 
29     //  3 -- 
30     //  4 --  IN1                    ->4
31     //  5 --     TEMP        ->5  
32     //  6 --     AC_CURRENT  ->6
33     //  7 -- 
34     //  8 -- 
35     //                         --> 5
36     // 16 -- Temp  --> 16
37     // 17 -- Vref  --> 17
38
39
40     
41 uint16_t ADC_ConvertedValue=0;
42 static int CurChannel=LL_ADC_CHANNEL_0;
43 //static int waitcount = 0;
44     
45                 if (!LL_ADC_REG_IsConversionOngoing(ADC1))
46                 {
47                     //waitcount++;
48                     //if (waitcount<2) return 0;
49                     //waitcount=0;
50                     ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
51                     
52         //            ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
53                     int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
54                     int nextchannel = LL_ADC_CHANNEL_0;
55                     if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
56                     {
57                         KMem.ADCValues[0] = ADC_ConvertedValue;
58                         nextchannel = LL_ADC_CHANNEL_4;
59                     }
60                     else if ((channels & LL_ADC_CHANNEL_4) == LL_ADC_CHANNEL_4)
61                     {
62                         ADC_TEMPs[0] += ADC_ConvertedValue;
63                         
64                         
65                         nextchannel = LL_ADC_CHANNEL_5;
66                     }
67                     else if ((channels & LL_ADC_CHANNEL_5) == LL_ADC_CHANNEL_5)
68                     {
69                         ADC_TEMPs[1] += ADC_ConvertedValue;
70                         
71                         nCount6++;
72                         
73                         if (nCount6 >= AVGCount) {
74                             
75                             KMem.ADCValues[3] = ADC_TEMPs[0]/AVGCount;
76             
77                             KMem.ADCValues[4] = ADC_TEMPs[1]/AVGCount;
78                             ADC_TEMPs[0]=0;
79                             ADC_TEMPs[1]=0;
80                             nCount6 = 0 ;
81                                 
82                         }
83                         //    KMem.ADCValues[3] = 255;//ADC_TEMP[0]/16;
84                         //    KMem.ADCValues[4] = 768; //ADC_TEMP[1]/16;                        
85                         nextchannel = LL_ADC_CHANNEL_6;
86
87                     }else if ((channels & LL_ADC_CHANNEL_6) == LL_ADC_CHANNEL_6)
88                     {
89                         KMem.ADCValues[6] = ADC_ConvertedValue;                        
90                         nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
91
92                     }else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
93                     {
94                         KMem.ADCValues[16] = ADC_ConvertedValue;        
95                             nextchannel = LL_ADC_CHANNEL_VREFINT;
96                     }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
97                     {
98                         KMem.ADCValues[17] = ADC_ConvertedValue;                        
99                         KMem.ADCValues[18] = *((unsigned short *)ADCrefAddr);
100                         KMem.ADCValues[19] = *((unsigned short *)ADCrefAddr);                
101                         
102                         nextchannel = LL_ADC_CHANNEL_0;
103                     }else
104                     {
105                         //ADCValues[0] = ADC_ConvertedValue;                        
106                     }
107                     //nextchannel = LL_ADC_CHANNEL_VREFINT;
108                     LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);                    
109                     LL_ADC_REG_StartConversion(ADC1);
110                     CurChannel = nextchannel;
111                 }    
112     return 0;
113 }
114
115