用STM32L052采样AD,两个通道,其中一个通道要用过采样技术,通道的切换是在AD中断进行。请问是否每次都要校准HAL_ADCEx_Calibration_Start()?而每次AD中断中,参数设置的时候,是否都要关中断HAL_ADC_Stop_IT() ,参数设置完在开中断HAL_ADC_Start_IT()? /* ADC2 init function */ void MX_ADC2_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc.Instance = ADC1; hadc.Init.OversamplingMode = DISABLE; hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; hadc.Init.Resolution = ADC_RESOLUTION12b; hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ContinuousConvMode = DISABLE; hadc.Init.DiscontinuousConvMode = ENABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.EOCSelection = EOC_SINGLE_CONV; hadc.Init.Overrun = OVR_DATA_PRESERVED; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerFrequencyMode = ENABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; HAL_ADC_Init(&hadc); /**Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_2; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; HAL_ADC_ConfigChannel(&hadc, &sConfig); } /* ADC7 init function */ void MX_ADC7_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc.Instance = ADC1; hadc.Init.OversamplingMode = ENABLE; hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128; hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3; hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; hadc.Init.Resolution = ADC_RESOLUTION12b; hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ContinuousConvMode = DISABLE; hadc.Init.DiscontinuousConvMode = ENABLE; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.EOCSelection = EOC_SINGLE_CONV; hadc.Init.Overrun = OVR_DATA_PRESERVED; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerFrequencyMode = ENABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; HAL_ADC_Init(&hadc); /**Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_7; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; HAL_ADC_ConfigChannel(&hadc, &sConfig); } |
STM32L011D4芯片用SWD无法下载程序
求助:STM32L0系列标准库哪里下?
STM32L051低温时LPUART串口波特率异常
STM32L051单片机ADC如何彻底关闭?
STM32L051C8进入低功耗模式电流偏大
STM32L052C8T6通过I2C模拟读16位数
STM32L0的VLCD外接5V会有问题吗
求助!!使用STM32L073 IO口模拟IIC接口读写AT24CM01 程序不停...
STM32L072RB写Bank2 EEPROM不响应中断
STM32L031无法进入boot
设置 的时候必须关掉中断 设置完成才开启中断。运行ad转换
至于你中断处理时是否要关闭相关中断不该有什么硬性规定,你自己最清楚,关于不关会带来什么影响或后果。
设置 的时候必须关掉中断
设置完成才开启中断。
运行ad转换
现在是通道7,可以切到通道2
但通道2,切不到通道7
/* ADC init function */
void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = ENABLE;
hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128;
hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3;
hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = EOC_SINGLE_CONV;
hadc.Init.Overrun = OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
HAL_ADC_Init(&hadc);
/* USER CODE BEGIN 7 */
if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
}
/* USER CODE END 7 */
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
/* ADC2 init function */
void MX_ADC2_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = ENABLE;
hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128;
hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3;
hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;//轮流对各个通道进行采集
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;//数据对齐方式
hadc.Init.ContinuousConvMode = ENABLE;//连续转换模式
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;//控制触发模式
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = EOC_SINGLE_CONV;
hadc.Init.Overrun = OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
HAL_ADC_Init(&hadc);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
/* ADC7 init function */
void MX_ADC7_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = ENABLE;
hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128;
hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3;
hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = EOC_SINGLE_CONV;
hadc.Init.Overrun = OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
HAL_ADC_Init(&hadc);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
/* Get the converted value of regular channel */
uwADCxConvertedValue = HAL_ADC_GetValue(AdcHandle);
HAL_ADC_Stop_IT(&hadc);
if(CH_dp==0)
{
CH_dp=1;
MX_ADC2_Init();
}
else
{
CH_dp=0;
MX_ADC7_Init();
}
HAL_ADC_Start_IT(&hadc);
HAL_UART_Transmit_IT(&huart1, (uint8_t*)uwADCxConvertedValue, 4);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC_Init();
MX_COMP1_Init();
MX_TIM21_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_COMP_Start(&hcomp1);
HAL_ADC_Start_IT(&hadc);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
可以了
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
/* Get the converted value of regular channel */
uwADCxConvertedValue = HAL_ADC_GetValue(AdcHandle);
HAL_ADC_Stop_IT(&hadc);
if(CH_dp==0)
{
CH_dp=1;
ADC1->CHSELR = ADC_CHANNEL_2;
}
else
{
CH_dp=0;
ADC1->CHSELR = ADC_CHANNEL_7;
}
HAL_ADC_Start_IT(&hadc);
HAL_UART_Transmit_IT(&huart1, (uint8_t*)uwADCxConvertedValue, 4);
}