Square wave generator and frequency meter

Also i changed variables to volatile

volatile unsigned int timervalue, overflows, timeovf;
float hz;
unsigned long timeofrev;
void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2), rpmcheck, RISING);
  Serial.begin(115200);
  cli();          
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  TCCR1B |= (1 << CS10);    
  TIMSK1 |= (1 << TOIE1); 
  sei();  
}

void loop() {
  cli();
  timeofrev = overflows*65535UL+timervalue;
  sei();
  hz = 16000000/timeofrev;
  Serial.println(hz, 2);
  delay(500);
}

ISR(TIMER1_OVF_vect){
  timeovf++;
}

void rpmcheck() {
  timervalue = TCNT1;
  TCNT1 = 0;
  overflows = timeovf;
  timeovf = 0;
}