Page 1 of 1

timer = solved

Posted: Thu Nov 19, 2020 3:26 pm
by stan
HI
I want to have signal on PB7 when PB11 is pressed, I think can be done by attachInterrupt / detachInterrupt in timer or pin PA7.
This is what I was trying, not working.

Code: Select all

HardwareTimer pwmtimer3(3);
void setup() {

  pinMode(PA7, PWM);// CH2
  pinMode(PB11, INPUT_PULLDOWN);

  pwmtimer3.pause();
  pwmtimer3.setPrescaleFactor(180);
  pwmtimer3.setOverflow(100 - 1);
  pwmtimer3.setCompare(TIMER_CH2, 50);
  //pwmtimer3.attachCompare1Interrupt(timer3);
  pwmtimer3.refresh();
  pwmtimer3.resume();
}

void loop() {
  if (digitalRead(PB11) == HIGH)
  {

    attachInterrupt(digitalPinToInterrupt(PA7));

  }
  else {

    detachInterrupt(digitalPinToInterrupt(PA7)) ;
  }
}

Re: timer

Posted: Thu Nov 19, 2020 5:21 pm
by ag123
don't think attachInterrupt works that way, an alternative implementation is to pause() the timer in setup() then resume() the timer when pressed.
so one of the if clauses is to pause() the timer the other to resume() the timer

Re: timer

Posted: Thu Nov 19, 2020 6:57 pm
by stan
Thanks for suggestions, problem solved.

Code: Select all

HardwareTimer pwmtimer3(3);
void setup() {

  pinMode(PA7, PWM);// CH2
  pinMode(PB11, INPUT_PULLDOWN);

  pwmtimer3.pause();
  pwmtimer3.setPrescaleFactor(180);
  pwmtimer3.setOverflow(100 - 1);
  pwmtimer3.setCompare(TIMER_CH2, 50);
  //pwmtimer3.refresh();
  pwmtimer3.resume();
}

void loop() {
  if (digitalRead(PB11) == LOW)
  {
    pwmtimer3.refresh();
  }
}