PWM problem

Hi
When I turn on disabled lines in loop PWM is disappears, how to fix that ?

volatile int i = 0; //initializing a integer for incrementing and decrementing duty ratio.
void Tim2_ISR(void)
{
  Timer2.setCompare(TIMER_CH4, i);
}


HardwareTimer pwmtimer2(2);

void setup() {
  pinMode(PB3, INPUT_PULLDOWN);// sets the pin0 as output
  pinMode(PB5, INPUT_PULLDOWN);// sets the pin1 as output
  pinMode(PA3, PWM);

  pwmtimer2.pause();
  pwmtimer2.setPrescaleFactor(100);       // Timer input clock Prescaler = 1 (= 72MHz input ?)
  pwmtimer2.setOverflow(256);          // PWM Period width for 7,2kHz ?
  pwmtimer2.setCompare(TIMER_CH4, 128);  // PWM High Pulse width is 50% duty (1:1)
  pwmtimer2.refresh();
 //Timer2.attachInterrupt(0, Tim2_ISR);
  pwmtimer2.resume();
}

void loop()

{
  /*
/////////////////////////////////////////
  analogWrite(PA3, i);  // analogWrite values from 0 to 255

  if (digitalRead(PB3) == HIGH)

  {

    if (i < 255)

    {

      i++;//if pin PB3 is pressed and the duty ratio value is less than 255

      delay(30);

    }

  }

  if (digitalRead(PB5) == HIGH)

  {

    if (i > 0)

    {

      i--;// if pin PB5 is pressed and the duty ratio value is greater than 0

      delay(30);

    }

  }
///////////////////////////////////
*/
}

You can use timer 2 for your purposes OR you can use it for PWM. Which is more important to you?

I want to regulate the duty cycle of square pulses on pin PA3

ted:
I want to regulate the duty cycle of square pulses on pin PA3

So, what is the problem? Are you wanting to toggle the state of the pin using the standard PWM functionality OR using the timer differently?

You can't expect to use the timer for two different purposes without causing problems.

The problem is - why pulses on PA3 are disappeared when disabled lines are activated, there is nothing going on inside the loop until button is pressed = empty loop.

why pulses on PA3 are disappeared when disabled lines are activated

I've told you that. You can use the timer for PWM (what analogWrite() enables) OR for your own purposes.

Real small words time:

YOU CAN NOT USE THE TIMER FOR BOTH.

Read any of OP's previous threads. Dozens of posts in each. You're banging your head against the wall. MAJOR TIME SINK.

So it is not possible to regulate the pulse width ?

ted:
I want to regulate the duty cycle of square pulses on pin PA3

You can't regulate the duty cycle of a square wave, only the frequency.
If you change the duty cycle, it is no longer square.

so how they control the speed of DC motor with PWM ?

ted:
so how they control the speed of DC motor with PWM ?

By varying the duty cycle of a rectangular waveform.

You right, my mistake, but this is not answering the question post #4

The answer to the question in 4 appears to be in reply 5.

I see a closed loop - post # 4, 5, and 9.

ted:
I see a closed loop - post # 4, 5, and 9.

Not sure about that, but reply 6 is looking relevant.

Relevant or not so far no one show the solution

ted:
Relevant or not so far no one show the solution

Solution to what? You haven't posted a specification or a question.

How to regulate the pulse width using code post #0 ?

Pulse width or PWM duty cycle?
What are the parameters of your requirements?

Do you understand the similie "getting blood from a stone?"

The only one requirement - width regulation - I repeat that many times