2022-07-27 08:11 AM
Hi,
I have stm32wle5 and a circuit to measure current. stm32 will generate a PWM using timer1 ch1. The opamp's output is also connected to the ADC input of stm32. I want to measure the current (read ADC values) when PWM is on. Preferably in the middle of the PWM high edge. In most of the examples, people use internal triggers to trigger ADC. I thought as all channels of one timer have the same frequency, can I use one channel to output pwn and another channel as PWM generation without output just to trigger ADC? And set their duty_cycle and everything the same? I'm unsure if they are entirely synced and if this is the right solution. Do you have any other recommendations?
I also don't know how to do the measurement at the middle of PWM's high edge.
circuit:
timer1 and channel one config:
timer1 ch2 config:
ADC config:
Note that I don't do a continuous conversion and just want one or maybe a few samples and take the average in each PWM period.
Any help would be appreciated.
Thanks
UPDATE:
I want to do one single measurement every hour but do this when the MOSFET switch is on, e.g., PWM output is one.
2022-07-27 08:19 AM
Isn't PWM quite slow. So you should be able know what is happening. Or take continuous readings and find out when the current starts to rise.
2022-07-28 12:07 AM
I want to sense the current to find the change from the normal situation. And the PWM duty cycle will change in my code continuously. So, I think I cannot just measure the current all the time and figure out when it is rising. I cannot identify whether it is because of PWM's rising edge or the event I'm looking for.
I think this is a common current measurement circuit and should be a solution for it. Any one can help?
2022-07-28 10:29 AM
"
1 Same frequency.
2 No, duty cycle has to be 1/2 for channel - adc trigger. Depends on stm part number, some have "combine PWM" mode, some does not. TIM's w/o have all 4 channels in sync, adjusting duty cycle you can only change timing of the falling edge, so configure ADC trigger accordingly.
I 'd configure 2 channel in PWM-output mode and observe two signal on dual channel oscilloscope, than varying phase one of the PWM observe ADC readings.
3 This is the best solution, read more in ST app note AN4449
2022-07-28 11:17 AM
Thanks. I'll try this.
2022-08-03 05:54 AM
I tested this but don't get stable values, especially in high frequencies. I added an "update" section to my question. Does this change anything? I want to measure the current every hour, but when PWM is 1.
Can I just check if the PWM value is one and then do the measurement? Not sure about the delays here if something like an if statement can be a solution.
2022-08-03 06:45 AM
Better to measure each PWM cycle, transfer data to memory using DMA. Than when 1-4k Bytes buffer is full, you can run averaging algorithm to reduce noise. 1k data could provide 5 additional bits accuracy to the result value.
I have no experience with stm32wl product line, but there are examples in CubeMX, check it out:
https://github.com/STMicroelectronics/STM32CubeWL/tree/main/Projects/NUCLEO-WL55JC/Examples/ADC