Practical_Electronics-May_2019

([email protected]) #1

up to 5A/48V (8A if you use the speci-
fi ed relay from Altronics).
You have the option to get the relay
to switch on if the sensor signal rises
above the threshold frequency (set by
S2) and switch off if the sensor signal
drops below the threshold set by S1.
The alternative is to get the relay to
switch on if the sensor drops below
the lower threshold frequency (set by
switch S1) and switch off if the sensor
signal rises above the threshold fre-
quency (set by switch S2). The second
mode is activated by installing a link
at JP1 on the PCB.


Block diagram
Fig.1 (above) shows how the Deluxe
Frequency Switch monitors the signal
frequency. The PIC16F88 micro’s in-
ternal clock is derived from a 20MHz
crystal which is driven by an internal
oscillator amplifi er.
The resulting 20MHz clock signal
is divided by four to produce a 5MHz
signal which drives an internal 16-bit
timer, TIMER1. This comprises two
8-bit cascaded timers, TIMER1H and
TIMER1L.
We have implemented an 8-bit
overfl ow counter (OVER) in the unit’s
fi rmware. That extends TIMER1 out to
24 bits, so it rolls over every 3.355 sec-
onds [or 2^24 ÷5,000,000]. This equates
to an input frequency close to 0.3Hz.
Hence, the unit is designed to handle
signals from 1Hz and up.
The input signal is fed to pin 6,
which is also the Capture/Compare/
PWM (CCP) pin. The Capture module
hardware in the micro is confi gured so
that on each positive signal transition
at this pin (ie, low-to-high), the values
of TIMER1H and TIMER1L are copied
into the CAPTURE1H and CAPTURE-
1L registers and an interrupt fl ag is set.
This then triggers an interrupt han-
dler routine which copies the contents
of the OVER register into the CAPTURE
OVER variable.
The timers and overfl ow counters are
then reset to zero, ready to count until
the next input positive-going edge.
The captured count represents the
number of pulses from the 5MHz clock
signal over the period between the two
positive input signal edges.
So for example, a 1Hz input signal
will have a one-second period between
each positive edge. The count value
stored will therefore be 5,000,000 (5M).
At 1kHz, the period between positive
edges is 1ms and the captured value
will be 5000.
To calculate the frequency, all we
need to do is to perform the calculation
F(Hz) = 5,000,000 ÷ value. Or we can
calculate the period as P(s) = value ÷
5,000,000.


In reality, the micro
just has to convert the up-
per and lower threshold
settings to these same
count units and then
compare the counter
values to those stored
values, to determine
whether either threshold
has been crossed.

On-board frequency
generation
Where the microcon-
troller produces an out-
put frequency for you
to measure during ad-
justment (as per setup
method on page 23),
pin 6 (CCP1) is confi gured differently.
Rather than being in Capture Mode,
with pin 6 as an input, it is used in
Compare Mode and pin 6 is an output.
TIMER1 is still driven with the
same 5MHz signal but the TIMER1L,
TIMER1H and OVER registers are pre-
loaded with values calculated from
the frequency to be produced. Each
time OVER register overfl ows, the pin
6 output toggles and new values are
loaded into the TIMER1L, TIMER1H
and OVER registers.
Because pin 6 toggles each time
the counters overfl ow, the output fre-
quency would be half what you might
expect based on the period value for
the required frequency. So we need to
divide the period by two to give two
separate half periods.
This means there will be an error
whenever an odd period value is used,
since dividing it by two will yield a
remainder of one.
To solve this, and avoid the inaccu-
racy, two different pre-load values are
used. They are used alternately to load
into the TIMER1L and TIMER1H regis-
ters. So the duty cycle will not quite be
50% but the frequency produced will
be accurate. The values from each of the
separate period values are loaded into
the TIMER1L, TIMER1H and OVER
counters alternately. At the same time,
the output at pin 6 is changed in level.
For those interested, the values to
pre-load into TIMER1L, TIMER1H
and the OVER variable are calculated

as 2^24 – (5,000,000 ÷ f (Hz)) ÷ 2, with
the alternative value being one higher
in cases where 5,000,000 ÷ f Hz is odd.

Circuit description
The full circuit shown in Fig.2 is based
on microcontroller IC1, a PIC16F88.
This monitors the input frequency,
jumper state (JP1 and JP2), switch state
(S1 and S2) and trimpot settings (VR1
and VR2). It also drives the frequency
LEDs (LED2-LED5), threshold LED
(LED6) and the relay coil (RLY1) and
its associated LED (LED7) via NPN
transistor Q2.
Power is fed in via CON1 and the
supply is nominally 12V DC. Diode
D1 provides reverse polarity protection
and its cathode connects directly to the
positive terminal of the relay coil, ap-
plying a nominal 11.4V to it as well as
to the 5V regulator, REG1 and it powers
the rest of the circuit.
A 10mF electrolytic capacitor is
used to fi lter the supply voltage and
transients are clamped using a 16V
zener diode (ZD1), with the peak cur-
rent limited by the series 47Ω resistor.
The supply is further fi ltered by
another 10mF capacitor and then REG1
reduces the 11.4V supply to 5V for IC1
and input conditioning transistor Q1.
The power LED (LED1) is connected
across the 5V supply with a 3.3kΩ series
current-limiting resistor.
The input signal is fed into CON2
and it’s AC-coupled via a 10mF
capacitor and 10kΩ resistor to the

Features
• Energises a relay when a signal goes above
a preset frequency and keeps it on until the
signal drops below a second preset frequency
• Adjustable hysteresis can be used instead of
setting upper and lower frequencies
• Switching frequency can be from 1Hz to 10kHz
• Adjustable switching delay
• Two sets of 5A changeover relay contacts
• Easy pushbutton set-up
• Can be set up on the bench or in situ
• Threshold can be set using a signal
generator or frequency meter (eg, DMM)
• On-board signal frequency range indicators
• Power, threshold and relay-on LED indicators.

Fig.1: This block diagram describes how the microcontroller measures frequency.
Free download pdf