| To functionally test some
circuits, a need often arises to measure the
frequency of a signal. External frequency
counters may be used, but can be expensive and
may offer more capabilities and/or accuracy than
actually needed. If your signal is below about 40
KHz, the following software program could be good
enough for your application. Plus, no extra
equipment is needed!
This frequency
counter program has some limitations that should
be considered:
- Timing is
dependent upon the FT-100 microprocessor crystal.
For critical measurements, this
may not be good enough.
- Only digital inputs in the main unit (module A)
can be used.
- The count duration is fixed at about 0.65
seconds.
How the program works
The actual
frequency counting is done by an assembly
subroutine. Parameter values are passed to/from
the assembly routine through designated RAM
locations. Once the main test program loads the
necessary parameters into RAM, they do not
normally need to be reloaded again unless the
program is stopped or restarted.
The assembly
subroutine monitors the input line and counts the
number of positive-going pulses for the count
duration of about 0.65 seconds. It will then
place the count value in the RAM locations and
return to the main program. The main program then
takes the raw count value and converts it into
frequency (Hz) by multiplying it by a constant
(CONST).
Calibration
Because of the
tolerances inherent in all crystals, this routine
should be calibrated using a good frequency
counter or signal generator. To calibrate, input
a known-frequency signal and record the CNT value
returned from the assembly routine. The CONST
value is derived from the following:
CONST = FREQUENCY
/ CNT
Once CONST is
found, place this value in your program to be
multiplied times the CNT value returned from the
assembly subroutine.
The Frequency Counter
Program
This frequency
counter program simply displays, on the LCD, the
frequency of the incoming signal. Your program
will most likely need to handle the frequency
value differently.
Be very careful
when copying the program, especially the assembly
DATA statements! Comments may be discarded. They
are included only for clarity.
FREQ1
'Program Name
'
PROG_LEN = 50 '# of
bytes in assembly program
'
'--- Test for enough memory ---
LAB_BEG=PEEK 45+(PEEK
46*256)+((PEEK 47+(PEEK 48*256))*16)
STR_BEG=PEEK 59+(PEEK
60*256)+((PEEK 61+(PEEK 62*256))*16)
IF
LAB_BEG<(STR_BEG+PROG_LEN)+16 THEN
BEEP:CLS:DISP "NOT ENOUGH":DISP
"MEMORY!":END
'
PROG_SEG=INT
(STR_BEG/16)+1
ADDR=PROG_SEG*16:FOR I=0
TO PROG_LEN
READ X:POKE
(I+ADDR),X:NEXT 'Load in assembly program
'
'-------------------- Assembly Program
--------------------
DATA
0BEH,7AH,1,2BH,0FFH,8AH,14H,2AH
DATA
0F6H,46H,8AH,3CH,46H,8BH,0CH,46H
DATA
46H,8AH,0E7H,0ECH,22H,0C7H,3AH,0C4H
DATA
74H,0BH,3CH,0,74H,0BH,47H,90H
DATA
90H,90H,0EBH,8,90H,86H,0C0H,86H
DATA
0C0H,0EBH,1,90H,8AH,0E0H,0E2H,0E3H
DATA 89H,3CH,0CBH
'------------------------------------------------------------------
'
CLS:CONST = 1.551868
'Calibration constant
'
' --- Set up input parameters to send to
frequency subroutine ---
LINE_IN$="LA42" 'Input line (any
digital input line in main unit)
'
'
'--- Insert parameter values in RAM ---
X=VAL
RIGHT$(LINE_IN$,2)-41
PORT=INT(X/8) 'Port #
L=INT((X/8-PORT)*8)
'Line#
LINE=1:IF L=0 THEN GOTO
L1
FOR I=1 TO L:SHL
LINE:NEXT 'Line Mask
'
L1: POKE 17AH,PORT 'Insert line address
information in RAM
POKE 17BH,LINE
'
POKE 17CH,0:POKE 17DH,0
'Insert count time information in RAM
'
'
L0: SYS PROG_SEG,0 'Call frequency count
subroutine
CNT=PEEK 17FH*256 + PEEK
17EH 'Number of cycles during count duration
FREQ=CNT*CONST 'FREQ =
frequency in Hz.
'
CLS 2:DISP " CNT
=";CNT
CLS 3:DISP "FREQ
=";FREQ
GOTO L0
/
NOTICE:
Every effort has been made to insure the accuracy
of the information contained in this document,
however
Y-tek is not responsible for any consequences
resulting from erroneous or inaccurate
information.
|