I've made a few tests, this code starts working good from 5Mhz. At lower frequencies readout unstable or impossible.
BUT I find a way how to run this algorithm with DCO on 1 MHz. All you need is to rewrite the arduino-like part using defines instread of using functions. This is my code:
//------------------------------------------------------------------------------
// Arduino like digitalRead, pinMode, and digitalWrite and some other helper functions.
//------------------------------------------------------------------------------
#define LOW 0
#define HIGH 1
#define INPUT 0
#defin