2022-01-18 00:58:54 +08:00

133 lines
4.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*********************************************************************************************************************
* COPYRIGHT NOTICE
* Copyright (c) 2020,<2C><><EFBFBD>ɿƼ<C9BF>
* All rights reserved.
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<51><C8BA><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA>824575535
*
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>;<EFBFBD><CDBE>
* <20><>ӭ<EFBFBD><D3AD>λʹ<CEBB>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EBB1A3><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC>İ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @file zf_vadc
* @company <09>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>޹<EFBFBD>˾
* @author <09><><EFBFBD>ɿƼ<C9BF>(QQ3184284598)
* @version <09>鿴doc<6F><63>version<6F>ļ<EFBFBD> <20>汾˵<E6B1BE><CBB5>
* @Software ADS v1.2.2
* @Target core TC264D
* @Taobao https://seekfree.taobao.com/
* @date 2020-3-23
********************************************************************************************************************/
#include "Vadc/Adc/IfxVadc_Adc.h"
#include "zf_assert.h"
#include "zf_vadc.h"
#include "stdio.h"
#define ADC_SAMPLE_FREQUENCY 10000000//<2F><><EFBFBD><EFBFBD>10Mhz
//-------------------------------------------------------------------------------------------------------------------
// @brief ADC<44><43>ʼ<EFBFBD><CABC>
// @param adcn ѡ<><D1A1>ADCģ<43><C4A3>(ADC_0<5F><30>ADC_1<5F><31>ADC_2)
// @param ch ѡ<><D1A1>ADCͨ<43><CDA8>
// @return void
// Sample usage: adc_init(ADC_0, ADC0_CH0_A0);
//-------------------------------------------------------------------------------------------------------------------
void adc_init(VADCN_enum vadc_n, VADC_CHN_enum vadc_chn)
{
static uint8 mudule_init_flag = 0;
IfxVadc_Adc vadc;
IfxVadc_Adc_Group adcGroup;
IfxVadc_Adc_Config adcConfig;
IfxVadc_Adc_initModuleConfig(&adcConfig, &MODULE_VADC);
if(!mudule_init_flag)
{
mudule_init_flag = 1;
IfxVadc_Adc_initModule(&vadc, &adcConfig);
}
else
{
vadc.vadc = adcConfig.vadc;
}
IfxVadc_Adc_GroupConfig adcGroupConfig;
IfxVadc_Adc_initGroupConfig(&adcGroupConfig, &vadc);
adcGroupConfig.groupId = (IfxVadc_GroupId)vadc_n;//IfxVadc_GroupId_0;
adcGroupConfig.master = adcGroupConfig.groupId;
adcGroupConfig.arbiter.requestSlotBackgroundScanEnabled = TRUE;
adcGroupConfig.backgroundScanRequest.autoBackgroundScanEnabled = TRUE;
adcGroupConfig.backgroundScanRequest.triggerConfig.gatingMode = IfxVadc_GatingMode_always;
adcGroupConfig.inputClass[0].resolution = IfxVadc_ChannelResolution_12bit;
adcGroupConfig.inputClass[0].sampleTime = 1.0f/ADC_SAMPLE_FREQUENCY;
adcGroupConfig.inputClass[1].resolution = IfxVadc_ChannelResolution_12bit;
adcGroupConfig.inputClass[1].sampleTime = 1.0f/ADC_SAMPLE_FREQUENCY;
IfxVadc_Adc_initGroup(&adcGroup, &adcGroupConfig);
IfxVadc_Adc_ChannelConfig adcChannelConfig;
IfxVadc_Adc_Channel adcChannel;
IfxVadc_Adc_initChannelConfig(&adcChannelConfig, &adcGroup);
adcChannelConfig.channelId = (IfxVadc_ChannelId)(vadc_chn%16);
adcChannelConfig.resultRegister = (IfxVadc_ChannelResult)(vadc_chn%16);
adcChannelConfig.backgroundChannel = TRUE;
IfxVadc_Adc_initChannel(&adcChannel, &adcChannelConfig);
unsigned channels = (1 << adcChannelConfig.channelId);
unsigned mask = channels;
IfxVadc_Adc_setBackgroundScan(&vadc, &adcGroup, channels, mask);
IfxVadc_Adc_startBackgroundScan(&vadc);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ADCת<43><D7AA>һ<EFBFBD><D2BB>
// @param adcn ѡ<><D1A1>ADCģ<43><C4A3>(ADC_0<5F><30>ADC_1<5F><31>ADC_2)
// @param ch ѡ<><D1A1>ADCͨ<43><CDA8>
// @return void
// Sample usage: adc_convert(ADC_0, ADC0_CH0_A0, ADC_12BIT);
//-------------------------------------------------------------------------------------------------------------------
uint16 adc_convert(VADCN_enum vadc_n, VADC_CHN_enum vadc_chn, VADC_RES_enum vadc_res)
{
Ifx_VADC_RES result;
uint8 temp;
do
{
result = IfxVadc_getResult(&MODULE_VADC.G[vadc_n], vadc_chn%16);
} while (!result.B.VF);
temp = 4 - (vadc_res * 2);
return((result.U&0x0fff)>>temp);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief ADC<44><43>ֵ<EFBFBD>˲<EFBFBD>
// @param adcn ѡ<><D1A1>ADCģ<43><C4A3>(ADC_0<5F><30>ADC_1<5F><31>ADC_2)
// @param ch ѡ<><D1A1>ADCͨ<43><CDA8>
// @param count <20><>ֵ<EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>
// @return void
// Sample usage: adc_mean_filter(ADC_0, ADC0_CH0_A0, ADC_12BIT, 5);//<2F>ɼ<EFBFBD>5<EFBFBD><35> Ȼ<>󷵻<EFBFBD>ƽ<EFBFBD><C6BD>ֵ
//-------------------------------------------------------------------------------------------------------------------
uint16 adc_mean_filter(VADCN_enum vadc_n, VADC_CHN_enum vadc_chn, VADC_RES_enum vadc_res, uint8 count)
{
uint8 i;
uint32 sum;
ZF_ASSERT(count);//<2F><><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0
sum = 0;
for(i=0; i<count; i++)
{
sum += adc_convert(vadc_n, vadc_chn, vadc_res);
}
sum = sum/count;
return (uint16)sum;
}