S32 Design Studio PE工具配置ADC

工具配置

我这个K1芯片有两个ADC驱动,也就有两个components,点开之后每个components都有四个选项卡converter转换器、channel通道、compare比较器、average求平均。

配置引脚

配置之前,得先配置好引脚,哪个引脚用来采集ADC。

每个components会生成两个文件,譬如我这里的adConv0会生成一个adConv0.c和一个adConv0.h,adConv1会生成一个adConv1.c和一个adConv1.h。

配置时钟

每个转换器有自己的基础时钟,譬如我们这里配个8M的,到ADC里面会继续分频。

converter转换器

一个转换器生成一个adc_converter_config_t类型的结构体。

/*! adConv0 configuration structure */
const adc_converter_config_t adConv0_ConvConfig0 = {.clockDivide = ADC_CLK_DIVIDE_4,.sampleTime = 255U,.resolution = ADC_RESOLUTION_12BIT,.inputClock = ADC_CLK_ALT_1,.trigger = ADC_TRIGGER_SOFTWARE,.pretriggerSel = ADC_PRETRIGGER_SEL_PDB,.triggerSel = ADC_TRIGGER_SEL_PDB,.dmaEnable = false,.voltageRef = ADC_VOLTAGEREF_VREF,.continuousConvEnable = false,.supplyMonitoringEnable = false,
};

分频可以选择1/2/4/8分频

/*!* @brief Clock Divider selection** Implements : adc_clk_divide_t_Class*/
typedef enum
{ADC_CLK_DIVIDE_1 = 0x00U,   /*!< Input clock divided by 1. */ADC_CLK_DIVIDE_2 = 0x01U,   /*!< Input clock divided by 2. */ADC_CLK_DIVIDE_4 = 0x02U,   /*!< Input clock divided by 4. */ADC_CLK_DIVIDE_8 = 0x03U    /*!< Input clock divided by 8. */
} adc_clk_divide_t;

采样时间就是采样周期,有个计数器不断累加,一到达特定次数就进行一次采样,并且将次数清零,最小1最大255。

精度可以选择8/10/12位精度,精度越高,转换时间越长。

/*!* @brief Conversion resolution selection** Implements : adc_resolution_t_Class*/
typedef enum
{ADC_RESOLUTION_8BIT = 0x00U,    /*!< 8-bit resolution mode */ADC_RESOLUTION_12BIT = 0x01U,   /*!< 12-bit resolution mode */ADC_RESOLUTION_10BIT = 0x02U    /*!< 10-bit resolution mode */
} adc_resolution_t;

输入时钟是在时钟树里面确定的,我这里只有一个可以选,其他的都只是个标签。

/*!* @brief Input clock source selection** Implements : adc_input_clock_t_Class*/
typedef enum
{ADC_CLK_ALT_1 = 0x00U,  /*!< Input clock alternative 1. */ADC_CLK_ALT_2 = 0x01U,  /*!< Input clock alternative 2. */ADC_CLK_ALT_3 = 0x02U,  /*!< Input clock alternative 3. */ADC_CLK_ALT_4 = 0x03U   /*!< Input clock alternative 4. */
} adc_input_clock_t;

触发源就是选择软件触发还是硬件触发,硬件触发有电平、边沿,软件有定时器溢出、PWM计数器溢出、ADC采集完毕等等。

前触发选择,也叫预触发,还有触发选择,这两个默认就行。

/*!* @brief Pretrigger types selectable from Trigger Latching and Arbitration Unit** Implements : adc_pretrigger_sel_t_Class*/
typedef enum
{ADC_PRETRIGGER_SEL_PDB     = 0x00U,   /*!< PDB pretrigger selected. */ADC_PRETRIGGER_SEL_TRGMUX  = 0x01U,   /*!< TRGMUX pretrigger selected. */ADC_PRETRIGGER_SEL_SW      = 0x02U    /*!< Software pretrigger selected. */
} adc_pretrigger_sel_t;
/*!* @brief Trigger source selectable from Trigger Latching and Arbitration Unit** Implements : adc_trigger_sel_t_Class*/
typedef enum
{ADC_TRIGGER_SEL_PDB        = 0x00U,   /*!< PDB trigger selected. */ADC_TRIGGER_SEL_TRGMUX     = 0x01U    /*!< TRGMUX trigger selected. */
} adc_trigger_sel_t;

参考电压可以选择MCU供电电压还是外部输入的参考电压,默认都是MCU供电电压,也就是第一个。

/*!* @brief Voltage reference selection** Implements : adc_voltage_reference_t_Class*/
typedef enum
{ADC_VOLTAGEREF_VREF = 0x00U,    /*!< VrefH and VrefL as Voltage reference. */ADC_VOLTAGEREF_VALT = 0x01U     /*!< ValtH and ValtL as Voltage reference. */
} adc_voltage_reference_t;

Channel通道

每个ADC转换器都对应好几个通道,自己配置对应到哪个通道就行,每个通道都有自己的引脚。一般都设置为只读,不启动中断。

这里每一个通道就生成一个通道结构体

const adc_chan_config_t adConv0_ChnConfig0 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT2,
};const adc_chan_config_t adConv0_ChnConfig1 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT6,
};const adc_chan_config_t adConv0_ChnConfig2 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT8,
};const adc_chan_config_t adConv0_ChnConfig3 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT9,
};const adc_chan_config_t adConv0_ChnConfig4 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT12,
};const adc_chan_config_t adConv0_ChnConfig5 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT13,
};const adc_chan_config_t adConv0_ChnConfig6 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT14,
};const adc_chan_config_t adConv0_ChnConfig7 = {.interruptEnable = false,.channel = ADC_INPUTCHAN_EXT15,
};

compare对比器

对比器很少用,因为一开就是一整个ADC所有通道都用上,也就是每个通道是或的关系,满足比较条件后,产生中断。具体细节可以看后面的接口使用。

生成代码

const adc_compare_config_t adConv0_HwCompConfig0 = {.compareEnable = true,.compareGreaterThanEnable = true,.compareRangeFuncEnable = true,.compVal1 = 10000U,.compVal2 = 20000U,
};

average求平均

可以用来硬件滤波,可以

生成代码

const adc_average_config_t adConv0_HwAvgConfig0 = {.hwAvgEnable = false,.hwAverage = ADC_AVERAGE_4,
};

接口使用

ADC_DRV_InitConverterStruct

初始化转换器

/*FUNCTION************************************************************************ Function Name : ADC_DRV_InitConverterStruct* Description   : This function initializes the members of the adc_converter_config_t* structure to default values (Reference Manual resets). This function should be called* on a structure before using it to configure the converter (ADC_DRV_ConfigConverter), otherwise all members* must be written (initialized) by the caller. This function insures that all members are written* with safe values, so the user can modify only the desired members.** Implements : ADC_DRV_InitConverterStruct_Activity*END**************************************************************************/
void ADC_DRV_InitConverterStruct(adc_converter_config_t * const config)
{DEV_ASSERT(config != NULL);config->clockDivide    = ADC_CLK_DIVIDE_1;config->sampleTime     = (uint8_t)ADC_DEFAULT_SAMPLE_TIME;config->resolution     = ADC_RESOLUTION_8BIT;config->inputClock     = ADC_CLK_ALT_1;config->trigger        = ADC_TRIGGER_SOFTWARE;config->pretriggerSel  = ADC_PRETRIGGER_SEL_PDB;config->triggerSel     = ADC_TRIGGER_SEL_PDB;config->dmaEnable      = false;config->voltageRef     = ADC_VOLTAGEREF_VREF;config->continuousConvEnable   = false;config->supplyMonitoringEnable = false;
}

ADC_DRV_ConfigConverter

将转换器配置到ADC

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ConfigConverter* Description   : This function configures the ADC converter with the options* provided in the configuration structure.** Implements : ADC_DRV_ConfigConverter_Activity*END**************************************************************************/
void ADC_DRV_ConfigConverter(const uint32_t instance,const adc_converter_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);/* Some alternative clocks can be unavailable depending on the device */DEV_ASSERT(config->inputClock <= NUMBER_OF_ALT_CLOCKS);ADC_Type * const base = s_adcBase[instance];clock_names_t adc_clocks[ADC_INSTANCE_COUNT] = ADC_CLOCKS;uint32_t adc_freq = 0u;status_t clk_status = CLOCK_SYS_GetFreq(adc_clocks[instance], &adc_freq);DEV_ASSERT(clk_status == STATUS_SUCCESS);(void) clk_status;adc_freq = adc_freq / (uint32_t)(1UL << ((uint32_t)(config->clockDivide)));DEV_ASSERT((adc_freq >= ADC_CLOCK_FREQ_MIN_RUNTIME) && (adc_freq <= ADC_CLOCK_FREQ_MAX_RUNTIME));ADC_SetClockDivide(base, config->clockDivide);ADC_SetSampleTime(base, config->sampleTime);ADC_SetResolution(base, config->resolution);ADC_SetInputClock(base, config->inputClock);ADC_SetTriggerMode(base, config->trigger);ADC_SetPretriggerSelect(instance, config->pretriggerSel);ADC_SetTriggerSelect(instance, config->triggerSel);ADC_SetDMAEnableFlag(base, config->dmaEnable);ADC_SetVoltageReference(base, config->voltageRef);ADC_SetContinuousConvFlag(base, config->continuousConvEnable);/* Supply monitoring is only available for ADC 0. */DEV_ASSERT((config->supplyMonitoringEnable == false) || (instance == 0u));if(instance == 0u){SIM_Type * const simBase = SIM;ADC_SetSupplyMonitoringEnableFlag(simBase, config->supplyMonitoringEnable);}
}

ADC_DRV_GetConverterConfig

获取转换器配置

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetConverterConfig* Description   : This functions returns the current converter configuration in* the form of a configuration structure.** Implements : ADC_DRV_GetConverterConfig_Activity*END**************************************************************************/
void ADC_DRV_GetConverterConfig(const uint32_t instance,adc_converter_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);const ADC_Type * const base = s_adcBase[instance];config->clockDivide = ADC_GetClockDivide(base);config->sampleTime = ADC_GetSampleTime(base);config->resolution = ADC_GetResolution(base);config->inputClock = ADC_GetInputClock(base);config->trigger = ADC_GetTriggerMode(base);config->triggerSel = ADC_GetTriggerSelect(instance);config->pretriggerSel = ADC_GetPretriggerSelect(instance);config->dmaEnable = ADC_GetDMAEnableFlag(base);config->voltageRef = ADC_GetVoltageReference(base);config->continuousConvEnable = ADC_GetContinuousConvFlag(base);/* Supply monitoring is only available for ADC 0. */if(instance == 0u){const SIM_Type * const simBase = SIM;config->supplyMonitoringEnable = ((simBase->CHIPCTL & SIM_CHIPCTL_ADC_SUPPLYEN_MASK) != 0u) ? true : false;}else{config->supplyMonitoringEnable = false;}
}

ADC_DRV_Reset

将整个ADC重启和恢复成默认配置,里面会重置所有ADC寄存器。

/*FUNCTION************************************************************************ Function Name : ADC_DRV_Reset* Description   : This function writes all the internal ADC registers with* their Reference Manual reset values.** Implements : ADC_DRV_Reset_Activity*END**************************************************************************/
void ADC_DRV_Reset(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);ADC_Type * const baseAddr = s_adcBase[instance];uint8_t idx = 0U;for(idx = 0U; idx < ADC_SC1_COUNT; idx++){baseAddr->SC1[idx] = ADC_SC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_SC1_AIEN(0x00U);}baseAddr->CFG1 = ADC_CFG1_ADICLK(ADC_CLK_ALT_1) | ADC_CFG1_MODE(ADC_RESOLUTION_8BIT) | ADC_CFG1_ADIV(ADC_CLK_DIVIDE_1);baseAddr->CFG2 = ADC_CFG2_SMPLTS(ADC_DEFAULT_SAMPLE_TIME);for(idx = 0U; idx < ADC_CV_COUNT; idx++){baseAddr->CV[idx] = ADC_CV_CV(0U);}baseAddr->SC2 = ADC_SC2_REFSEL(ADC_VOLTAGEREF_VREF) | ADC_SC2_DMAEN(0x00U) | ADC_SC2_ACREN(0x00U) | ADC_SC2_ACFGT(0x00U) | ADC_SC2_ACFE(0x00U) |ADC_SC2_ADTRG(0x00U);baseAddr->SC3 = ADC_SC3_AVGS(ADC_AVERAGE_4) | ADC_SC3_AVGE(0x00U) | ADC_SC3_ADCO(0x00U) | ADC_SC3_CAL(0x00U);baseAddr->USR_OFS = ADC_USR_OFS_USR_OFS(0U);baseAddr->UG = ADC_UG_UG(ADC_DEFAULT_USER_GAIN);#if FEATURE_ADC_HAS_EXTRA_NUM_REGSfor(idx = 0U; idx < ADC_aSC1_COUNT; idx++){baseAddr->aSC1[idx] = ADC_aSC1_ADCH(ADC_INPUTCHAN_DISABLED) | ADC_aSC1_AIEN(0x00U);}
#endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */ADC_SetPretriggerSelect(instance, ADC_PRETRIGGER_SEL_PDB);ADC_SetTriggerSelect(instance, ADC_TRIGGER_SEL_PDB);ADC_DRV_SetSwPretrigger(instance, ADC_SW_PRETRIGGER_DISABLED);/* Reset ADC Supply Monitoring - available only for ADC 0 */if(instance == 0u){SIM_Type * const simBase = SIM;ADC_SetSupplyMonitoringEnableFlag(simBase, false);simBase->CHIPCTL &= ~SIM_CHIPCTL_ADC_SUPPLY_MASK;}
}

ADC_DRV_InitHwCompareStruct

初始化对比器

/*FUNCTION************************************************************************ Function Name : ADC_DRV_InitHwCompareStruct* Description   : This function initializes the Hardware Compare configuration* structure to default values (Reference Manual resets). This function should be* called before configuring the Hardware Compare feature (ADC_DRV_ConfigHwCompare),* otherwise all members must be written by the caller. This function insures that all* members are written with safe values, so the user can modify the desired members.** Implements : ADC_DRV_InitHwCompareStruct_Activity*END**************************************************************************/
void ADC_DRV_InitHwCompareStruct(adc_compare_config_t * const config)
{DEV_ASSERT(config != NULL);config->compareEnable = false;config->compareGreaterThanEnable = false;config->compareRangeFuncEnable = false;config->compVal1 = 0U;config->compVal2 = 0U;
}

ADC_DRV_ConfigHwCompare

将对比器配置到ADC里面

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ConfigHwCompare* Description   : This functions sets the configuration for the Hardware* Compare feature using the configuration structure.** Implements : ADC_DRV_ConfigHwCompare_Activity*END**************************************************************************/
void ADC_DRV_ConfigHwCompare(const uint32_t instance,const adc_compare_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);ADC_Type * const base = s_adcBase[instance];ADC_SetHwCompareEnableFlag(base, config->compareEnable);ADC_SetHwCompareGtEnableFlag(base, config->compareGreaterThanEnable);ADC_SetHwCompareRangeEnableFlag(base, config->compareRangeFuncEnable);ADC_SetHwCompareComp1Value(base, config->compVal1);ADC_SetHwCompareComp2Value(base, config->compVal2);
}

ADC_DRV_GetHwCompareConfig

获取比较器配置

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetHwCompareConfig* Description   : This function returns the configuration for the Hardware* Compare feature.** Implements : ADC_DRV_GetHwCompareConfig_Activity*END**************************************************************************/
void ADC_DRV_GetHwCompareConfig(const uint32_t instance,adc_compare_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);const ADC_Type * const base = s_adcBase[instance];config->compareEnable = ADC_GetHwCompareEnableFlag(base);config->compareGreaterThanEnable = ADC_GetHwCompareGtEnableFlag(base);config->compareRangeFuncEnable = ADC_GetHwCompareRangeEnableFlag(base);config->compVal1 = ADC_GetHwCompareComp1Value(base);config->compVal2 = ADC_GetHwCompareComp2Value(base);
}

ADC_DRV_InitHwAverageStruct

求平均器结构体初始化,这个感觉没啥用。

/*FUNCTION************************************************************************ Function Name : ADC_DRV_InitHwAverageStruct* Description   : This function initializes the Hardware Average configuration* structure to default values (Reference Manual resets). This function should be* called before configuring the Hardware Average feature (ADC_DRV_ConfigHwAverage),* otherwise all members must be written by the caller. This function insures that all* members are written with safe values, so the user can modify the desired members.** Implements : ADC_DRV_InitHwAverageStruct_Activity*END**************************************************************************/
void ADC_DRV_InitHwAverageStruct(adc_average_config_t * const config)
{DEV_ASSERT(config != NULL);config->hwAvgEnable = false;config->hwAverage = ADC_AVERAGE_4;
}

ADC_DRV_ConfigHwAverage

求平均器配置进ADC里面

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ConfigHwAverage* Description   : This function sets the configuration for the Hardware* Average feature.** Implements : ADC_DRV_ConfigHwAverage_Activity*END**************************************************************************/
void ADC_DRV_ConfigHwAverage(const uint32_t instance,const adc_average_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);ADC_Type * const base = s_adcBase[instance];ADC_SetHwAverageEnableFlag(base, config->hwAvgEnable);ADC_SetHwAverageMode(base, config->hwAverage);
}

ADC_DRV_GetHwAverageConfig

获取求平均器的配置

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetHwAverageConfig* Description   : This function returns the configuration for the Hardware* Average feature.** Implements : ADC_DRV_GetHwAverageConfig_Activity*END**************************************************************************/
void ADC_DRV_GetHwAverageConfig(const uint32_t instance,adc_average_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);const ADC_Type * const base = s_adcBase[instance];config->hwAvgEnable = ADC_GetHwAverageEnableFlag(base);config->hwAverage = ADC_GetHwAverageMode(base);
}

ADC_DRV_InitChanStruct

初始化通道结构体,这个也没啥用。

/*FUNCTION************************************************************************ Function Name : ADC_DRV_InitChanStruct* Description   : This function initializes the control channel* configuration structure to default values (Reference Manual resets). This function should* be called on a structure before using it to configure a channel (ADC_DRV_ConfigChan), otherwise* all members must be written by the caller. This function insures that all members are written* with safe values, so the user can modify only the desired members.** Implements : ADC_DRV_InitChanStruct_Activity*END**************************************************************************/
void ADC_DRV_InitChanStruct(adc_chan_config_t * const config)
{DEV_ASSERT(config != NULL);config->interruptEnable = false;config->channel = ADC_INPUTCHAN_DISABLED;
}

ADC_DRV_ConfigChan

转换接口,这个一定会用上,每次转换都得用它。

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ConfigChan* Description   : This function sets a control channel configuration.** When Software Trigger mode is enabled, configuring control channel index 0,* implicitly triggers a new conversion on the selected ADC input channel.* Therefore, ADC_DRV_ConfigChan can be used for sw-triggering conversions.** Configuring any control channel while it is actively controlling a conversion* (sw or hw triggered) will implicitly abort the on-going conversion.** Implements : ADC_DRV_ConfigChan_Activity*END**************************************************************************/
void ADC_DRV_ConfigChan(const uint32_t instance,const uint8_t chanIndex,const adc_chan_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);DEV_ASSERT(config != NULL);ADC_Type * const base = s_adcBase[instance];/* ADC_INPUTCHAN_SUPPLY_ can only be used with ADC 0,* If used, the feature must be enabled separately via supplyMonitoringEnable flag in adc_converter_config_t. */DEV_ASSERT((instance == 0u) || ((uint32_t)config->channel < (uint32_t)ADC_INPUTCHAN_SUPPLY_VDD) || \((uint32_t)config->channel > (uint32_t)ADC_INPUTCHAN_SUPPLY_VDD_LV));ADC_SetInputChannel(base, chanIndex, config->channel, config->interruptEnable);
}

ADC_DRV_GetChanConfig

获取通道配置,也没啥用

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetChanConfig* Description   : This function returns the current configuration for a control* channel.** Implements : ADC_DRV_GetChanConfig_Activity*END**************************************************************************/
void ADC_DRV_GetChanConfig(const uint32_t instance,const uint8_t chanIndex,adc_chan_config_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);DEV_ASSERT(config != NULL);const ADC_Type * const base = s_adcBase[instance];config->interruptEnable = ADC_GetChanInterruptEnableFlag(base, chanIndex);config->channel = ADC_GetInputChannel(base, chanIndex);
}

ADC_DRV_SetSwPretrigger

设置转换器前触发

/*FUNCTION************************************************************************ Function Name : ADC_DRV_SetSwPretrigger* Description   : This function sets the software pretrigger - affects only first 4 control channels.** Implements : ADC_DRV_SetSwPretrigger_Activity*END**************************************************************************/
void ADC_DRV_SetSwPretrigger(const uint32_t instance,const adc_sw_pretrigger_t swPretrigger)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);SIM_Type * const simBase = SIM;uint32_t intermValue = 0U;uint32_t mask[ADC_INSTANCE_COUNT] = {0U};
#if (ADC_INSTANCE_COUNT == 1U)mask[0] = SIM_ADCOPT_ADC0SWPRETRG_MASK;
#elif (ADC_INSTANCE_COUNT == 2U)mask[0] = SIM_ADCOPT_ADC0SWPRETRG_MASK;mask[1] = SIM_ADCOPT_ADC1SWPRETRG_MASK;
#else
#error "Maximum supported value for ADC_INSTANCE_COUNT is 2."
#endif/* If SW Pretrigger Select is not enabled, the SW pretriggers will be ignored by ADC. */DEV_ASSERT((ADC_GetPretriggerSelect(instance) == ADC_PRETRIGGER_SEL_SW) || \(swPretrigger == ADC_SW_PRETRIGGER_DISABLED));intermValue = simBase->ADCOPT & (~ mask[instance]);switch(instance){case 0:intermValue |= SIM_ADCOPT_ADC0SWPRETRG(swPretrigger);break;case 1:intermValue |= SIM_ADCOPT_ADC1SWPRETRG(swPretrigger);break;default:DEV_ASSERT(false);break;}simBase->ADCOPT = intermValue;
}

ADC_DRV_WaitConvDone

等待转换完成,这个也必定用上。

/*FUNCTION************************************************************************ Function Name : ADC_DRV_WaitConvDone* Description   : This functions waits for a conversion to complete by* continuously polling the Conversion Active Flag.** Implements : ADC_DRV_WaitConvDone_Activity*END**************************************************************************/
void ADC_DRV_WaitConvDone(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);const ADC_Type * const base = s_adcBase[instance];while (ADC_GetConvActiveFlag(base) == true){/* Wait for conversion to finish */}
}

ADC_DRV_GetConvCompleteFlag

获取转换成功的标志位

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetConvCompleteFlag* Description   : This function returns the state of the Conversion Complete* flag for a control channel. This flag is set when a conversion is complete* or the condition generated by the Hardware Compare feature is evaluated to true.** Implements : ADC_DRV_GetConvCompleteFlag_Activity*END**************************************************************************/
bool ADC_DRV_GetConvCompleteFlag(const uint32_t instance,const uint8_t chanIndex)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(chanIndex < ADC_CTRL_CHANS_COUNT);const ADC_Type * const base = s_adcBase[instance];#if FEATURE_ADC_HAS_EXTRA_NUM_REGSuint32_t tmp = base->aSC1[chanIndex];tmp = (tmp & ADC_aSC1_COCO_MASK) >> ADC_aSC1_COCO_SHIFT;
#elseuint32_t tmp = base->SC1[chanIndex];tmp = (tmp & ADC_SC1_COCO_MASK) >> ADC_SC1_COCO_SHIFT;
#endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */return (tmp != 0u) ? true : false;
}

ADC_DRV_GetChanResult

获取转换成功结果

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetChanResult* Description   : This function returns the conversion result from a* control channel.** Implements : ADC_DRV_GetChanResult_Activity*END**************************************************************************/
void ADC_DRV_GetChanResult(const uint32_t instance,const uint8_t chanIndex,uint16_t * const result)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(result != NULL);const ADC_Type * const base = s_adcBase[instance];#if FEATURE_ADC_HAS_EXTRA_NUM_REGSDEV_ASSERT(chanIndex < ADC_aR_COUNT);uint32_t tmp = base->aR[chanIndex];tmp = (tmp & ADC_aR_D_MASK) >> ADC_aR_D_SHIFT;
#elseDEV_ASSERT(chanIndex < ADC_R_COUNT);uint32_t tmp = base->R[chanIndex];tmp = (tmp & ADC_R_D_MASK) >> ADC_R_D_SHIFT;
#endif /* FEATURE_ADC_HAS_EXTRA_NUM_REGS */*result = (uint16_t)tmp;
}

ADC_DRV_AutoCalibration

ADC自动标定

/*FUNCTION************************************************************************ Function Name : ADC_DRV_AutoCalibration* Description   : This functions executes an Auto-Calibration sequence. It* is recommended to run this sequence before using the ADC converter.* this function will check and reset clock divide based the adc frequency.* an error will be displayed if adc_freq too big* this function will set satisfy clock divide,start calibration.* final this function: restore adc clock divide,hardware average and trigger settings.** Implements : ADC_DRV_AutoCalibration_Activity*END**************************************************************************/
void ADC_DRV_AutoCalibration(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);ADC_Type * const base = s_adcBase[instance];/* set hardware average to maximum and set software trigger*/bool hwavgen = ADC_GetHwAverageEnableFlag(base);adc_average_t hwavg = ADC_GetHwAverageMode(base);adc_trigger_t trig = ADC_GetTriggerMode(base);uint8_t sampletime = ADC_GetSampleTime(base);ADC_SetHwAverageMode(base, ADC_AVERAGE_32);ADC_SetHwAverageEnableFlag(base, true);ADC_SetTriggerMode(base, ADC_TRIGGER_SOFTWARE);/* Set the sample time to the reset value because it affects thecalibration duration but not the results*/ADC_SetSampleTime(base, ADC_RESET_SAMPLE_TIME_VALUE);base->CLPS = 0x00u;base->CLP3 = 0x00u;base->CLP2 = 0x00u;base->CLP1 = 0x00u;base->CLP0 = 0x00u;base->CLPX = 0x00u;base->CLP9 = 0x00u;/*Set clock divider */clock_names_t adc_clocks[ADC_INSTANCE_COUNT] = ADC_CLOCKS;uint32_t adc_freq = 0u;adc_clk_divide_t adc_clk_divide_res = ADC_GetClockDivide(base);adc_clk_divide_t adc_clk_divide = ADC_CLK_DIVIDE_1;status_t clk_status = CLOCK_SYS_GetFreq(adc_clocks[instance], &adc_freq);DEV_ASSERT(clk_status == STATUS_SUCCESS);(void) clk_status;DEV_ASSERT(adc_freq >= ADC_CLOCK_FREQ_MIN_RUNTIME);if ((adc_freq / (uint32_t)(1UL << ((uint32_t)(adc_clk_divide_res)))) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U)){/* no action if adc_freq is satisfy */}else{if ((adc_freq / 2U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U)){adc_clk_divide = ADC_CLK_DIVIDE_2;}else if ((adc_freq / 4U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U)){adc_clk_divide = ADC_CLK_DIVIDE_4;}else if ((adc_freq / 8U) <= (ADC_CLOCK_FREQ_MAX_RUNTIME / 2U)){adc_clk_divide = ADC_CLK_DIVIDE_8;}else{/* frequency is greater than required clock for calibration */DEV_ASSERT(false);}ADC_SetClockDivide(base, adc_clk_divide);}/* start calibration */ADC_SetCalibrationActiveFlag(base, true);while (ADC_GetCalibrationActiveFlag(base)){/* Wait for calibration to finish */}/* restore adc clock divide*/ADC_SetClockDivide(base, adc_clk_divide_res);/* restore hardware average and trigger settings*/ADC_SetHwAverageEnableFlag(base, hwavgen);ADC_SetHwAverageMode(base, hwavg);ADC_SetTriggerMode(base, trig);ADC_SetSampleTime(base, sampletime);
}

ADC_DRV_InitUserCalibrationStruct

初始化自动标定结构体

/*FUNCTION************************************************************************ Function Name : ADC_DRV_InitUserCalibrationStruct* Description   : This function initializes the User Calibration configuration* structure to default values (Reference Manual resets). This function should be called* on a structure before using it to configure the User Calibration feature (ADC_DRV_ConfigUserCalibration),* otherwise all members must be written by the caller. This function insures that all members are written* with safe values, so the user can modify only the desired members.** Implements : ADC_DRV_InitUserCalibrationStruct_Activity*END**************************************************************************/
void ADC_DRV_InitUserCalibrationStruct(adc_calibration_t * const config)
{DEV_ASSERT(config != NULL);config->userGain = (uint16_t)ADC_DEFAULT_USER_GAIN;config->userOffset = (uint16_t)0U;
}

ADC_DRV_ConfigUserCalibration

将用户标定结构体配置进ADC里面

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ConfigUserCalibration* Description   : This function sets the configuration for the user calibration* registers.** Implements : ADC_DRV_ConfigUserCalibration_Activity*END**************************************************************************/
void ADC_DRV_ConfigUserCalibration(const uint32_t instance,const adc_calibration_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);ADC_Type * const base = s_adcBase[instance];ADC_SetUserGainValue(base, config->userGain);ADC_SetUserOffsetValue(base, config->userOffset);
}

ADC_DRV_GetUserCalibration

获取用户标定结构体

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetUserCalibration* Description   : This function returns the current user calibration* register values.** Implements : ADC_DRV_GetUserCalibration_Activity*END**************************************************************************/
void ADC_DRV_GetUserCalibration(const uint32_t instance,adc_calibration_t * const config)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT(config != NULL);const ADC_Type * const base = s_adcBase[instance];config->userGain = ADC_GetUserGainValue(base);config->userOffset = ADC_GetUserOffsetValue(base);
}

ADC_DRV_GetInterruptNumber

获取中断号

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetInterruptNumber* Description   : This function returns the interrupt number for the specified ADC instance.** Implements : ADC_DRV_GetInterruptNumber_Activity*END**************************************************************************/
IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);static const IRQn_Type adcIrqId[ADC_INSTANCE_COUNT] = ADC_IRQS;IRQn_Type irqId = adcIrqId[instance];return irqId;
}

ADC_DRV_ClearLatchedTriggers

清除触发标志位

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ClearLatchedTriggers* Description   : This function clears all trigger latched flags of the ADC instance.** Implements : ADC_DRV_ClearLatchedTriggers_Activity*END**************************************************************************/
void ADC_DRV_ClearLatchedTriggers(const uint32_t instance,const adc_latch_clear_t clearMode)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);DEV_ASSERT((clearMode == ADC_LATCH_CLEAR_WAIT) || (clearMode == ADC_LATCH_CLEAR_FORCE));ADC_Type * const base = s_adcBase[instance];if (clearMode == ADC_LATCH_CLEAR_FORCE){ADC_ClearLatchTriggers(base);}while (ADC_GetTriggerLatchFlags(base) != 0u){/* Wait for latched triggers to be processed */}
}

ADC_DRV_ClearTriggerErrors

清除触发错误标志位

/*FUNCTION************************************************************************ Function Name : ADC_DRV_ClearTriggerErrors* Description   : This function clears all trigger error flags of the ADC instance.** Implements : ADC_DRV_ClearTriggerErrors_Activity*END**************************************************************************/
void ADC_DRV_ClearTriggerErrors(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);ADC_Type * const base = s_adcBase[instance];base->SC2 |= ADC_SC2_TRGSTERR_MASK;
}

ADC_DRV_GetTriggerErrorFlags

获取触发错误标志位

/*FUNCTION************************************************************************ Function Name : ADC_DRV_GetTriggerErrorFlags* Description   : This function returns the trigger error flags bits of the ADC instance.** Implements : ADC_DRV_GetTriggerErrorFlags_Activity*END**************************************************************************/
uint32_t ADC_DRV_GetTriggerErrorFlags(const uint32_t instance)
{DEV_ASSERT(instance < ADC_INSTANCE_COUNT);const ADC_Type * const base = s_adcBase[instance];uint32_t trig_errors = (base->SC2 & ADC_SC2_TRGSTERR_MASK) >> ADC_SC2_TRGSTERR_SHIFT;return trig_errors;
}

一般用法

初始化的时候初始化ADC就可以了,顺带自动标定一下。

 void ADC_Init()
{ADC_DRV_ConfigConverter(INST_ADCONV0, &adConv0_ConvConfig0);ADC_DRV_AutoCalibration(INST_ADCONV0);
}

采集的时候三步走:转换对应通道采集到的值,等待转换完成,获取转换之后的值。

static uint16_t ADC_DRV_GetADCValue(const uint32_t instance,const adc_chan_config_t * const config)
{uint16_t adcRawValue;/* Configure ADC channel and software trigger a conversion */ADC_DRV_ConfigChan(instance, 0U, config);/* Wait for the conversion to be done */ADC_DRV_WaitConvDone(instance);/* Store the channel result into a local variable */ADC_DRV_GetChanResult(instance, 0U, &adcRawValue);return adcRawValue;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/729766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

亚洲股市下一步的关键:中国看财报、日本看汇率、韩国看治理、印度看基建

汇丰认为财报将是驱动中国股市走势的关键因素。目前市场预计2024年中国企业每股收益将增长16%。 日本央行转向、A股业绩复苏、印度基建、韩国市场改革......最近这段时间&#xff0c;亚洲各大市场涌现出了不同的交易主题。 汇丰银行指出&#xff0c;中国受到本土企业盈利能力…

域控操作八:下发打印机策略

、此方法可以直接下发打印机&#xff0c;加域OU后自动连接 1&#xff0c;在域控服务器上安装打印机确保域控可以让打印机使用 并且要开共享入目录 这个共享路径正常情况下应该是在运行里面输入是直接能连接打印机的

算法——链表王

链表刷题详解 203.移除链表元素 力扣题目链接(opens new window) 题意&#xff1a;删除链表中等于给定值 val 的所有节点。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff1a;[1,2,3,4,5] 示例 2&#xff1a; 输入&#xff1a;head []…

数据库(mysql)-新手笔记(主外键,视图)

主外键 主键(唯一性,非空性) 主键是数据库表中的一个或多个字段&#xff0c;其值唯一标识表中的每一行/记录。 唯一性: 主键字段中的每个值都必须是唯一的&#xff0c;不能有两个或更多的记录具有相同的主键值 非空性&#xff1a;主键字段不能包含NULL值。 外键(引用完整 …

AutoPSA里给定了弹簧刚度,为什么计算没有引用?

山东一用户问&#xff1a;已经给定了弹簧刚度&#xff0c;为什么计算没引用&#xff1f; 在AutoPSA里包含两种算法仿CAESARII &#xff0c;仿GLIF算法。 在仿CAESARII算法里直接给定弹簧刚度与安载荷载&#xff0c;两个都给了相应值&#xff0c;也就是给定了弹簧号&#xff1b…

处理error: remote origin already exists.及其Gitee文件上传保姆级教程

解决error: remote origin already exists.&#xff1a; 删除远程 Git 仓库 git remote rm origin 再添加远程 Git 仓库 git remote add origin &#xff08;HTTPS&#xff09; 比如这样&#xff1a; 然后再push过去就ok了 好多人可能还是不熟悉怎么将文件上传 Gitee:我…

Python实习生(自动化测试脚本开发) - 面经 - TCL新技术有限公司

JD&#xff1a; 招聘流程&#xff1a; 2024.1.3 Boss直聘 沟通 2024.1.4 约面 2024.1.6 上午面试 面试流程&#xff1a; 上来第一步&#xff0c;直接问Python基础语法&#xff0c;讲一下基础的数据类型 就记得元组和字典 分别具体说一下元组和字典 流程控制语句有哪些&…

应急布控球远程视频监控方案:视频监控平台EasyCVR+4G/5G应急布控球

随着科技的不断发展&#xff0c;应急布控球远程视频监控方案在公共安全、交通管理、城市管理等领域的应用越来越广泛。这种方案通过在现场部署应急布控球&#xff0c;实现对特定区域的实时监控&#xff0c;有助于及时发现问题、快速响应&#xff0c;提高管理效率。 智慧安防视…

SPFA找负环

2024-01-31&#xff08;最短路径&#xff09;-CSDN博客 求负环的常用方法&#xff0c;基于spfa&#xff1a; 1.统计每个点入队的次数&#xff0c;如果有个点入队n次&#xff0c;则说明存在负环 2.统计当前每个点的最短路中包含的边数&#xff0c;如果某个点的最短路的所包含的边…

QT:用opencv的KNN识别图片中的LED数字(一)

前言 一款功能测试的软件demo,使用了QT作为界面,主要使用了opencv的KNN识别,使用gstreamer作为管道,用来打开图片。后期会写一篇打开摄像头实时识别的文章。 (正在写,未完成,稍候) 效果一预览: 效果二预览: 效果三预览: 正在写。。。 设计思路 1. 软件UI设…

【uni-app】condition 启动模式配置,生产环境无效,仅开发期间生效

在小程序开发过程中&#xff0c;每次代码修改后&#xff0c;都会启动到首页&#xff0c;有时非常不方便&#xff0c;为了更高效的开发&#xff0c;有时需要模拟直接跳转到指定的页面&#xff0c; 操作方法如下&#xff1a; 在pages.joson里面配置下列代码&#xff1a; "…

Mybatis-Spring | Mybatis与Spring的“整合“

目录 : 一、配置环境1. 整合环境需导入的JAR :Spring框架所需JARMybatis框架所需JARMyBatis与Spring整合的中间JAR数据库驱动JAR包数据源所需JAR包 &#xff08;下面的例子中 : 用的不是这个数据源&#xff09; 2. 编写“配置文件” 和 “.properties文件” ( 只是概述&#xf…

Claude3真的超越GPT4了吗?

一文探究Claude3真实能力 Claude3就在昨天悄无声息的上线了&#xff0c;OpenAI的好兄弟Anthropic公司仅仅在推特上发了一条消息来宣布这件事情。 Anthropic这次一下就发了三个模型&#xff1a;Opus、Sonnet、Haiku。说实话这名字感觉取得不咋地&#xff0c;主要是看不懂&#x…

图机器学习(3)-面向节点的人工特征工程

0 问题引入 地铁导航图 计算机是看不懂这些图&#xff0c;计算机只能看懂向量、矩阵。 传统图机器学习只讨论连接特征。 构造一个新的特征 x 1 x 2 x_1x_2 x1​x2​&#xff0c;有利于分开这种数据。 人需要去翻译这些计算机不懂的特征&#xff0c;变成计算机可以懂…

javaSE-----继承和多态

目录 一.初识继承&#xff1a; 1.1什么是继承&#xff0c;为什么需要继承&#xff1a; 1.2继承的概念与语法&#xff1a; 二.成员的访问&#xff1a; 2.1super关键字 2.2this和super的区别&#xff1a; 三.再谈初始化: 小结&#xff1a; 四.初识多态&#xff1a; 4.1多…

CAS 登出方案

1.配置 CAS 服务器端 添加配置cas.logout.followServiceRedirects:true&#xff0c;使支持 CAS 退出时支持输入 service 参数为跳转路径 2.配置客户端服务,添加session清除操作 3.前端文件添加跳转重定向 1) 直接在客户端调用http请求/cas/logout去注销不能携带cookie信息, 无…

ATM系统(Java)

ATM系统&#xff08;Java&#xff09; 1、实现要求 实现基本的ATM系统功能&#xff0c;包括注册&#xff0c;登录&#xff0c;查询&#xff0c;取款&#xff0c;存款&#xff0c;以及修改密码等。 2、代码实现 2.1 Test package com.ham;public class Test {public static v…

jmap-各种option参数说明

基本情况 jmap&#xff08;JVM Memory Map&#xff09;&#xff1a;作用一方面是获取dump文件&#xff08;堆转储快照文件&#xff0c;二进制文件&#xff09;&#xff0c;它还可以获取目标Java进程的内存相关信息&#xff0c;包括Java堆各区域的使用情况、堆中对象的统计信息…

高清数学公式视频素材、科学公式和方程式视频素材下载

适用于科普、解说的自媒体视频剪辑素材&#xff0c;黑色背景数学、科学公式和方程式视频素材下载。 视频编码&#xff1a;H.264 | 分辨率&#xff1a;3840x2160 (4K) | 无需插件 | 文件大小&#xff1a;16.12MB 来自PR视频素材&#xff0c;下载地址&#xff1a;https://prmuban…

阿里云服务器怎么使用?3分钟搭建网站教程2024新版

使用阿里云服务器快速搭建网站教程&#xff0c;先为云服务器安装宝塔面板&#xff0c;然后在宝塔面板上新建站点&#xff0c;阿里云服务器网aliyunfuwuqi.com以搭建WordPress网站博客为例&#xff0c;来详细说下从阿里云服务器CPU内存配置选择、Web环境、域名解析到网站上线全流…