Arduino Nano input capture

First try with interrupts. Reading period time of squarewave generator at pin 8 (ICP1)
Doesnt seem to work, outputs random numbers.
Input freq = 1000 Hz 5Vt, result should be app. 2000. Why not.

//Arduino Nano
//Input capture on timer1 pin D8 (AVR pin12)
word ICin,pICR1=0;

void setup() {
  Serial.begin(9600);
  while(!Serial){};
  
  TCCR1B=(1<<CS11);//Start timer1,prescaler 8
  TIMSK1=(1<<ICIE1);//enable capture int
  TIFR1=(1<<ICF1);//enable capture
}

void loop() {
  delay(1000);//uses timer0
  Serial.println(ICin);
}

ISR(TIMER1_CAPT_vect)
{
  if (ICR1>pICR1) {
    ICin=ICR1-pICR1;//no timer overflow
  }
  else {
    ICin=(ICR1+0x10000)-pICR1;//timer overflow
  }
  pICR1=ICR1;    
}

haven't look at the code, but interrupts variables that are used within the main code need to be volatile (and you'd be better off making a copy within a protected section in the loop before printing them)

--

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Input freq = 1000 Hz 5Vt, result should be app. 2000. Why not.

There are pwm timer presets in the Arduino core to enable analogWrite().

You want to be in normal mode for your icp sketch.

Clear the TCCR1A register in setup().

void setup() {
  Serial.begin(9600);
  while(!Serial){};
  TCCR1A = 0; //clear presets
  TCCR1B=(1<<CS11);//Start timer1,prescaler 8
  TIMSK1=(1<<ICIE1);//enable capture int
  TIFR1=(1<<ICF1);//enable capture
}