Path: Home =>
Applications => Frequency counter
40MHz-Frequency counter with ATmega8
This application of an AVR describes a frequency counter with ATMEL's ATmega8 with the
The frequency counter consists of an input unit (preamp and prescaler) and the
- Digital- and Analog-input, voltage measuring input
- Input stage with preamp and prescaler by 16
- Display with a single- ode double-line LCD
- Nine measuring- and display modes, adjustable with a potentiometer:
- 0: Frequency measurement with prescaler, gate time 0,25 s,
result in Hz
- 1: Frequency measurement without prescaler, gate time 0,25 s,
result in Hz
- 2: Frequency measurement as period measurement with conversion,
result in 0,01 Hz bzw. 0,001 Hz
- 3: Rounds per Minute, without prescaler, via period measurement
and conversion, result in rpm
- 4: Period duration complete cycle, result in microseconds
- 5: Period duration high-period, result in microseconds
- 6: Period duration low-period, result in microseconds
- 7: Period portion high-period, result in 0,1%
- 8: Period portion low-period, result in 0,1%
- 9: Voltage measurement, result in 0,001 Volt
- The selection of mode 9 (potentiometer on the right) swiches
frequency/time/period measurements off.
- SIO-Interface for connecting to a PC
- Xtal oscillator 16 MHz
The input unit has an analogue and a digital input unit.
The signal on the analogue input is amplified by a fast operation amplifier
NE592. Its output signal is adjusted to the the input of a NAND 74HCT132.
The digital input is directly fed into the NAND.
Depending from a processor signal the output of the NAND is either transferred
directly to the processor's counting port or is divided by 16 by a 74HCT93.
The processor unit is designed around the ATmega8. The processor is
driven with an xtal of 16 MHz on its pins XTAL1 and XTAL2. The
ceramic capacitors of 22 pF help to accelerate the oscillator
The supply voltage of 5 volts is attached to pin 8 (GND) and 7 (GND)
and blocked by a ceramic concensator of 100 nF. Supply of the AD
converter is by 5 volt over an inductor of 22 µH to pin 22,
also blocked by a 100 nF condensator. The internal reference voltage
is filtered by a film capacitor of 100 nF.
The AD conversion input pin PC1 is attached to the potentiometer that
selects the mode. The resistor of 100 k limits its output
voltage to 2,5 V. If a certain mode shall be fixed, a trim
potentiometer or a voltage divider with two resistors can be used.
On the AD converter channel ADC0 (on PC0) is attached to a voltage
divider for the input signal to measure input voltages. In the default
case, a maximum of 5.12 V can be measured. By changing the
voltage divider resistors, range and resolution can be easily changed.
The input of the ADC is blocked against high frequency influence.
The signals TXD and RXD make up the serial interface and attached to
the driver IC MAX232. The electrolytic condensators on the MAX232
provide the necessary voltages for the serial RS232 interface. The
serial signals are attached to a 10-pole connector that delivers
a 9-pole standard serial connector. The signals CTS, DSR and CD are
activated by resistors of 2k2 to the supply voltage.
The I/O pin PC5 drives the prescaler. If high, the prescaler is
disabled, if low the signal is divided by 16.
The signal input from the preamp/prescaler is attached to the input
INT0 (for edge detection) as well as to T0 (for counting signals).
The port bits PB0 to PB3 serve the four data bits of the LCD. The
Enable-input of the LCD is driven by PB5, its RS input by PB4.
The Enable-input is via a 100 k resistor attached to GND to
disable the input in case the port bit is inactive. The V0 input
of the LCD is attached to a trim potentiometer of 10 k to
adjust the contrast of the LCD.
The port bits MOSI, SCK and MISO as well as the RESET signal are
attached to a 10-pole connector, over which the processor can be
programmed. The red LED is on if programming is active. Supply
voltage lines are also attached to this connector.
The RESET input is attached with a 10 k resistor to the
The LCD is attached over a standard 14-pole connector to the
processor unit. You can use LCDs with 8, 16 or more characters
per line and single or double lines (adjustable via software).
The counter has a SIO connection, so measuring data can be read and
parameters can be written.
The complete circuit was build on a breadboard of 10 * 5 cm
and wired with enammelled copper.
Two of the srews for the LCD also fix the breadboard.
All components, including the supply and a 9 V battery fit into a small
Operation is very easy. A selection between analogue and digital input is
not necessary, just attach the source to the required female connector.
Just adjust the trim potentiometer to a setting where signals from the
analogue input are measured, and where no false signals from the opamp
confuse these measurements.
The mode selector (potentiometer) selects the mode. If no input signals
are present, and a mode based on signal duration measurement is selected,
update of the display is not performed immediately but on the first detected
The display of results on a two-line-LCD with 16 characters is as follows:
|0||Frequency||Counting, Prescaler=16||F=99,999,999 Hz|
|1||Frequency||Counting, Prescaler=1||f= 9,999,999 Hz|
|2||Frequency||Period duration||v= 9,999.999 Hz|
|3||Rounds per minute||Period duration||u= 9,999,999 rpm|
|4||Period duration||Period duration||t=99,999,999 us|
|5||High-period||Period duration||h=99,999,999 us|
|6||Low-period||Period duration||l=99,999,999 us|
|7||High-period portion||Period duration||P=100.0%|
|8||Low-period portion||Period duration||p=100,0%|
In a single-line LCD, the voltage is displayed only if mode 9 is selected.
At less than 16 character per line, thousand separators and dimension are
not displayed. Abbreviations for the measuring code are diplayed only if
the displayed value does not require this space.
The voltage on the input pin is measured and displayed four times per second.
The software is completely written in Assembler. Prior to assembling the source
code the internal adjustments have to be made (see 3.1). During or after
programming the hex code to the chip's flash the fuses have to be changed
(see 3.2). Note that after setting the fuses, the chip is only accessible
with an external xtal attached.
The following adjustments have to be made on the source code file
In its original form ATmega8 runs with its internal RC oscillator. To change this
the fuses must be adjusted to the external xtal.
- The switches debug and debugpulse must be set to 0.
- If a LCD is attached, cDisplay must be set to 1.
- If the attached LCD is 8-character-per-line cDisplay8 must be set to 1.
For larger LCDs, cDisplay8 must be cleard to 0.
- If the attached LCD has a single line only, cDisplay2 must be set to 0.
For two or more lines set this to 1.
- If the serial interface is attached and should be used, set cUart to 1.
- If the prescaler by 16 is connected to a different portbit than PC5, the
symbols pPresc and pPrescD as well as bPresc is to be set accordingly.
- If the processor clock is different from 16 MHz, change cFreq
- If the SIO's baudrate should be different from 9,600 bd, change cBaud.
- If the voltage divider for measuring voltages is not (1 M and 1 M), adjust
cR1 and cR2 accordingly. If the displayed voltages are different from the input
voltage, change cRin. Smaller values of cRin yield higher displayed voltages.
The following adjustments shown are with ATMEL's Studio:
In PonyProg 2000 the same looks like this:
Please note: After changing the fuses, the ATmega8 only works with an xtal attached.
The source code can be displayed in HTML format (here)
and downloaded as assembler source code file (here).
The text file ReadMe3.txt has additional hints for
programming and operation.
©2006-2009 by http://www.avr-asm-tutorial.net