历史上的今天
今天是:2025年03月04日(星期二)
2019年03月04日 | STM32 三路AD的使用方法
2019-03-04 来源:eefocus
void ADC_ChannelSelect(uint32_t ADChannel)
{
// ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADChannel;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;//ADC_RANK_CHANNEL_NUMBER;//1;//ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
{
HAL_StatusTypeDef tmp_hal_status = HAL_OK;
__IO uint32_t wait_loop_index = 0U;
/* Check the parameters */
assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
assert_param(IS_ADC_CHANNEL(sConfig->Channel));
assert_param(IS_ADC_RANK(sConfig->Rank));
if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
{
assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
}
/* Process locked */
__HAL_LOCK(hadc);
/* Parameters update conditioned to ADC state: */
/* Parameters that can be updated when ADC is disabled or enabled without */
/* conversion on going on regular group: */
/* - Channel number */
/* - Channel sampling time */
/* - Management of internal measurement channels: VrefInt/TempSensor/Vbat */
if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
{
/* Configure channel: depending on rank setting, add it or remove it from */
/* ADC conversion sequencer. */
if (sConfig->Rank != ADC_RANK_NONE)
{
/* Regular sequence configuration */
/* Set the channel selection register from the selected channel */
if (sConfig->Channel == 0)
{
hadc->Instance->CHSELR = 0x01;
}
else if (sConfig->Channel == 4)
{
hadc->Instance->CHSELR = 0x10;
}
else if (sConfig->Channel == 6)
{
hadc->Instance->CHSELR = 0x40;
}
// hadc->Instance->CHSELR |= ADC_CHSELR_CHANNEL(sConfig->Channel);
/* Channel sampling time configuration */
/* Management of parameters "SamplingTimeCommon" and "SamplingTime" */
/* (obsolete): sampling time set in this function with */
/* parameter "SamplingTime" (obsolete) only if not already set into */
/* ADC initialization structure with parameter "SamplingTimeCommon". */
if (! IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon))
{
/* Modify sampling time if needed (not needed in case of reoccurrence */
/* for several channels programmed consecutively into the sequencer) */
if (sConfig->SamplingTime != ADC_GET_SAMPLINGTIME(hadc))
{
/* Channel sampling time configuration */
/* Clear the old sample time */
hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
/* Set the new sample time */
hadc->Instance->SMPR |= ADC_SMPR_SET(sConfig->SamplingTime);
}
}
/* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
/* internal measurement paths enable: If internal channel selected, */
/* enable dedicated internal buffers and path. */
/* Note: these internal measurement paths can be disabled using */
/* HAL_ADC_DeInit() or removing the channel from sequencer with */
/* channel configuration parameter "Rank". */
if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
{
/* If Channel_16 is selected, enable Temp. sensor measurement path. */
/* If Channel_17 is selected, enable VREFINT measurement path. */
/* If Channel_18 is selected, enable VBAT measurement path. */
ADC->CCR |= ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
/* If Temp. sensor is selected, wait for stabilization delay */
if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
{
/* Delay for temperature sensor stabilization time */
/* Compute number of CPU cycles to wait for */
wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
while(wait_loop_index != 0U)
{
wait_loop_index--;
}
}
}
}
else
{
/* Regular sequence configuration */
/* Reset the channel selection register from the selected channel */
hadc->Instance->CHSELR &= ~ADC_CHSELR_CHANNEL(sConfig->Channel);
/* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
/* internal measurement paths disable: If internal channel selected, */
/* disable dedicated internal buffers and path. */
if(ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
{
/* If Channel_16 is selected, disable Temp. sensor measurement path. */
/* If Channel_17 is selected, disable VREFINT measurement path. */
/* If Channel_18 is selected, disable VBAT measurement path. */
ADC->CCR &= ~ADC_CHANNEL_INTERNAL_PATH(sConfig->Channel);
}
}
}
/* If a conversion is on going on regular group, no update on regular */
/* channel could be done on neither of the channel configuration structure */
/* parameters. */
else
{
/* Update ADC state machine to error */
SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
tmp_hal_status = HAL_ERROR;
}
/* Process unlocked */
__HAL_UNLOCK(hadc);
/* Return function status */
return tmp_hal_status;
}
上一篇:STM32的CRC计算
史海拾趣
|
这两天出差到南京,就感觉到江苏人民可真是幸福,用江苏电信的E9套餐算算,去掉赠送的600分钟+400分钟这一摊固话和手机通话费(挺复杂,送的特别多,这些话费一毛一分钟都已经有小一百块钱了),几乎是一天一块钱宽带了,可真是让偶介个首都人民眼 ...… 查看全部问答> |
|
现在PCI数据线与CPLD相连,有一个功能是测速,首先用LAD0发一个控制信号,然后如入LAD[15..0]的数据。 由于LAD0需要双向,因此将LAD[15..0]都设为Bir(在顶层框图中)但是发现通过LAD0根本写入不了命令,不知为什么? 现在只能LAD0只作输入 其余1 ...… 查看全部问答> |
|
有网友试过,效果确实不错:宿舍窗台上(11楼)不到20秒,显示了时间,再过几秒,就显示了经纬度,没调,就收到4颗心!! 材料: 同轴线 直径1mm的漆包线200mm以上 敷铜板:长:100mm; 宽:100mm; 敷铜条 ...… 查看全部问答> |
|
测试设置 此测试采用与前一个测试完全同样的脚本,只是这次包括了TSP触发模型。此触发模型使用了2602与3706灵活性高、功能强大的数字I/O控制。 下列指令通过触发模型提高了吞吐量: 测试结果 取决于DUT,此方法能在一次 ...… 查看全部问答> |
|
各位大虾,小弟最近在写程序时会经常调用双口RAM,双口RAM的数据读写并不需要同时进行,只有在数据写入RAM之后才会读取RAM中的数据,由于实际应用对安全性要求特别高,而且要长时间应用,我怕RAM不能及时更新数据或者出现其他故障,请教各位有没有 ...… 查看全部问答> |




