When PWM output through TIM2 channel 1 on STM32G061G6U6 is enabled, the data register of ADC1 sometimes stores high values (~4095) even if there is no signal being fed to it.
Here is the configuration I use:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM2_Init();
// configure PWM
__HAL_TIM_SET_AUTORELOAD(&htim2, 1000);
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1001);
// start PWM
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
// configure ADC
HAL_ADC_MspInit(&hadc1);
// start ADC in interrupt mode
HAL_ADC_Start_IT(&hadc1);
while (1)
{
// some code
}
// callback for ADC interrupts
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
// some code
}When lines 11 and 14 are not commented out (i.e. compare register is configured and PWM is enabled), the ADC1 sometimes reads high values with no signal being fed to it (ADC input pins are pulled down, so there is no floating voltage). When lines 11 and 14 are commented out, there is no such issue. Also, by changing the TIM2 PWM channel to any other one, the issue dissapers. This was tested on 2 MCUs.
Does anyone have any ideas why TIM2 channel 1 PWM might interfere with ADC readings?