GonzoG wrote: Mon Jan 16, 2023 12:08 am
In STM examples you have example that reads frequency and duty cycle.
I have tried that example, changing the pin to the pin I use on my board (it was a gift) and it does not work.
Code: Select all
[19:22:28:154] Frequency = 0 Dutycycle = 0
[19:22:29:161] Frequency = 0 Dutycycle = 0
[19:22:30:152] Frequency = 0 Dutycycle = 0
[19:22:31:169] Frequency = 0 Dutycycle = 0
[19:22:32:150] Frequency = 0 Dutycycle = 0
I can read the duty cycle with other Arduino codes but it is not a stable value and changes constantly, and if I change the frequency, it takes seconds to detect the new duty cycle.
doubleaswi wrote: Mon Jan 16, 2023 10:32 am
There are a few different ways you could approach measuring the duty cycle of a PWM signal without using the pulseIn() function, which can be affected by changes in frequency.
One approach is to use a timer to capture the rising and falling edges of the PWM signal, and then calculate the duty cycle based on the time between those edges. The attachInterrupt() function can be used to trigger an interrupt when the PWM signal changes state, and the micros() function can be used to get the current time in microseconds.
Another approach could be to use a hardware timer to measure the high and low time of the PWM signal and calculate the duty cycle. This can be done by configuring the timer to count the number of clock cycles during the high and low periods of the PWM signal.
You can also use a library like the Arduino-PWM library which allows you to measure the duty cycle of the signal.
In any case, you'll need to understand the basics of how timers and interrupts work on the STM32 microcontroller. I recommend starting with the official documentation and examples from the STM32 website, as well as searching for tutorials and example code online.
It will also be helpful to have a good understanding of the PWM signal you're working with, such as the frequency and the resolution of the PWM signal.
Finally, you could also use a logic analyzer to monitor the signal, this way you could see the exact time of the high and low states of the signal and calculate the duty cycle accordingly.
I hope this helps and good luck with your project!
_____________________________
https://www.rst.software/blog/chat-app- ... ry-in-2023
I can't use an interrupt, I already detect some pulses with an interrupt and it is possible that it affects this routine (it is more important).
I tried to use a hardware timer, but it affected the use of another interrupt that triggers another timer (the one that triggers the interrupt I mentioned, to disable a few microseconds later an output) and I had to disable it.
I tried the example of reading the PWM signal on an empty sketch, but it didn't work for me.
I have tried several codes that I have found on the internet, that use millis() or micros() to read the states of the input, but only one has worked, but it does not give me stable values, each cycle fluctuates and if I change the working frequency (I am trying with a signal generator), it takes seconds to detect it.
Code: Select all
const int pin = PB0;
unsigned long highDuration = 0;
unsigned long lowDuration = 0;
unsigned long sampleDuration = 10000;
unsigned long prevMicros = 0;
bool prevState = LOW;
void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
prevMicros = micros();
}
void loop() {
if (micros() - prevMicros >= sampleDuration) {
unsigned long currentMicros = micros();
highDuration += digitalRead(pin) == HIGH ? currentMicros - prevMicros : 0;
lowDuration += digitalRead(pin) == LOW ? currentMicros - prevMicros : 0;
prevMicros = currentMicros;
float totalDuration = highDuration + lowDuration;
float dutyCycle = 100.0 * highDuration / totalDuration;
Serial.println(dutyCycle);
}
// Other code
delay(25);
}
Thanks!
Regards