A Z80 machine language program to use the ZX81 as an audio frequency counter

See all articles from QZX v2 n11
Author(s): Carol F. Milazzo (KP4MD)
Publication: QZX
Pub Details: QZX v2 n11 (Volume 2, Issue 11)
Date: October 1984

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).

AUDIO FREQUENCY COUNTER PROGRAM MACHINE LANGUAGE ROUTINE

4082:1002        DJNZ E
4084:0D DEC C
4085:C8 RET Z
4086:DBFE IN A,(N)
4088:BA CP D
4089:2804 JR Z,E
408B:57 LD D ,A
408C:23 INC HL
408D:18F3 JR E
408F:C0 RET NZ
4090:18F0 JR E
4092:017055 LD BC,NN
4095:163F LD D,N
4097:210000 LD HL,NN
409A:CD8640 CALL NN
409D:E5 PUSH HL
409E:CDF508 CALL NN
40A1:0605 LD B,N
40A3:AF XOR A
40A4:D7 RST 10H
40A5:10FC DJNZ E
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,NN
40AF:CDF508 CALL NN
40B2:E1 POP HL
40B3:E5 PUSH HL
40B4:1E00 LD E,N
40B6:01F0D8 LD BC,NN
40B9:CDE107 CALL NN
40BC:CDAD0A CALL NN
40BF:C9 RET

AUDIO FREQUENCY COUNTER PROGRAM BY CAROL F. MILAZZO, KP4MD 1 SEP 1984

1 REM [illegible]
2 FAST
3 PRINT AT 0,6;"HZ FREQUENCY COUNTER"
4 RAND USR 16530
5 PAUSE 150
6 GOTO 4

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.