Highest PWM frequency output for the Uno/Nano

To test the solution I made the sketch below. There are some strange observations:

  1. The serial monitor only works on 4800 baud (the setting in the sketch is 9600 baud.
  2. When setting a delay in de void loop the x value is only printed ones. Removing the delay reads the x value continuously.

It seems a lot is changing in the background when the timer is used. How to avoid this.

#include <TimerHelpers.h>

// Timer 0

// output    OC0B   pin 11  (D5)
# define Distance_PIN A0
const byte timer0OutputB = 5 ;
float xvalue;
 
void setup() {
  Serial.begin(9600);
  pinMode (timer0OutputB, OUTPUT);
  TIMSK0 = 0;  // no interrupts
  Timer0::setMode (7, Timer0::PRESCALE_1, Timer0::CLEAR_B_ON_COMPARE);
  OCR0A = 8;   // count to 4, zero-relative
  OCR0B = 3;   // duty cycle
}  // end of setup

void loop() 
{
  xvalue = analogRead(Distance_PIN);
  pinMode (timer0OutputB, INPUT);
  Serial.print ("X = ");
  Serial.println (xvalue);
  pinMode (timer0OutputB, OUTPUT);
  delay(1000);
}