Milazzo, Carol F., KP4MD, "Audio Frequency Counter Program for the Timex/Sinclair ZX81", Syncware News, Vol 3:1, September-October 1985. QZX, Vol 2:11, October 1984. A Z80 machine language program to use the ZX81 as an audio frequency counter.

This program will run with as little as 1K of RAM and will allow frequency measurements of up to 28,500 Hz with the ZX-81, TS-1000 or TS-1500 computer. This is useful for tuning RTTY tone generators, electronic musical instruments, tone encoders, and for calibrating audio generators. The audio signal is fed to the computer's earphone jack, and the frequency is displayed to the nearest Hz on the screen.

The work is done by a 62 byte machine language routine which is stored in the first REM statement (memory locations 4082H to 40BFH). The hexadecimal codes should be loaded with a machine code loader having first entered a REM statement with 62 spaces. Alternatively, the decimal equivalents may be individually POKEd into these locations (16514 to 16575). The entry point to the machine language routine is at address 4092H. Here, the value for 28501 (7055H)* is loaded into the BC register pair, the value 3FH is loaded into the D register and the HL register pair is initialized to zero. A call is then made to the cassette port sampling loop at 4082H to 4091H. Each cycle of the loop requires an average of 57.015 machine cycles and decrements the BC register pair by one. When the value of BC equals zero, exactly ½ second has passed and a return is made from the loop. During each loop cycle input port FE is sampled. If the input is high, 3F is loaded into the accumulator; if it is low, then BF is loaded instead. This value is compared with the previous value which had been stored in register D, and the HL register pair is incremented if a change has occurred. As each audio signal cycle involves two transitions, the count in the HL register pair after ½ second is equivalent to the number of audio signal cycles in 1 second. Upon returning from the loop, the previous displayed frequency is erased by printing 5 spaces at the beginning of the display file. If the count in HL was zero a return is made to BASIC, otherwise the print position is returned to the beginning of the display file and the contents of HL are printed as a 5 digit decimal integer with leading zeroes suppressed (using ROM routines called at 40B4H through 40BCH). The screen is blank during the ½ second sampling period and the frequency is displayed during the pause in statement 5. The applied signal voltage should be about 3 or 4 volts (amplification through the cassette recorder's monitor output may be sufficient for some weaker signals). Lowest measurable frequency varies from 100 to 200 Hz and is limited by the computer's cassette port circuitry.

The accuracy of the frequency count depends on the computer's clock frequency which is nominally 3.25 MHz. The TS-1500 which uses a quartz crystal is very accurate. However, the frequency of the ceramic elements in the other models may vary significantly. The initial value of the BC register pair can be changed to compensate for these variations. Measure the clock frequency with a counter or a loosely-coupled well-calibrated receiver. Divide the frequency in Hz by 114.03 and convert to hexadecimal. Load the least significant byte into the B register;* add 1 to the most significant byte and load into the C register. E.g., for a clock frequency of 3.225 MHz: 3225000/114.03 = 28282 = 6E7AH. Change machine code at 4092H to 016F7A (LD BC,7A6F).


4082:1002       DJNZ 4086
4084:0D         DEC C
4085:C8         RET Z
4086:DBFE       IN A,FE
4088:BA         CP D
4089:2804       JR Z,408F
408B:57         LD D,A
408C:23         INC HL
408D:18F3       JR 4082
408F:C0         RET NZ
4090:18F0       JR 4082
4092:017055     LD BC,5570
4095:163F       LD D,3F
4097:210000     LD HL,0000
409A:CD8640     CALL 4086
409D:E5         PUSH HL
409E:CDF508     CALL 08F5
40A1:0605       LD B,05
40A3:AF         XOR A
40A4:D7         RST 10H
40A5:10FC       DJNZ 40A3
40A7:E1         POP HL
40A8:7C         LD A,H
40A9:B5         OR L
40AA:C8         RET Z
40AB:E5         PUSH HL
40AC:010000     LD BC,0000
40AF:CDF508     CALL 08F5
40B2:E1         POP HL
40B3:E5         PUSH HL
40B4:1E00       LD E,00
40B6:01F0D8     LD BC,D8F0
40B9:CDE107     CALL 07E1
40BF:C9         RET


1 SEP 1984

4 RAND USR 16530
5 PAUSE 150
6 GOTO 4