Microcontroller as a frequency generator

Many electronic experiences require clock generator. In common, analog clock generator can build clock signal with 5 % error, and digital clock generator can build it with 0.1 % error. Microcontroller-based clock signal generator has crystal accuracy. The useful frequency range that can be generated is between 1 Hz and 1 MHz when the microcontroller operating at a 24 MHz clock frequency. The 1 MHz frequency output can be generated with continuing to execute a one-cycle-instruction to complement the pin output. To generate a specific frequency below 1 MHz, the other instruction(s) such as no-operation can be inserted to delay the next instruction. The maximum error of 0.1 % can be generated at frequency output below 2 kHz and the maximum error of 1 % at frequency below 20 kHz. The selected frequency signal with 0.1 % or less error also can be generated for frequency below 1 MHz.
The main principle of microcontroller-based clock generator is toggling a output pin with specific time interval.
Here is a sample program for toggling pin 0 through 7 of port 0 with delay routine.
$MOD51
Org 0h
Start: Mov P1,#00001111B
call Delay
Mov P1,#11110000B
call Delay
Jmp Start
;------------------- Delay sub routine -------------------
Delay: Mov R0,#0FFh ;R0=FF (Hex)
Delay1: Mov R1,#0FFh ;R1=FF (hex)
Delay2: nop ;no operation
nop
nop
Djnz R1,Delay2
Djnz R0,Delay1
Ret
End


Here is a sample program will source the clock with its periode 6 ms.

$mod51
MOV P1,#0h
Repeat: CPL P1.0
JMP Repeat
END