CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 FOUT = 0 FREQ = 20_000_000 PUB start dira[FOUT]~~ set_freq(FREQ) repeat PUB set_freq(frq) | c ' Set output frequency to frq, unless frq == 0, in which case ' turn off the counter. if (frq == 0) ctra := 0 elseif (frq < 500_000) ctra := %00100 << 26 | FOUT else c := %00010_011 repeat while frq > 8_000_000 frq >>= 1 c++ repeat while frq < 4_000_000 frq <<= 1 c-- ctra := c << 23 | FOUT frqa := frq_val(frq) PRI frq_val(frq) : r ' Compute the required value of frqx to obtain the frequency frq. repeat 32 frq <<= 1 r <<= 1 if frq => clkfreq frq -= clkfreq r++