mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-22 17:12:55 +08:00
133 lines
4.7 KiB
C
133 lines
4.7 KiB
C
|
/*********************************************************************************************************************
|
|||
|
* COPYRIGHT NOTICE
|
|||
|
* Copyright (c) 2020,<EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>
|
|||
|
* All rights reserved.
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD>824575535
|
|||
|
*
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>;<EFBFBD><EFBFBD>
|
|||
|
* <EFBFBD><EFBFBD>ӭ<EFBFBD><EFBFBD>λʹ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>뱣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD>İ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @file zf_vadc
|
|||
|
* @company <EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾
|
|||
|
* @author <EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>(QQ3184284598)
|
|||
|
* @version <EFBFBD>鿴doc<EFBFBD><EFBFBD>version<EFBFBD>ļ<EFBFBD> <EFBFBD>汾˵<EFBFBD><EFBFBD>
|
|||
|
* @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;
|
|||
|
}
|