timer = solved

What are you developing?
Post Reply
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

timer = solved

Post 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)) ;
  }
}
Last edited by stan on Thu Nov 19, 2020 6:58 pm, edited 1 time in total.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: timer

Post 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
stan
Posts: 70
Joined: Wed Nov 11, 2020 7:40 pm

Re: timer

Post 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();
  }
}
Post Reply

Return to “Projects”