2022-01-14 05:39 AM
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?
2022-01-14 06:17 AM
Which pins exactly, both PWM and ADC?
What is the ADC readout periodicity, what is the sampling time, how exactly is reference set, etc.?
JW
2022-01-14 06:20 AM
PA0 for PWM output and PA2/PA3 for ADC input.
2022-01-14 07:03 AM
And what about external hardware:
2022-01-14 07:20 AM
ADC is connected to an active mode phototransistor circuit (Vout in the image):
PWM and ADC traces are close to each other only at some points and mostly due to the MCU package. I initially thought about crosstalk between them, but 10kOhm (Re) pull-downs on ADC pins should have helped with it. There is no RC filter and a phototransistor signal is not expected to be noisy anyway.
PWM frequency is 50kHz, but, here is the interesting part, the duty cycle is set to 100%, so there should have been no swithching between HIGH and LOW in my tests.
The ADC sampling frequency is 1.6MHz, but that's not taking into account the interrupt code.
2022-01-17 01:32 AM
If the PWM is inactive (better check pins on scope), it's probably no hardware problem.
On the other hand, depending on the phototransistor, it seems very bold to connect it directly to the STM's ADC input.
To finally check if the problem is related to external circuitry, I would bridge the pull-down R with 0 Ohm, or even connect the ADC input to GND very close to the pin.
Better remove the phototransistor before doing that!
2022-01-17 03:34 AM
I've checked the PWM output pin, it is truly at 100% duty cycle. I've also checked that the duty cycle can be reduced to other values. It is possible, so PWM works properly. However, it doesn't change the situation, ADC still detects max values from time to time.
I've also removed phototransistors on both ADC pins and replaced Re with jumper wires. The result is the same. However, it's not possible to connect ADC inputs to ground really close to the pins due to the layout. The distance to Re is ~2cm.
It would be nice, if someone could check this issue with their own PCBs, although I understand that chances for this are pretty low.
2022-01-17 03:42 AM
As a last test, I've commented out
// HAL_ADC_MspInit(&hadc1);
command, so no ADC pins should be configured and ADC can't receive any data from anywhere, however the issue is still there.
2022-01-21 04:40 AM
Hello,
Can you show the content of MX_ADC1_Init() and HAL_ADC_MspInit() ?
By the way, it should not be needed to call HAL_ADC_MspInit() because already called by HAL_ADC_Init().
2022-01-21 04:50 AM
> It would be nice, if someone could check this issue with their own PCBs,
Conversely, you can try to do it on a "known good" board such as Nucleo or EVAL.
JW