QuakeGod
2024-11-25 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9
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
/**
  ******************************************************************************
  * @file           : ADC.c
  * @brief          : ADC functions program body
  ******************************************************************************
    */
#include "ADC.h"
#include "globaldef.h"
#include "functions.h"
#include "string.h"
#include "modbusRTU.h"
 
#include "stm32f0xx_hal.h"
 
#define ADCrefAddr 0x1FFFF7BA
 
 
#define AVGCount 8
    
int nCount6 = 0;
unsigned short ADC_TEMPs[16] ={0};
 
int ADCProcess2()
{
    // ADC channels
    //  0 --      --> 0
    //  1 --      --> 1
    //    2 -- 
    //  3 -- 
    //  4 --  IN1                    ->4
    //  5 --     TEMP        ->5  
    //  6 --     AC_CURRENT  ->6
    //  7 -- 
    //  8 -- 
    //                         --> 5
    // 16 -- Temp  --> 16
    // 17 -- Vref  --> 17
 
 
    
uint16_t ADC_ConvertedValue=0;
static int CurChannel=LL_ADC_CHANNEL_0;
//static int waitcount = 0;
    
                if (!LL_ADC_REG_IsConversionOngoing(ADC1))
                {
                    //waitcount++;
                    //if (waitcount<2) return 0;
                    //waitcount=0;
                    ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
                    
        //            ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
                    int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
                    int nextchannel = LL_ADC_CHANNEL_0;
                    if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
                    {
                        KMem.ADCValues[0] = ADC_ConvertedValue;
                        nextchannel = LL_ADC_CHANNEL_4;
                    }
                    else if ((channels & LL_ADC_CHANNEL_4) == LL_ADC_CHANNEL_4)
                    {
                        ADC_TEMPs[0] += ADC_ConvertedValue;
                        
                        
                        nextchannel = LL_ADC_CHANNEL_5;
                    }
                    else if ((channels & LL_ADC_CHANNEL_5) == LL_ADC_CHANNEL_5)
                    {
                        ADC_TEMPs[1] += ADC_ConvertedValue;
                        
                        nCount6++;
                        
                        if (nCount6 >= AVGCount) {
                            
                            KMem.ADCValues[3] = ADC_TEMPs[0]/AVGCount;
            
                            KMem.ADCValues[4] = ADC_TEMPs[1]/AVGCount;
                            ADC_TEMPs[0]=0;
                            ADC_TEMPs[1]=0;
                            nCount6 = 0 ;
                                
                        }
                        //    KMem.ADCValues[3] = 255;//ADC_TEMP[0]/16;
                        //    KMem.ADCValues[4] = 768; //ADC_TEMP[1]/16;                        
                        nextchannel = LL_ADC_CHANNEL_6;
 
                    }else if ((channels & LL_ADC_CHANNEL_6) == LL_ADC_CHANNEL_6)
                    {
                        KMem.ADCValues[6] = ADC_ConvertedValue;                        
                        nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
 
                    }else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
                    {
                        KMem.ADCValues[16] = ADC_ConvertedValue;        
                            nextchannel = LL_ADC_CHANNEL_VREFINT;
                    }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
                    {
                        KMem.ADCValues[17] = ADC_ConvertedValue;                        
                        KMem.ADCValues[18] = *((unsigned short *)ADCrefAddr);
                        KMem.ADCValues[19] = *((unsigned short *)ADCrefAddr);                
                        
                        nextchannel = LL_ADC_CHANNEL_0;
                    }else
                    {
                        //ADCValues[0] = ADC_ConvertedValue;                        
                    }
                    //nextchannel = LL_ADC_CHANNEL_VREFINT;
                    LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);                    
                    LL_ADC_REG_StartConversion(ADC1);
                    CurChannel = nextchannel;
                }    
    return 0;
}