/** ****************************************************************************** * @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; }