Tuesday, May 31, 2022

tinkercat timer interrupt measure pulse duration


press and release button, button hold duration is displayed
int period;

void setup()
{
  pinMode(7, INPUT_PULLUP);
  
  PCICR = 0b00000100; //enable pin change interrupt on group 3
  PCMSK2 = 0b10000000; //enable pin change interrupt on pin 7
  
  TCCR1A = 0b00000000; //TCCR1A bit 0, 1 and TCCR1B bit 3, 4 set timer mode
  TCCR1B = 0b00000101; //Set mode to be normal. set scaler to be clock/1024

  sei();
  Serial.begin(9600);
}

ISR (PCINT2_vect){
  if(digitalRead(7)){
  period = TCNT1 *0.0000625*1024;
  Serial.print("button press periord:");
  Serial.println(period);
  }
  else{
    TCNT1 = 0;
  }
}

void loop()
{
}

reference:

No comments:

Post a Comment