Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWM using analogWrite sometimes stops #59

Open
ktand opened this issue Apr 3, 2018 · 1 comment
Open

PWM using analogWrite sometimes stops #59

ktand opened this issue Apr 3, 2018 · 1 comment

Comments

@ktand
Copy link
Contributor

ktand commented Apr 3, 2018

When the PWM interrupt handler is delayed by another interrupt using the same or higher priority the PWM can stop working for a long time. The problem can be reproduced using the following example:

#include <HardwareTimer.h>

#define PWM_PIN_1 PC7
#define PWM_PIN_2 PC6
#define TIMER_PIN PC8

void interrupt()
{
    digitalWrite(TIMER_PIN, 1);
    for (int i = 0; i < 2048; i++)
    {
        __asm__("nop;");
    }
    digitalWrite(TIMER_PIN, 0);
}

void setup()
{
    pinMode(TIMER_PIN, 1);

    Timer1.setPeriod(5000);
    Timer1.attachInterrupt(interrupt);
    Timer1.resume();

    analogWrite(PWM_PIN_1, 128);
}

byte ratio = 0;

void loop()
{ 
    analogWrite(PWM_PIN_2, ratio++);

    delay(50);
}

This screenshot shows that the PWM1 & PWM2 signals stop while the TIMER signal continues to pulse.

2018-04-03 19_35_23-saleae logic software

@ktand ktand changed the title PWM using analogWrite can stop under certain circumstances PWM using analogWrite somtimes stops Apr 3, 2018
@ktand ktand changed the title PWM using analogWrite somtimes stops PWM using analogWrite sometimes stops Apr 3, 2018
@mr-miky
Copy link

mr-miky commented May 30, 2018

I do not know if the problem I'm experiencing is connected to this but strange things happen to the pwm output even if you change the duty cycle during the operation of the pwm. I noticed that if you decrease the d.c. normally nothing happens but if you increase d.c. the pwm signal disappears and may reappear after several seconds.
When this happens, the timer interrupt routine2 is no longer executed. After the infamous seconds it seems that the timer2 overflows and generates the interrupt that restarts the pwm.
I'm using stm32F407 with pwm freq of 1KHz.

I have the impression that "stm32_pwm.c" pwm_callback () does not take into account that pwm_config [i] .dutyCycle has changed even before finishing the full cycle of the pwm.
Perhaps you need to create a local copy of pwm_config [i] .dutyCycle which is then updated only at the end of the pwm cycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants