对ADC1的结构体AdcHandle1赋值并初始化后,想要继续初始化ADC2的AdcHandle2,想着大部分是一样的,可以直接拷贝AdcHandle1,也就是
AdcHandle2 = AdcHandle1;
但ADC2却不起作用,经过一番debug,发现不能在AdcHandle1初始化后使用AdcHandle1赋值AdcHandle2,应该是初始化ADC1的代码部分对AdcHandle1的某些成员进行了修改。
可以将
AdcHandle2 = AdcHandle1;
放到赋值完 AdcHandle1并在初始化ADC1的函数HAL_ADC_Init()之前。
安全和规范起见,也可以像cubeMX生成的代码一样,每一个外设的结构体都按规范全部重写一遍。
不使用结构体直接赋值的方式。
static void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/*----------------------------ADC1-------------------------------*/
AdcHandle1.Instance = ADC1;
AdcHandle1.Init.ScanConvMode = ENABLE;
AdcHandle1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
AdcHandle1.Init.NbrOfConversion = 4;
AdcHandle1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONVHRTIM_TRG1;
AdcHandle1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
AdcHandle1.Init.DMAContinuousRequests = ENABLE;
AdcHandle2 = AdcHandle1;
if (HAL_ADC_Init(&AdcHandle1) != HAL_OK){Error_Handler();}
//-------------------------ADC1通道配置----------------------------- 通道1、2、3、4
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES_5; //采样时间加长
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&AdcHandle1, &sConfig) != HAL_OK){Error_Handler();}
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&AdcHandle1, &sConfig) != HAL_OK){Error_Handler();}
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&AdcHandle1, &sConfig) != HAL_OK){Error_Handler();}
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&AdcHandle1, &sConfig) != HAL_OK){Error_Handler();}
if (HAL_ADCEx_Calibration_Start(&AdcHandle1, ADC_SINGLE_ENDED) != HAL_OK){Error_Handler();}
if (HAL_ADC_Start_DMA(&AdcHandle1, (uint32_t *)&ADC1_Value, 4) != HAL_OK){Error_Handler();}
/*----------------------------ADC2-------------------------------*/
AdcHandle2.Instance = ADC2;
AdcHandle2.Init.NbrOfConversion = 4;
// AdcHandle2.Instance = ADC2;
// AdcHandle2.Init.ScanConvMode = ENABLE; //ADC_SCAN_ENABLE /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
// AdcHandle2.Init.EOCSelection = ADC_EOC_SEQ_CONV; //ADC_ISR_EOS
// AdcHandle2.Init.NbrOfConversion = 4; //转换通道
// AdcHandle2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONVHRTIM_TRG1; /* Software start to trig the 1st conversion manually, without external event */
// AdcHandle2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
// AdcHandle2.Init.DMAContinuousRequests = ENABLE;
if (HAL_ADC_Init(&AdcHandle2) != HAL_OK) { Error_Handler(); }
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
if (HAL_ADC_ConfigChannel(&AdcHandle2, &sConfig) != HAL_OK) { Error_Handler(); }
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&AdcHandle2, &sConfig) != HAL_OK) { Error_Handler(); }
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&AdcHandle2, &sConfig) != HAL_OK) { Error_Handler(); }
sConfig.Channel = ADC_CHANNEL_12;
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&AdcHandle2, &sConfig) != HAL_OK) { Error_Handler(); }
if (HAL_ADCEx_Calibration_Start(&AdcHandle2, ADC_SINGLE_ENDED) != HAL_OK) { Error_Handler(); }
if (HAL_ADC_Start_DMA(&AdcHandle2, (uint32_t *)&ADC2_Value, 4) != HAL_OK) { Error_Handler(); }
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)